diff --git a/src/com/massivecraft/factions/Board.java b/src/com/massivecraft/factions/Board.java index af2f61d3..763485e2 100644 --- a/src/com/massivecraft/factions/Board.java +++ b/src/com/massivecraft/factions/Board.java @@ -1,5 +1,6 @@ package com.massivecraft.factions; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -15,9 +16,12 @@ import com.massivecraft.factions.util.AsciiCompass; import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.util.Txt; +import com.massivecraft.mcore.xlib.gson.reflect.TypeToken; public class Board extends Entity implements BoardInterface { + public static final transient Type MAP_TYPE = new TypeToken>(){}.getType(); + // -------------------------------------------- // // META // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/BoardColl.java b/src/com/massivecraft/factions/BoardColl.java index e6d1e252..6e114427 100644 --- a/src/com/massivecraft/factions/BoardColl.java +++ b/src/com/massivecraft/factions/BoardColl.java @@ -54,7 +54,7 @@ public class BoardColl extends Coll implements BoardInterface { // Create file objects File oldFile = new File(Factions.get().getDataFolder(), "board.json"); - File newFile = new File(Factions.get().getDataFolder(), "board.json.old"); + File newFile = new File(Factions.get().getDataFolder(), "board.json.migrated"); // Already migrated? if ( ! oldFile.exists()) return; @@ -71,11 +71,10 @@ public class BoardColl extends Coll implements BoardInterface for (Entry entry2 : entry.getValue().entrySet()) { String[] ChunkCoordParts = entry2.getKey().trim().split("[,\\s]+"); - int chunkX = Integer.parseInt(ChunkCoordParts[0]); int chunkZ = Integer.parseInt(ChunkCoordParts[1]); - PS ps = new PSBuilder().chunkX(chunkX).chunkZ(chunkZ).build(); + TerritoryAccess territoryAccess = entry2.getValue(); board.setTerritoryAccessAt(ps, territoryAccess); diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 30ad53ca..27d4cecb 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Arrays; import com.massivecraft.factions.adapters.BoardAdapter; +import com.massivecraft.factions.adapters.BoardMapAdapter; import com.massivecraft.factions.adapters.FFlagAdapter; import com.massivecraft.factions.adapters.FPermAdapter; import com.massivecraft.factions.adapters.LazyLocationAdapter; @@ -137,6 +138,7 @@ public class Factions extends MPlugin .registerTypeAdapter(LazyLocation.class, new LazyLocationAdapter()) .registerTypeAdapter(TerritoryAccess.class, TerritoryAccessAdapter.get()) .registerTypeAdapter(Board.class, BoardAdapter.get()) + .registerTypeAdapter(Board.MAP_TYPE, BoardMapAdapter.get()) .registerTypeAdapter(Rel.class, new RelAdapter()) .registerTypeAdapter(FPerm.class, new FPermAdapter()) .registerTypeAdapter(FFlag.class, new FFlagAdapter()); diff --git a/src/com/massivecraft/factions/adapters/BoardAdapter.java b/src/com/massivecraft/factions/adapters/BoardAdapter.java index eeaf86db..e0bf4951 100644 --- a/src/com/massivecraft/factions/adapters/BoardAdapter.java +++ b/src/com/massivecraft/factions/adapters/BoardAdapter.java @@ -10,7 +10,6 @@ import com.massivecraft.mcore.xlib.gson.JsonElement; import com.massivecraft.mcore.xlib.gson.JsonParseException; import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; import com.massivecraft.mcore.xlib.gson.JsonSerializer; -import com.massivecraft.mcore.xlib.gson.reflect.TypeToken; import com.massivecraft.factions.Board; import com.massivecraft.factions.TerritoryAccess; @@ -24,23 +23,21 @@ public class BoardAdapter implements JsonDeserializer, JsonSerializer>(){}.getType(); + // -------------------------------------------- // @SuppressWarnings("unchecked") @Override public Board deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - return new Board((Map) context.deserialize(json, mapType)); + return new Board((Map) context.deserialize(json, Board.MAP_TYPE)); } @Override public JsonElement serialize(Board src, Type typeOfSrc, JsonSerializationContext context) { - return context.serialize(src.getMap(), mapType); + return context.serialize(src.getMap(), Board.MAP_TYPE); } } \ No newline at end of file diff --git a/src/com/massivecraft/factions/adapters/BoardMapAdapter.java b/src/com/massivecraft/factions/adapters/BoardMapAdapter.java new file mode 100644 index 00000000..d7c8a456 --- /dev/null +++ b/src/com/massivecraft/factions/adapters/BoardMapAdapter.java @@ -0,0 +1,71 @@ +package com.massivecraft.factions.adapters; + +import java.lang.reflect.Type; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentSkipListMap; + +import com.massivecraft.mcore.ps.PS; +import com.massivecraft.mcore.ps.PSBuilder; +import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; +import com.massivecraft.mcore.xlib.gson.JsonDeserializer; +import com.massivecraft.mcore.xlib.gson.JsonElement; +import com.massivecraft.mcore.xlib.gson.JsonObject; +import com.massivecraft.mcore.xlib.gson.JsonParseException; +import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; +import com.massivecraft.mcore.xlib.gson.JsonSerializer; + +import com.massivecraft.factions.TerritoryAccess; + +public class BoardMapAdapter implements JsonDeserializer>, JsonSerializer> +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static BoardMapAdapter i = new BoardMapAdapter(); + public static BoardMapAdapter get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Map deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + Map ret = new ConcurrentSkipListMap(); + + JsonObject jsonObject = json.getAsJsonObject(); + + for (Entry entry : jsonObject.entrySet()) + { + String[] ChunkCoordParts = entry.getKey().split("[,\\s]+"); + int chunkX = Integer.parseInt(ChunkCoordParts[0]); + int chunkZ = Integer.parseInt(ChunkCoordParts[1]); + PS ps = new PSBuilder().chunkX(chunkX).chunkZ(chunkZ).build(); + + TerritoryAccess territoryAccess = context.deserialize(entry.getValue(), TerritoryAccess.class); + + ret.put(ps, territoryAccess); + } + + return ret; + } + + @Override + public JsonElement serialize(Map src, Type typeOfSrc, JsonSerializationContext context) + { + JsonObject ret = new JsonObject(); + + for (Entry entry : src.entrySet()) + { + PS ps = entry.getKey(); + TerritoryAccess territoryAccess = entry.getValue(); + + ret.add(ps.getChunkX().toString() + "," + ps.getChunkZ().toString(), context.serialize(territoryAccess, TerritoryAccess.class)); + } + + return ret; + } + +} \ No newline at end of file