Advertisement
Guest User

WorldData.java

a guest
Feb 11th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package musaddict.snowgrow;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.util.ArrayList;
  10. import java.util.UUID;
  11.  
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.World;
  14.  
  15. public class WorldData {
  16.     private String resourcePath;
  17.     public ArrayList<World> list;
  18.  
  19.     public WorldData(String resourcePath) {
  20.         this.resourcePath = resourcePath;
  21.  
  22.         File path = new File(resourcePath);
  23.  
  24.         if (!path.exists())
  25.             path.mkdir();
  26.     }
  27.  
  28.     private static ArrayList<UUID> toSerializableMap(ArrayList<World> worldList) {
  29.         ArrayList<UUID> newArrList = new ArrayList<UUID>();
  30.  
  31.         if (!worldList.isEmpty()) {
  32.             for (World w : worldList) {
  33.                 newArrList.add(w.getUID());
  34.             }
  35.         }
  36.  
  37.         return newArrList;
  38.     }
  39.  
  40.     public boolean saveWorldData() {
  41.         try {
  42.             ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(resourcePath + File.separator + "WorldList.data"));
  43.             out.writeObject(toSerializableMap(list));
  44.             out.flush();
  45.             out.close();
  46.  
  47.             return true;
  48.         }
  49.         catch (IOException e) {
  50.             //SnowGrow.Log(Level.SEVERE, e.getMessage());
  51.             e.getStackTrace();
  52.         }
  53.  
  54.         return false;
  55.     }
  56.  
  57.     private static ArrayList<World> fromSerializableMap(ArrayList<UUID> worldNameList) {
  58.         ArrayList<World> worldList = new ArrayList<World>();
  59.  
  60.         if (!worldNameList.isEmpty())
  61.             for (UUID id : worldNameList) {
  62.                 World w = Bukkit.getServer().getWorld(id);
  63.  
  64.                 if (w != null)
  65.                     worldList.add(w);
  66.             }
  67.  
  68.         return worldList; //May return empty map if no doors to load.
  69.     }
  70.  
  71.     @SuppressWarnings("unchecked")
  72.     public boolean loadWorldData() {
  73.         try {
  74.             if (new File(resourcePath + File.separator + "WorldList.data").exists()) {
  75.                 ObjectInputStream in = new ObjectInputStream(new FileInputStream(resourcePath + File.separator + "WorldList.data"));
  76.                 Object result = in.readObject();
  77.  
  78.                 list = fromSerializableMap((ArrayList<UUID>) result);
  79.             }
  80.             else //File dosen't exist, ignore and continue.
  81.                 list = new ArrayList<World>();
  82.  
  83.             return true;
  84.         }
  85.         catch (Exception e) {
  86.             //SnowGrow.Log(Level.SEVERE, e.getMessage());
  87.             e.getStackTrace();
  88.         }
  89.  
  90.         return false;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement