Decouple TerritoryAccess and it's adapter.
This commit is contained in:
parent
936c28d5a9
commit
a891fc8a0a
@ -157,7 +157,7 @@ public class Factions extends MPlugin
|
|||||||
.disableHtmlEscaping()
|
.disableHtmlEscaping()
|
||||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE)
|
.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE)
|
||||||
.registerTypeAdapter(LazyLocation.class, new LocationTypeAdapter())
|
.registerTypeAdapter(LazyLocation.class, new LocationTypeAdapter())
|
||||||
.registerTypeAdapter(TerritoryAccess.class, new TerritoryAccess())
|
.registerTypeAdapter(TerritoryAccess.class, TerritoryAccessAdapter.get())
|
||||||
.registerTypeAdapter(Rel.class, new RelTypeAdapter())
|
.registerTypeAdapter(Rel.class, new RelTypeAdapter())
|
||||||
.registerTypeAdapter(FPerm.class, new FPermTypeAdapter())
|
.registerTypeAdapter(FPerm.class, new FPermTypeAdapter())
|
||||||
.registerTypeAdapter(FFlag.class, new FFlagTypeAdapter());
|
.registerTypeAdapter(FFlag.class, new FFlagTypeAdapter());
|
||||||
|
@ -1,32 +1,16 @@
|
|||||||
package com.massivecraft.factions;
|
package com.massivecraft.factions;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
public class TerritoryAccess
|
||||||
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<TerritoryAccess>, JsonSerializer<TerritoryAccess>
|
|
||||||
{
|
{
|
||||||
private String hostFactionID;
|
protected String hostFactionID;
|
||||||
private boolean hostFactionAllowed = true;
|
protected boolean hostFactionAllowed = true;
|
||||||
private Set<String> factionIDs = new LinkedHashSet<String>();
|
protected Set<String> factionIDs = new LinkedHashSet<String>();
|
||||||
private Set<String> fplayerIDs = new LinkedHashSet<String>();
|
protected Set<String> fplayerIDs = new LinkedHashSet<String>();
|
||||||
|
|
||||||
|
|
||||||
public TerritoryAccess(String factionID)
|
public TerritoryAccess(String factionID)
|
||||||
{
|
{
|
||||||
@ -210,112 +194,8 @@ public class TerritoryAccess implements JsonDeserializer<TerritoryAccess>, JsonS
|
|||||||
return ( ! this.isHostFactionAllowed() && this.doesHostFactionMatch(testSubject) && ! FPerm.ACCESS.has(testSubject, this.getHostFaction()));
|
return ( ! this.isHostFactionAllowed() && this.doesHostFactionMatch(testSubject) && ! FPerm.ACCESS.has(testSubject, this.getHostFaction()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------//
|
//----------------------------------------------//
|
||||||
// JSON Serialize/Deserialize Type Adapters
|
// COMPARISON
|
||||||
//----------------------------------------------//
|
|
||||||
|
|
||||||
@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<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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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<String> 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
|
|
||||||
//----------------------------------------------//
|
//----------------------------------------------//
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -327,10 +207,9 @@ public class TerritoryAccess implements JsonDeserializer<TerritoryAccess>, JsonS
|
|||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj)
|
public boolean equals(Object obj)
|
||||||
{
|
{
|
||||||
if (obj == this)
|
if (obj == this) return true;
|
||||||
return true;
|
|
||||||
if (!(obj instanceof TerritoryAccess))
|
if (!(obj instanceof TerritoryAccess)) return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
TerritoryAccess that = (TerritoryAccess) obj;
|
TerritoryAccess that = (TerritoryAccess) obj;
|
||||||
return this.hostFactionID.equals(that.hostFactionID) && this.hostFactionAllowed == that.hostFactionAllowed && this.factionIDs == that.factionIDs && this.fplayerIDs == that.fplayerIDs;
|
return this.hostFactionID.equals(that.hostFactionID) && this.hostFactionAllowed == that.hostFactionAllowed && this.factionIDs == that.factionIDs && this.fplayerIDs == that.fplayerIDs;
|
||||||
|
137
src/com/massivecraft/factions/TerritoryAccessAdapter.java
Normal file
137
src/com/massivecraft/factions/TerritoryAccessAdapter.java
Normal file
@ -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<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";
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// 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<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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user