Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package musaddict.snowgrow;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.World;
- public class WorldData {
- private String resourcePath;
- public ArrayList<World> list;
- public WorldData(String resourcePath) {
- this.resourcePath = resourcePath;
- File path = new File(resourcePath);
- if (!path.exists())
- path.mkdir();
- }
- private static ArrayList<UUID> toSerializableMap(ArrayList<World> worldList) {
- ArrayList<UUID> newArrList = new ArrayList<UUID>();
- if (!worldList.isEmpty()) {
- for (World w : worldList) {
- newArrList.add(w.getUID());
- }
- }
- return newArrList;
- }
- public boolean saveWorldData() {
- try {
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(resourcePath + File.separator + "WorldList.data"));
- out.writeObject(toSerializableMap(list));
- out.flush();
- out.close();
- return true;
- }
- catch (IOException e) {
- //SnowGrow.Log(Level.SEVERE, e.getMessage());
- e.getStackTrace();
- }
- return false;
- }
- private static ArrayList<World> fromSerializableMap(ArrayList<UUID> worldNameList) {
- ArrayList<World> worldList = new ArrayList<World>();
- if (!worldNameList.isEmpty())
- for (UUID id : worldNameList) {
- World w = Bukkit.getServer().getWorld(id);
- if (w != null)
- worldList.add(w);
- }
- return worldList; //May return empty map if no doors to load.
- }
- @SuppressWarnings("unchecked")
- public boolean loadWorldData() {
- try {
- if (new File(resourcePath + File.separator + "WorldList.data").exists()) {
- ObjectInputStream in = new ObjectInputStream(new FileInputStream(resourcePath + File.separator + "WorldList.data"));
- Object result = in.readObject();
- list = fromSerializableMap((ArrayList<UUID>) result);
- }
- else //File dosen't exist, ignore and continue.
- list = new ArrayList<World>();
- return true;
- }
- catch (Exception e) {
- //SnowGrow.Log(Level.SEVERE, e.getMessage());
- e.getStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement