diff --git a/plugin.yml b/plugin.yml index 5532bbbe..61ff85b1 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,5 +1,5 @@ name: Factions -version: 1.1.1 +version: 1.1.2 main: com.bukkit.mcteam.factions.Factions commands: f: diff --git a/releases/factions 1.1.2.zip b/releases/factions 1.1.2.zip new file mode 100644 index 00000000..c685ad24 Binary files /dev/null and b/releases/factions 1.1.2.zip differ diff --git a/src/com/bukkit/mcteam/factions/Board.java b/src/com/bukkit/mcteam/factions/Board.java index 222976d1..2fbb5a4e 100644 --- a/src/com/bukkit/mcteam/factions/Board.java +++ b/src/com/bukkit/mcteam/factions/Board.java @@ -1,11 +1,11 @@ package com.bukkit.mcteam.factions; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.logging.Level; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; @@ -14,6 +14,9 @@ import org.bukkit.ChatColor; import com.bukkit.mcteam.factions.util.TextUtil; import com.bukkit.mcteam.gson.reflect.TypeToken; +import com.bukkit.mcteam.gson.JsonArray; +import com.bukkit.mcteam.gson.JsonObject; +import com.bukkit.mcteam.gson.JsonParser; import com.bukkit.mcteam.util.AsciiCompass; import com.bukkit.mcteam.util.DiscUtil; @@ -204,7 +207,8 @@ public class Board { Factions.log("Loading board from disk"); if ( ! file.exists()) { - Factions.log("No board to load from disk. Creating new file."); + if ( ! loadOld()) + Factions.log("No board to load from disk. Creating new file."); save(); return true; } @@ -221,6 +225,50 @@ public class Board { return true; } + + private static boolean loadOld() { + File folderBoard = new File(Factions.instance.getDataFolder(), "board"); + + if ( ! folderBoard.isDirectory()) + return false; + + Factions.log("Board file doesn't exist, attempting to load old pre-1.1 data."); + + String ext = ".json"; + + class jsonFileFilter implements FileFilter { + @Override + public boolean accept(File file) { + return (file.getName().toLowerCase().endsWith(".json") && file.isFile()); + } + } + + File[] jsonFiles = folderBoard.listFiles(new jsonFileFilter()); + for (File jsonFile : jsonFiles) { + // Extract the name from the filename. The name is filename minus ".json" + String name = jsonFile.getName(); + name = name.substring(0, name.length() - ext.length()); + try { + JsonParser parser = new JsonParser(); + JsonObject json = (JsonObject) parser.parse(DiscUtil.read(jsonFile)); + JsonArray coords = json.getAsJsonArray("coordFactionIds"); + Iterator coordSet = coords.iterator(); + while(coordSet.hasNext()) { + JsonArray coordDat = (JsonArray) coordSet.next(); + JsonObject coord = coordDat.get(0).getAsJsonObject(); + int coordX = coord.get("x").getAsInt(); + int coordZ = coord.get("z").getAsInt(); + int factionId = coordDat.get(1).getAsInt(); + flocationIds.put(new FLocation(name, coordX, coordZ), factionId); + } + Factions.log("loaded pre-1.1 board "+name); + } catch (Exception e) { + e.printStackTrace(); + Factions.log(Level.WARNING, "failed to load board "+name); + } + } + return true; + } } diff --git a/src/com/bukkit/mcteam/factions/FPlayer.java b/src/com/bukkit/mcteam/factions/FPlayer.java index b94e7a11..fcc0053f 100644 --- a/src/com/bukkit/mcteam/factions/FPlayer.java +++ b/src/com/bukkit/mcteam/factions/FPlayer.java @@ -1,9 +1,9 @@ package com.bukkit.mcteam.factions; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.lang.reflect.Type; import java.util.*; +import java.util.logging.Level; import java.util.Map.Entry; import org.bukkit.ChatColor; @@ -470,7 +470,8 @@ public class FPlayer { public static boolean load() { Factions.log("Loading players from disk"); if ( ! file.exists()) { - Factions.log("No players to load from disk. Creating new file."); + if ( ! loadOld()) + Factions.log("No players to load from disk. Creating new file."); save(); return true; } @@ -515,5 +516,41 @@ public class FPlayer { } } } - + + private static boolean loadOld() { + File folderFollower = new File(Factions.instance.getDataFolder(), "follower"); + + if ( ! folderFollower.isDirectory()) + return false; + + Factions.log("Players file doesn't exist, attempting to load old pre-1.1 data."); + + String ext = ".json"; + + class jsonFileFilter implements FileFilter { + @Override + public boolean accept(File file) { + return (file.getName().toLowerCase().endsWith(".json") && file.isFile()); + } + } + + File[] jsonFiles = folderFollower.listFiles(new jsonFileFilter()); + + for (File jsonFile : jsonFiles) { + // Extract the name from the filename. The name is filename minus ".json" + String name = jsonFile.getName(); + name = name.substring(0, name.length() - ext.length()); + try { + FPlayer follower = Factions.gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class); + follower.playerName = name; + follower.lastLoginTime = System.currentTimeMillis(); + instances.put(follower.playerName, follower); + Factions.log("loaded pre-1.1 follower "+name); + } catch (Exception e) { + e.printStackTrace(); + Factions.log(Level.WARNING, "failed to load follower "+name); + } + } + return true; + } } \ No newline at end of file diff --git a/src/com/bukkit/mcteam/factions/Faction.java b/src/com/bukkit/mcteam/factions/Faction.java index 043d8e90..d926a60d 100644 --- a/src/com/bukkit/mcteam/factions/Faction.java +++ b/src/com/bukkit/mcteam/factions/Faction.java @@ -1,7 +1,6 @@ package com.bukkit.mcteam.factions; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.lang.reflect.Type; import java.util.*; import java.util.Map.Entry; @@ -345,7 +344,8 @@ public class Faction { Factions.log("Loading factions from disk"); if ( ! file.exists()) { - Factions.log("No factions to load from disk. Creating new file."); + if ( ! loadOld()) + Factions.log("No factions to load from disk. Creating new file."); save(); } @@ -392,7 +392,7 @@ public class Faction { } nextId += 1; // make it the next id and not the current highest. } - + public static Faction get(Integer factionId) { if ( ! instances.containsKey(factionId)) { Factions.log(Level.WARNING, "Non existing factionId "+factionId+" requested! Issuing cleaning!"); @@ -439,4 +439,41 @@ public class Faction { // Clean the fplayers FPlayer.clean(); } + + private static boolean loadOld() { + File folderFaction = new File(Factions.instance.getDataFolder(), "faction"); + + if ( ! folderFaction.isDirectory()) + return false; + + Factions.log("Factions file doesn't exist, attempting to load old pre-1.1 data."); + + String ext = ".json"; + + class jsonFileFilter implements FileFilter { + @Override + public boolean accept(File file) { + return (file.getName().toLowerCase().endsWith(".json") && file.isFile()); + } + } + + File[] jsonFiles = folderFaction.listFiles(new jsonFileFilter()); + for (File jsonFile : jsonFiles) { + // Extract the name from the filename. The name is filename minus ".json" + String name = jsonFile.getName(); + name = name.substring(0, name.length() - ext.length()); + int id = Integer.parseInt(name); + + try { + Faction faction = Factions.gson.fromJson(DiscUtil.read(jsonFile), Faction.class); + faction.id = id; + instances.put(faction.id, faction); + Factions.log("loaded pre-1.1 faction "+id); + } catch (Exception e) { + e.printStackTrace(); + Factions.log(Level.WARNING, "Failed to load faction "+id); + } + } + return true; + } }