From 00774087d373387b541d77fa0cf4627dd060a9bc Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 9 Apr 2013 14:05:00 +0200 Subject: [PATCH] Some minor cleanup to TerritoryAccess source code. --- src/com/massivecraft/factions/Board.java | 12 +- src/com/massivecraft/factions/FLocation.java | 1 - .../factions/TerritoryAccess.java | 190 +++++++++--------- .../factions/TerritoryAccessAdapter.java | 10 +- 4 files changed, 102 insertions(+), 111 deletions(-) diff --git a/src/com/massivecraft/factions/Board.java b/src/com/massivecraft/factions/Board.java index 5c759abd..07cf7764 100644 --- a/src/com/massivecraft/factions/Board.java +++ b/src/com/massivecraft/factions/Board.java @@ -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 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> iter = flocationIds.entrySet().iterator(); while (iter.hasNext()) { Entry 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> iter = flocationIds.entrySet().iterator(); while (iter.hasNext()) { Entry 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; } diff --git a/src/com/massivecraft/factions/FLocation.java b/src/com/massivecraft/factions/FLocation.java index 7e419c36..b1c4bb26 100644 --- a/src/com/massivecraft/factions/FLocation.java +++ b/src/com/massivecraft/factions/FLocation.java @@ -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; diff --git a/src/com/massivecraft/factions/TerritoryAccess.java b/src/com/massivecraft/factions/TerritoryAccess.java index 5c8ae62e..4e5c95f5 100644 --- a/src/com/massivecraft/factions/TerritoryAccess.java +++ b/src/com/massivecraft/factions/TerritoryAccess.java @@ -7,125 +7,91 @@ import org.bukkit.entity.Player; public class TerritoryAccess { - protected String hostFactionID; - protected boolean hostFactionAllowed = true; - protected Set factionIDs = new LinkedHashSet(); - protected Set fplayerIDs = new LinkedHashSet(); + // -------------------------------------------- // + // 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 factionIds = new LinkedHashSet(); + public Set getFactionIds() { return this.factionIds; } + + private Set fplayerIds = new LinkedHashSet(); + public Set 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; } } \ No newline at end of file diff --git a/src/com/massivecraft/factions/TerritoryAccessAdapter.java b/src/com/massivecraft/factions/TerritoryAccessAdapter.java index c424106a..5bcbec3f 100644 --- a/src/com/massivecraft/factions/TerritoryAccessAdapter.java +++ b/src/com/massivecraft/factions/TerritoryAccessAdapter.java @@ -94,10 +94,10 @@ public class TerritoryAccessAdapter implements JsonDeserializer 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 JsonArray factions = new JsonArray(); JsonArray fplayers = new JsonArray(); - Iterator iter = src.factionIDs.iterator(); + Iterator 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);