Some minor cleanup to TerritoryAccess source code.

This commit is contained in:
Olof Larsson 2013-04-09 14:05:00 +02:00
parent a891fc8a0a
commit 00774087d3
4 changed files with 102 additions and 111 deletions

View File

@ -32,7 +32,7 @@ public class Board
{
if ( ! flocationIds.containsKey(flocation)) return "0";
return flocationIds.get(flocation).getHostFactionID();
return flocationIds.get(flocation).getHostFactionId();
}
public static TerritoryAccess getTerritoryAccessAt(FLocation flocation)
@ -84,7 +84,7 @@ public class Board
while (iter.hasNext())
{
Entry<FLocation, TerritoryAccess> entry = iter.next();
if (entry.getValue().getHostFactionID().equals(factionId))
if (entry.getValue().getHostFactionId().equals(factionId))
{
if(ConfServer.onUnclaimResetLwcLocks && LWCFeatures.getEnabled())
LWCFeatures.clearAllChests(entry.getKey());
@ -126,12 +126,12 @@ public class Board
Iterator<Entry<FLocation, TerritoryAccess>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<FLocation, TerritoryAccess> entry = iter.next();
if ( ! FactionColl.i.exists(entry.getValue().getHostFactionID()))
if ( ! FactionColl.i.exists(entry.getValue().getHostFactionId()))
{
if(ConfServer.onUnclaimResetLwcLocks && LWCFeatures.getEnabled())
LWCFeatures.clearAllChests(entry.getKey());
Factions.get().log("Board cleaner removed "+entry.getValue().getHostFactionID()+" from "+entry.getKey());
Factions.get().log("Board cleaner removed "+entry.getValue().getHostFactionId()+" from "+entry.getKey());
iter.remove();
}
}
@ -146,7 +146,7 @@ public class Board
int ret = 0;
for (TerritoryAccess thatFactionId : flocationIds.values())
{
if(thatFactionId.getHostFactionID().equals(factionId))
if(thatFactionId.getHostFactionId().equals(factionId))
{
ret += 1;
}
@ -166,7 +166,7 @@ public class Board
Iterator<Entry<FLocation, TerritoryAccess>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<FLocation, TerritoryAccess> entry = iter.next();
if (entry.getValue().getHostFactionID().equals(factionId) && entry.getKey().getWorldName().equals(worldName))
if (entry.getValue().getHostFactionId().equals(factionId) && entry.getKey().getWorldName().equals(worldName))
{
ret += 1;
}

View File

@ -14,7 +14,6 @@ import com.massivecraft.factions.util.MiscUtil;
public class FLocation
{
private String worldName = "world";
private int x = 0;
private int z = 0;

View File

@ -7,125 +7,91 @@ import org.bukkit.entity.Player;
public class TerritoryAccess
{
protected String hostFactionID;
protected boolean hostFactionAllowed = true;
protected Set<String> factionIDs = new LinkedHashSet<String>();
protected Set<String> fplayerIDs = new LinkedHashSet<String>();
// -------------------------------------------- //
// FIELDS: RAW
// -------------------------------------------- //
private String hostFactionId;
public String getHostFactionId()
{
return this.hostFactionId;
}
public Faction getHostFaction() { return FactionColl.i.get(this.hostFactionId); }
public void setHostFactionId(String factionId)
{
// TODO: Why all these here???
this.hostFactionId = factionId;
this.hostFactionAllowed = true;
this.factionIds.clear();
this.fplayerIds.clear();
}
private boolean hostFactionAllowed = true;
public boolean isHostFactionAllowed() { return this.hostFactionAllowed; }
public void setHostFactionAllowed(boolean allowed) { this.hostFactionAllowed = allowed; }
private Set<String> factionIds = new LinkedHashSet<String>();
public Set<String> getFactionIds() { return this.factionIds; }
private Set<String> fplayerIds = new LinkedHashSet<String>();
public Set<String> getFPlayerIds() { return this.fplayerIds; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public TerritoryAccess(String factionID)
{
hostFactionID = 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 FactionColl.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(FPlayerColl.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());
}
public TerritoryAccess()
{
}
// -------------------------------------------- //
// FIELDS: UTILS
// -------------------------------------------- //
public void addFaction(String factionId) { this.getFactionIds().add(factionId); }
public void addFaction(Faction faction) { this.addFaction(faction.getId()); }
public void removeFaction(String factionID) { this.getFactionIds().remove(factionID); }
public void removeFaction(Faction faction) { this.removeFaction(faction.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;
this.hostFactionAllowed ^= true;
return this.hostFactionAllowed;
}
if (factionIDs.contains(factionID))
if (this.getFactionIds().contains(factionID))
{
removeFaction(factionID);
return false;
}
addFaction(factionID);
this.addFaction(factionID);
return true;
}
public boolean toggleFaction(Faction faction)
{
return toggleFaction(faction.getId());
}
public void addFPlayer(String fplayerID) { this.getFPlayerIds().add(fplayerID); }
public void addFPlayer(FPlayer fplayer) { this.addFPlayer(fplayer.getId()); }
public void removeFPlayer(String fplayerID) { this.getFPlayerIds().remove(fplayerID); }
public void removeFPlayer(FPlayer fplayer) { this.removeFPlayer(fplayer.getId()); }
public boolean toggleFPlayer(String fplayerID)
{
if (fplayerIDs.contains(fplayerID))
if (this.getFPlayerIds().contains(fplayerID))
{
removeFPlayer(fplayerID);
return false;
@ -137,11 +103,37 @@ public class TerritoryAccess
{
return toggleFPlayer(fplayer.getId());
}
public boolean doesHostFactionMatch(Object testSubject)
{
if (testSubject instanceof String)
return hostFactionId.equals((String)testSubject);
else if (testSubject instanceof Player)
return hostFactionId.equals(FPlayerColl.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;
}
// -------------------------------------------- //
// UTILS
// -------------------------------------------- //
// 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 String factionList()
{
StringBuilder list = new StringBuilder();
for (String factionID : factionIDs)
for (String factionID : factionIds)
{
if (list.length() > 0)
list.append(", ");
@ -153,7 +145,7 @@ public class TerritoryAccess
public String fplayerList()
{
StringBuilder list = new StringBuilder();
for (String fplayerID : fplayerIDs)
for (String fplayerID : fplayerIds)
{
if (list.length() > 0)
list.append(", ");
@ -177,7 +169,7 @@ public class TerritoryAccess
public boolean fPlayerHasAccess(FPlayer fplayer)
{
if (factionHasAccess(fplayer.getFactionId())) return true;
return fplayerIDs.contains(fplayer.getId());
return fplayerIds.contains(fplayer.getId());
}
public boolean factionHasAccess(Faction faction)
{
@ -185,7 +177,7 @@ public class TerritoryAccess
}
public boolean factionHasAccess(String factionID)
{
return factionIDs.contains(factionID);
return factionIds.contains(factionID);
}
// this should normally only be checked after running subjectHasAccess() or fPlayerHasAccess() above to see if they have access explicitly granted
@ -201,7 +193,7 @@ public class TerritoryAccess
@Override
public int hashCode()
{
return this.hostFactionID.hashCode();
return this.hostFactionId.hashCode();
}
@Override
@ -212,6 +204,6 @@ public class TerritoryAccess
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;
return this.hostFactionId.equals(that.hostFactionId) && this.hostFactionAllowed == that.hostFactionAllowed && this.factionIds == that.factionIds && this.fplayerIds == that.fplayerIds;
}
}

View File

@ -94,10 +94,10 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
if (src.isDefault())
{
// if Wilderness (faction "0") and default access values, no need to store it
if (src.getHostFactionID().equals("0"))
if (src.getHostFactionId().equals("0"))
return null;
return new JsonPrimitive(src.getHostFactionID());
return new JsonPrimitive(src.getHostFactionId());
}
// otherwise, store all data
@ -106,19 +106,19 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
JsonArray factions = new JsonArray();
JsonArray fplayers = new JsonArray();
Iterator<String> iter = src.factionIDs.iterator();
Iterator<String> iter = src.getFactionIds().iterator();
while (iter.hasNext())
{
factions.add(new JsonPrimitive(iter.next()));
}
iter = src.fplayerIDs.iterator();
iter = src.getFPlayerIds().iterator();
while (iter.hasNext())
{
fplayers.add(new JsonPrimitive(iter.next()));
}
obj.addProperty(ID, src.getHostFactionID());
obj.addProperty(ID, src.getHostFactionId());
obj.addProperty(OPEN, src.isHostFactionAllowed());
obj.add(FACTIONS, factions);
obj.add(FPLAYERS, fplayers);