Pass 1 at reworking the Territory Access system.
This commit is contained in:
@ -1,10 +1,8 @@
|
||||
package com.massivecraft.factions.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Iterator;
|
||||
import java.util.logging.Level;
|
||||
import java.util.Set;
|
||||
|
||||
import com.massivecraft.mcore.xlib.gson.JsonArray;
|
||||
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
|
||||
import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
|
||||
import com.massivecraft.mcore.xlib.gson.JsonElement;
|
||||
@ -13,21 +11,23 @@ import com.massivecraft.mcore.xlib.gson.JsonParseException;
|
||||
import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
|
||||
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.Factions;
|
||||
import com.massivecraft.factions.TerritoryAccess;
|
||||
|
||||
public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>, JsonSerializer<TerritoryAccess>
|
||||
{
|
||||
//----------------------------------------------//
|
||||
// -------------------------------------------- //
|
||||
// CONSTANTS
|
||||
//----------------------------------------------//
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static final String ID = "ID";
|
||||
public static final String OPEN = "open";
|
||||
public static final String FACTIONS = "factions";
|
||||
public static final String FPLAYERS = "fplayers";
|
||||
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
|
||||
// -------------------------------------------- //
|
||||
@ -42,95 +42,76 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
|
||||
@Override
|
||||
public TerritoryAccess deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
try
|
||||
// isDefault <=> simple hostFactionId string
|
||||
if (json.isJsonPrimitive())
|
||||
{
|
||||
// if stored as simple string, it's just the faction ID and default values are to be used
|
||||
if (json.isJsonPrimitive())
|
||||
{
|
||||
String factionID = json.getAsString();
|
||||
return new TerritoryAccess(factionID);
|
||||
}
|
||||
|
||||
// otherwise, it's stored as an object and all data should be present
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
if (obj == null) return null;
|
||||
|
||||
String factionID = obj.get(ID).getAsString();
|
||||
boolean hostAllowed = obj.get(OPEN).getAsBoolean();
|
||||
JsonArray factions = obj.getAsJsonArray(FACTIONS);
|
||||
JsonArray fplayers = obj.getAsJsonArray(FPLAYERS);
|
||||
|
||||
TerritoryAccess access = new TerritoryAccess(factionID);
|
||||
access.setHostFactionAllowed(hostAllowed);
|
||||
|
||||
Iterator<JsonElement> iter = factions.iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
access.addFaction(iter.next().getAsString());
|
||||
}
|
||||
|
||||
iter = fplayers.iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
access.addFPlayer(iter.next().getAsString());
|
||||
}
|
||||
|
||||
return access;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
Factions.get().log(Level.WARNING, "Error encountered while deserializing TerritoryAccess data.");
|
||||
return null;
|
||||
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)
|
||||
{
|
||||
try
|
||||
if (src == null) return null;
|
||||
|
||||
// isDefault <=> simple hostFactionId string
|
||||
if (src.isDefault())
|
||||
{
|
||||
if (src == null) return null;
|
||||
|
||||
// if default values, store as simple string
|
||||
if (src.isDefault())
|
||||
{
|
||||
return new JsonPrimitive(src.getHostFactionId());
|
||||
}
|
||||
|
||||
// otherwise, store all data
|
||||
JsonObject obj = new JsonObject();
|
||||
|
||||
JsonArray factions = new JsonArray();
|
||||
JsonArray fplayers = new JsonArray();
|
||||
|
||||
Iterator<String> iter = src.getFactionIds().iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
factions.add(new JsonPrimitive(iter.next()));
|
||||
}
|
||||
|
||||
iter = src.getFPlayerIds().iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
fplayers.add(new JsonPrimitive(iter.next()));
|
||||
}
|
||||
|
||||
obj.addProperty(ID, src.getHostFactionId());
|
||||
obj.addProperty(OPEN, src.isHostFactionAllowed());
|
||||
obj.add(FACTIONS, factions);
|
||||
obj.add(FPLAYERS, fplayers);
|
||||
|
||||
return obj;
|
||||
|
||||
return new JsonPrimitive(src.getHostFactionId());
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
// Otherwise object
|
||||
JsonObject obj = new JsonObject();
|
||||
|
||||
obj.addProperty(HOST_FACTION_ID, src.getHostFactionId());
|
||||
|
||||
if (!src.isHostFactionAllowed())
|
||||
{
|
||||
ex.printStackTrace();
|
||||
Factions.get().log(Level.WARNING, "Error encountered while serializing TerritoryAccess data.");
|
||||
return null;
|
||||
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