2013-04-22 09:37:53 +02:00
|
|
|
package com.massivecraft.factions.adapter;
|
2013-04-11 10:11:14 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.concurrent.ConcurrentSkipListMap;
|
|
|
|
|
2014-06-04 14:02:23 +02:00
|
|
|
import com.massivecraft.massivecore.ps.PS;
|
2014-09-13 10:24:49 +02:00
|
|
|
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
|
|
|
|
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
|
|
|
|
import com.massivecraft.massivecore.xlib.gson.JsonElement;
|
|
|
|
import com.massivecraft.massivecore.xlib.gson.JsonObject;
|
|
|
|
import com.massivecraft.massivecore.xlib.gson.JsonParseException;
|
|
|
|
import com.massivecraft.massivecore.xlib.gson.JsonSerializationContext;
|
|
|
|
import com.massivecraft.massivecore.xlib.gson.JsonSerializer;
|
2013-04-11 10:11:14 +02:00
|
|
|
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]);
|
2013-04-11 11:27:04 +02:00
|
|
|
PS chunk = PS.valueOf(chunkX, chunkZ);
|
2013-04-11 10:11:14 +02:00
|
|
|
|
|
|
|
TerritoryAccess territoryAccess = context.deserialize(entry.getValue(), TerritoryAccess.class);
|
|
|
|
|
2013-04-11 11:27:04 +02:00
|
|
|
ret.put(chunk, territoryAccess);
|
2013-04-11 10:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|