Maven Attempt 1
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
import com.massivecraft.massivecore.ps.PS;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.massivecraft.factions.TerritoryAccess;
|
||||
import com.massivecraft.factions.entity.Board;
|
||||
|
||||
public class BoardAdapter implements JsonDeserializer<Board>, JsonSerializer<Board>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static BoardAdapter i = new BoardAdapter();
|
||||
public static BoardAdapter get() { return i; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Board deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return new Board((Map<PS, TerritoryAccess>) context.deserialize(json, Board.MAP_TYPE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Board src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return context.serialize(src.getMap(), Board.MAP_TYPE);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
|
||||
import com.massivecraft.massivecore.ps.PS;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.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 chunk = PS.valueOf(chunkX, chunkZ);
|
||||
|
||||
TerritoryAccess territoryAccess = context.deserialize(entry.getValue(), TerritoryAccess.class);
|
||||
|
||||
ret.put(chunk, 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;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.massivecraft.factions.FFlag;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
public class FFlagAdapter implements JsonDeserializer<FFlag>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static FFlagAdapter i = new FFlagAdapter();
|
||||
public static FFlagAdapter get() { return i; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public FFlag deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return FFlag.parse(json.getAsString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.massivecraft.factions.FPerm;
|
||||
|
||||
public class FPermAdapter implements JsonDeserializer<FPerm>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static FPermAdapter i = new FPermAdapter();
|
||||
public static FPermAdapter get() { return i; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public FPerm deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return FPerm.parse(json.getAsString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.massivecraft.factions.Factions;
|
||||
import com.massivecraft.factions.entity.Faction;
|
||||
|
||||
public class FactionPreprocessAdapter implements JsonDeserializer<Faction>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static FactionPreprocessAdapter i = new FactionPreprocessAdapter();
|
||||
public static FactionPreprocessAdapter get() { return i; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public Faction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
preprocess(json);
|
||||
return Factions.get().gsonWithoutPreprocessors.fromJson(json, typeOfT);
|
||||
}
|
||||
|
||||
public void preprocess(JsonElement json)
|
||||
{
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
|
||||
// Renamed fields
|
||||
// 1.8.X --> 2.0.0
|
||||
rename(jsonObject, "tag", "name");
|
||||
rename(jsonObject, "invites", "invitedPlayerIds");
|
||||
rename(jsonObject, "relationWish", "relationWishes");
|
||||
rename(jsonObject, "flagOverrides", "flags");
|
||||
rename(jsonObject, "permOverrides", "perms");
|
||||
}
|
||||
|
||||
public void rename(final JsonObject jsonObject, final String from, final String to)
|
||||
{
|
||||
JsonElement element = jsonObject.remove(from);
|
||||
if (element != null) jsonObject.add(to, element);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.massivecraft.factions.Rel;
|
||||
|
||||
public class RelAdapter implements JsonDeserializer<Rel>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static RelAdapter i = new RelAdapter();
|
||||
public static RelAdapter get() { return i; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public Rel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return Rel.parse(json.getAsString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,116 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.massivecraft.factions.TerritoryAccess;
|
||||
|
||||
public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>, JsonSerializer<TerritoryAccess>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// CONSTANTS
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static final String HOST_FACTION_ID = "hostFactionId";
|
||||
public static final String HOST_FACTION_ALLOWED = "hostFactionAllowed";
|
||||
public static final String FACTION_IDS = "factionIds";
|
||||
public static final String PLAYER_IDS = "playerIds";
|
||||
|
||||
public static final Type SET_OF_STRING_TYPE = new TypeToken<Set<String>>(){}.getType();
|
||||
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static TerritoryAccessAdapter i = new TerritoryAccessAdapter();
|
||||
public static TerritoryAccessAdapter get() { return i; }
|
||||
|
||||
//----------------------------------------------//
|
||||
// OVERRIDE
|
||||
//----------------------------------------------//
|
||||
|
||||
@Override
|
||||
public TerritoryAccess deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
// isDefault <=> simple hostFactionId string
|
||||
if (json.isJsonPrimitive())
|
||||
{
|
||||
String hostFactionId = json.getAsString();
|
||||
return TerritoryAccess.valueOf(hostFactionId);
|
||||
}
|
||||
|
||||
// Otherwise object
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
|
||||
// Prepare variables
|
||||
String hostFactionId = null;
|
||||
Boolean hostFactionAllowed = null;
|
||||
Set<String> factionIds = null;
|
||||
Set<String> playerIds = null;
|
||||
|
||||
// Read variables (test old values first)
|
||||
JsonElement element = null;
|
||||
|
||||
element = obj.get("ID");
|
||||
if (element == null) element = obj.get(HOST_FACTION_ID);
|
||||
hostFactionId = element.getAsString();
|
||||
|
||||
element = obj.get("open");
|
||||
if (element == null) element = obj.get(HOST_FACTION_ALLOWED);
|
||||
if (element != null) hostFactionAllowed = element.getAsBoolean();
|
||||
|
||||
element = obj.get("factions");
|
||||
if (element == null) element = obj.get(FACTION_IDS);
|
||||
if (element != null) factionIds = context.deserialize(element, SET_OF_STRING_TYPE);
|
||||
|
||||
element = obj.get("fplayers");
|
||||
if (element == null) element = obj.get(PLAYER_IDS);
|
||||
if (element != null) playerIds = context.deserialize(element, SET_OF_STRING_TYPE);
|
||||
|
||||
return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(TerritoryAccess src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
if (src == null) return null;
|
||||
|
||||
// isDefault <=> simple hostFactionId string
|
||||
if (src.isDefault())
|
||||
{
|
||||
return new JsonPrimitive(src.getHostFactionId());
|
||||
}
|
||||
|
||||
// Otherwise object
|
||||
JsonObject obj = new JsonObject();
|
||||
|
||||
obj.addProperty(HOST_FACTION_ID, src.getHostFactionId());
|
||||
|
||||
if (!src.isHostFactionAllowed())
|
||||
{
|
||||
obj.addProperty(HOST_FACTION_ALLOWED, src.isHostFactionAllowed());
|
||||
}
|
||||
|
||||
if (!src.getFactionIds().isEmpty())
|
||||
{
|
||||
obj.add(FACTION_IDS, context.serialize(src.getFactionIds(), SET_OF_STRING_TYPE));
|
||||
}
|
||||
|
||||
if (!src.getPlayerIds().isEmpty())
|
||||
{
|
||||
obj.add(PLAYER_IDS, context.serialize(src.getPlayerIds(), SET_OF_STRING_TYPE));
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user