Advertisement
riking

Untitled

Jul 8th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.bukkit.configuration.serialization.ConfigurationSerialization;
  2. @SuppressWarnings("unchecked")
  3. @Override
  4. public void onEnable(){
  5.  
  6. File f = new File(this.getDataFolder() + "/database/" + "data.hashmap");
  7. map = HashBiMap.create();
  8. if(!f.exists()){
  9. System.out.println("Hashmap not found, creating a new one");
  10. map = HashBiMap.create();
  11. }else{
  12. try{
  13. System.out.println("Hashmap found, loading...");
  14. FileInputStream fileIn = new FileInputStream(this.getDataFolder() + "/database/" + "data.hashmap");
  15. ObjectInputStream in = new ObjectInputStream(fileIn);
  16.  
  17. Map<String, Object> inMap = in.readObject();
  18. for (Map.Entry<String, Object> entry : inMap.entrySet()) {
  19. BlockVector bvec = (BlockVector) ConfigurationSerialization.deserializeObject(entry.getValue());
  20. map.put(entry.getKey(), bvec);
  21. }
  22. in.close();
  23. fileIn.close();
  24. }catch (Exception e){
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. getServer().getPluginManager().registerEvents(this, this);
  30. }
  31.  
  32. @Override
  33. public void onDisable(){
  34. Map<String, Object> outMap = new HashMap<String, Object>();
  35. for (Map.Entry<String, BlockVector> entry : map.entrySet()) {
  36. Map<String, Object> ser = entry.getValue().serialize();
  37. ser.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, "BlockVector");
  38. outMap.put(entry.getKey(), ser);
  39. }
  40. try{
  41. System.out.println("Saving hashmap...");
  42. FileOutputStream fileOut = new
  43. FileOutputStream(this.getDataFolder() + "/database/" +
  44. "data.hashmap");
  45. ObjectOutputStream out = new ObjectOutputStream(fileOut);
  46. out.writeObject(outMap);
  47. out.flush();
  48. out.close();
  49. fileOut.flush();
  50. fileOut.close();
  51. }catch (Exception e){
  52. e.printStackTrace();
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement