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

@ -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;
}
}