diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 53ebcc83..ae504d4c 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -157,7 +157,7 @@ public class Factions extends MPlugin .disableHtmlEscaping() .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) .registerTypeAdapter(LazyLocation.class, new LocationTypeAdapter()) - .registerTypeAdapter(TerritoryAccess.class, new TerritoryAccess()) + .registerTypeAdapter(TerritoryAccess.class, TerritoryAccessAdapter.get()) .registerTypeAdapter(Rel.class, new RelTypeAdapter()) .registerTypeAdapter(FPerm.class, new FPermTypeAdapter()) .registerTypeAdapter(FFlag.class, new FFlagTypeAdapter()); diff --git a/src/com/massivecraft/factions/TerritoryAccess.java b/src/com/massivecraft/factions/TerritoryAccess.java index 8c0ca71b..5c8ae62e 100644 --- a/src/com/massivecraft/factions/TerritoryAccess.java +++ b/src/com/massivecraft/factions/TerritoryAccess.java @@ -1,32 +1,16 @@ package com.massivecraft.factions; -import java.lang.reflect.Type; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; -import java.util.logging.Level; import org.bukkit.entity.Player; - -import org.bukkit.craftbukkit.libs.com.google.gson.JsonArray; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonPrimitive; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext; -import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer; - - -public class TerritoryAccess implements JsonDeserializer, JsonSerializer +public class TerritoryAccess { - private String hostFactionID; - private boolean hostFactionAllowed = true; - private Set factionIDs = new LinkedHashSet(); - private Set fplayerIDs = new LinkedHashSet(); - + protected String hostFactionID; + protected boolean hostFactionAllowed = true; + protected Set factionIDs = new LinkedHashSet(); + protected Set fplayerIDs = new LinkedHashSet(); public TerritoryAccess(String factionID) { @@ -210,112 +194,8 @@ public class TerritoryAccess implements JsonDeserializer, JsonS return ( ! this.isHostFactionAllowed() && this.doesHostFactionMatch(testSubject) && ! FPerm.ACCESS.has(testSubject, this.getHostFaction())); } - //----------------------------------------------// - // JSON Serialize/Deserialize Type Adapters - //----------------------------------------------// - - @Override - public TerritoryAccess deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - try - { - // 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 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; - } - } - - @Override - public JsonElement serialize(TerritoryAccess src, Type typeOfSrc, JsonSerializationContext context) - { - try - { - if (src == null) return null; - - // if default values, store as simple string - if (src.isDefault()) - { - // if Wilderness (faction "0") and default access values, no need to store it - if (src.getHostFactionID().equals("0")) - return null; - - return new JsonPrimitive(src.getHostFactionID()); - } - - // otherwise, store all data - JsonObject obj = new JsonObject(); - - JsonArray factions = new JsonArray(); - JsonArray fplayers = new JsonArray(); - - Iterator iter = src.factionIDs.iterator(); - while (iter.hasNext()) - { - factions.add(new JsonPrimitive(iter.next())); - } - - iter = src.fplayerIDs.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; - - } - catch (Exception ex) - { - ex.printStackTrace(); - Factions.get().log(Level.WARNING, "Error encountered while serializing TerritoryAccess data."); - return null; - } - } - - - //----------------------------------------------// - // Comparison + // COMPARISON //----------------------------------------------// @Override @@ -327,10 +207,9 @@ public class TerritoryAccess implements JsonDeserializer, JsonS @Override public boolean equals(Object obj) { - if (obj == this) - return true; - if (!(obj instanceof TerritoryAccess)) - return false; + if (obj == this) return true; + + if (!(obj instanceof TerritoryAccess)) return false; TerritoryAccess that = (TerritoryAccess) obj; return this.hostFactionID.equals(that.hostFactionID) && this.hostFactionAllowed == that.hostFactionAllowed && this.factionIDs == that.factionIDs && this.fplayerIDs == that.fplayerIDs; diff --git a/src/com/massivecraft/factions/TerritoryAccessAdapter.java b/src/com/massivecraft/factions/TerritoryAccessAdapter.java new file mode 100644 index 00000000..c424106a --- /dev/null +++ b/src/com/massivecraft/factions/TerritoryAccessAdapter.java @@ -0,0 +1,137 @@ +package com.massivecraft.factions; + +import java.lang.reflect.Type; +import java.util.Iterator; +import java.util.logging.Level; + +import org.bukkit.craftbukkit.libs.com.google.gson.JsonArray; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonPrimitive; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer; + +public class TerritoryAccessAdapter implements JsonDeserializer, JsonSerializer +{ + //----------------------------------------------// + // 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"; + + // -------------------------------------------- // + // 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 + { + try + { + // 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 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; + } + } + + @Override + public JsonElement serialize(TerritoryAccess src, Type typeOfSrc, JsonSerializationContext context) + { + try + { + if (src == null) return null; + + // if default values, store as simple string + if (src.isDefault()) + { + // if Wilderness (faction "0") and default access values, no need to store it + if (src.getHostFactionID().equals("0")) + return null; + + return new JsonPrimitive(src.getHostFactionID()); + } + + // otherwise, store all data + JsonObject obj = new JsonObject(); + + JsonArray factions = new JsonArray(); + JsonArray fplayers = new JsonArray(); + + Iterator iter = src.factionIDs.iterator(); + while (iter.hasNext()) + { + factions.add(new JsonPrimitive(iter.next())); + } + + iter = src.fplayerIDs.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; + + } + catch (Exception ex) + { + ex.printStackTrace(); + Factions.get().log(Level.WARNING, "Error encountered while serializing TerritoryAccess data."); + return null; + } + } + +} \ No newline at end of file