Creating a BoardMapAdapter as well to fix the GSON handling of Baord keys.

This commit is contained in:
Olof Larsson 2013-04-11 10:11:14 +02:00
parent 18b38d4320
commit 4cc36b8d96
5 changed files with 83 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package com.massivecraft.factions; package com.massivecraft.factions;
import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -15,9 +16,12 @@ import com.massivecraft.factions.util.AsciiCompass;
import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.Txt; import com.massivecraft.mcore.util.Txt;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class Board extends Entity<Board, String> implements BoardInterface public class Board extends Entity<Board, String> implements BoardInterface
{ {
public static final transient Type MAP_TYPE = new TypeToken<Map<PS, TerritoryAccess>>(){}.getType();
// -------------------------------------------- // // -------------------------------------------- //
// META // META
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -54,7 +54,7 @@ public class BoardColl extends Coll<Board, String> implements BoardInterface
{ {
// Create file objects // Create file objects
File oldFile = new File(Factions.get().getDataFolder(), "board.json"); 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? // Already migrated?
if ( ! oldFile.exists()) return; if ( ! oldFile.exists()) return;
@ -71,11 +71,10 @@ public class BoardColl extends Coll<Board, String> implements BoardInterface
for (Entry<String,TerritoryAccess> entry2 : entry.getValue().entrySet()) for (Entry<String,TerritoryAccess> entry2 : entry.getValue().entrySet())
{ {
String[] ChunkCoordParts = entry2.getKey().trim().split("[,\\s]+"); String[] ChunkCoordParts = entry2.getKey().trim().split("[,\\s]+");
int chunkX = Integer.parseInt(ChunkCoordParts[0]); int chunkX = Integer.parseInt(ChunkCoordParts[0]);
int chunkZ = Integer.parseInt(ChunkCoordParts[1]); int chunkZ = Integer.parseInt(ChunkCoordParts[1]);
PS ps = new PSBuilder().chunkX(chunkX).chunkZ(chunkZ).build(); PS ps = new PSBuilder().chunkX(chunkX).chunkZ(chunkZ).build();
TerritoryAccess territoryAccess = entry2.getValue(); TerritoryAccess territoryAccess = entry2.getValue();
board.setTerritoryAccessAt(ps, territoryAccess); board.setTerritoryAccessAt(ps, territoryAccess);

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import com.massivecraft.factions.adapters.BoardAdapter; import com.massivecraft.factions.adapters.BoardAdapter;
import com.massivecraft.factions.adapters.BoardMapAdapter;
import com.massivecraft.factions.adapters.FFlagAdapter; import com.massivecraft.factions.adapters.FFlagAdapter;
import com.massivecraft.factions.adapters.FPermAdapter; import com.massivecraft.factions.adapters.FPermAdapter;
import com.massivecraft.factions.adapters.LazyLocationAdapter; import com.massivecraft.factions.adapters.LazyLocationAdapter;
@ -137,6 +138,7 @@ public class Factions extends MPlugin
.registerTypeAdapter(LazyLocation.class, new LazyLocationAdapter()) .registerTypeAdapter(LazyLocation.class, new LazyLocationAdapter())
.registerTypeAdapter(TerritoryAccess.class, TerritoryAccessAdapter.get()) .registerTypeAdapter(TerritoryAccess.class, TerritoryAccessAdapter.get())
.registerTypeAdapter(Board.class, BoardAdapter.get()) .registerTypeAdapter(Board.class, BoardAdapter.get())
.registerTypeAdapter(Board.MAP_TYPE, BoardMapAdapter.get())
.registerTypeAdapter(Rel.class, new RelAdapter()) .registerTypeAdapter(Rel.class, new RelAdapter())
.registerTypeAdapter(FPerm.class, new FPermAdapter()) .registerTypeAdapter(FPerm.class, new FPermAdapter())
.registerTypeAdapter(FFlag.class, new FFlagAdapter()); .registerTypeAdapter(FFlag.class, new FFlagAdapter());

View File

@ -10,7 +10,6 @@ import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonParseException; import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer; import com.massivecraft.mcore.xlib.gson.JsonSerializer;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
import com.massivecraft.factions.Board; import com.massivecraft.factions.Board;
import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.TerritoryAccess;
@ -24,23 +23,21 @@ public class BoardAdapter implements JsonDeserializer<Board>, JsonSerializer<Boa
private static BoardAdapter i = new BoardAdapter(); private static BoardAdapter i = new BoardAdapter();
public static BoardAdapter get() { return i; } public static BoardAdapter get() { return i; }
//----------------------------------------------// // -------------------------------------------- //
// OVERRIDE // OVERRIDE
//----------------------------------------------// // -------------------------------------------- //
private static final Type mapType = new TypeToken<Map<PS, TerritoryAccess>>(){}.getType();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public Board deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException public Board deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{ {
return new Board((Map<PS, TerritoryAccess>) context.deserialize(json, mapType)); return new Board((Map<PS, TerritoryAccess>) context.deserialize(json, Board.MAP_TYPE));
} }
@Override @Override
public JsonElement serialize(Board src, Type typeOfSrc, JsonSerializationContext context) public JsonElement serialize(Board src, Type typeOfSrc, JsonSerializationContext context)
{ {
return context.serialize(src.getMap(), mapType); return context.serialize(src.getMap(), Board.MAP_TYPE);
} }
} }

View File

@ -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<Map<PS, TerritoryAccess>>, JsonSerializer<Map<PS, TerritoryAccess>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static BoardMapAdapter i = new BoardMapAdapter();
public static BoardMapAdapter get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Map<PS, TerritoryAccess> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
Map<PS, TerritoryAccess> ret = new ConcurrentSkipListMap<PS, TerritoryAccess>();
JsonObject jsonObject = json.getAsJsonObject();
for (Entry<String, JsonElement> 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<PS, TerritoryAccess> src, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject ret = new JsonObject();
for (Entry<PS, TerritoryAccess> 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;
}
}