Factions/src/com/massivecraft/factions/struct/TerritoryAccess.java

337 lines
8.4 KiB
Java
Raw Normal View History

New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
package com.massivecraft.factions.struct;
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 com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
import com.google.gson.JsonArray;
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.massivecraft.factions.FPlayers;
public class TerritoryAccess implements JsonDeserializer<TerritoryAccess>, JsonSerializer<TerritoryAccess>
{
private String hostFactionID;
private boolean hostFactionAllowed = true;
private Set<String> factionIDs = new LinkedHashSet<String>();
private Set<String> fplayerIDs = new LinkedHashSet<String>();
public TerritoryAccess(String factionID)
{
hostFactionID = factionID;
}
public TerritoryAccess() {}
public void setHostFactionID(String factionID)
{
hostFactionID = factionID;
hostFactionAllowed = true;
factionIDs.clear();
fplayerIDs.clear();
}
public String getHostFactionID()
{
return hostFactionID;
}
public Faction getHostFaction()
{
return Factions.i.get(hostFactionID);
}
// considered "default" if host faction is still allowed and nobody has been granted access
public boolean isDefault()
{
return this.hostFactionAllowed && factionIDs.isEmpty() && fplayerIDs.isEmpty();
}
public boolean isHostFactionAllowed()
{
return this.hostFactionAllowed;
}
public void setHostFactionAllowed(boolean allowed)
{
this.hostFactionAllowed = allowed;
}
public boolean doesHostFactionMatch(Object testSubject)
{
if (testSubject instanceof String)
return hostFactionID.equals((String)testSubject);
else if (testSubject instanceof Player)
return hostFactionID.equals(FPlayers.i.get((Player)testSubject).getFactionId());
else if (testSubject instanceof FPlayer)
return hostFactionID.equals(((FPlayer)testSubject).getFactionId());
else if (testSubject instanceof Faction)
return hostFactionID.equals(((Faction)testSubject).getId());
return false;
}
public void addFaction(String factionID)
{
factionIDs.add(factionID);
}
public void addFaction(Faction faction)
{
addFaction(faction.getId());
}
public void addFPlayer(String fplayerID)
{
fplayerIDs.add(fplayerID);
}
public void addFPlayer(FPlayer fplayer)
{
addFPlayer(fplayer.getId());
}
public void removeFaction(String factionID)
{
factionIDs.remove(factionID);
}
public void removeFaction(Faction faction)
{
removeFaction(faction.getId());
}
public void removeFPlayer(String fplayerID)
{
fplayerIDs.remove(fplayerID);
}
public void removeFPlayer(FPlayer fplayer)
{
removeFPlayer(fplayer.getId());
}
// return true if faction was added, false if it was removed
public boolean toggleFaction(String factionID)
{
// if the host faction, special handling
if (doesHostFactionMatch(factionID))
{
hostFactionAllowed ^= true;
return hostFactionAllowed;
}
if (factionIDs.contains(factionID))
{
removeFaction(factionID);
return false;
}
addFaction(factionID);
return true;
}
public boolean toggleFaction(Faction faction)
{
return toggleFaction(faction.getId());
}
public boolean toggleFPlayer(String fplayerID)
{
if (fplayerIDs.contains(fplayerID))
{
removeFPlayer(fplayerID);
return false;
}
addFPlayer(fplayerID);
return true;
}
public boolean toggleFPlayer(FPlayer fplayer)
{
return toggleFPlayer(fplayer.getId());
}
public String factionList()
{
StringBuilder list = new StringBuilder();
for (String factionID : factionIDs)
{
if (list.length() > 0)
list.append(", ");
list.append(Factions.i.get(factionID).getTag());
}
return list.toString();
}
public String fplayerList()
{
StringBuilder list = new StringBuilder();
for (String fplayerID : fplayerIDs)
{
if (list.length() > 0)
list.append(", ");
list.append(fplayerID);
}
return list.toString();
}
// these return false if not granted explicit access, or true if granted explicit access (in FPlayer or Faction lists)
// they do not take into account hostFactionAllowed, which will need to be checked separately (as to not override FPerms which are denied for faction members and such)
public boolean subjectHasAccess(Object testSubject)
{
if (testSubject instanceof Player)
return fPlayerHasAccess(FPlayers.i.get((Player)testSubject));
else if (testSubject instanceof FPlayer)
return fPlayerHasAccess((FPlayer)testSubject);
else if (testSubject instanceof Faction)
return factionHasAccess((Faction)testSubject);
return false;
}
public boolean fPlayerHasAccess(FPlayer fplayer)
{
if (factionHasAccess(fplayer.getFactionId())) return true;
return fplayerIDs.contains(fplayer.getId());
}
public boolean factionHasAccess(Faction faction)
{
return factionHasAccess(faction.getId());
}
public boolean factionHasAccess(String factionID)
{
return factionIDs.contains(factionID);
}
//----------------------------------------------//
// 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<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();
P.p.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();
P.p.log(Level.WARNING, "Error encountered while serializing TerritoryAccess data.");
return null;
}
}
//----------------------------------------------//
// Comparison
//----------------------------------------------//
@Override
public int hashCode()
{
return this.hostFactionID.hashCode();
}
@Override
public boolean equals(Object obj)
{
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;
}
}