From 66b4eb4b92af6c66120c8c225a8fd3d02a6af688 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 29 Apr 2013 10:23:39 +0200 Subject: [PATCH] Pass 1 at reworking the Territory Access system. --- plugin.yml | 42 ++- src/com/massivecraft/factions/Perm.java | 4 +- .../factions/TerritoryAccess.java | 316 +++++++++++------- .../adapter/TerritoryAccessAdapter.java | 157 ++++----- .../factions/cmd/CmdFactionsAccess.java | 103 +----- .../cmd/CmdFactionsAccessAbstract.java | 61 ++++ .../cmd/CmdFactionsAccessFaction.java | 43 +++ .../factions/cmd/CmdFactionsAccessPlayer.java | 43 +++ .../factions/cmd/CmdFactionsAccessView.java | 21 ++ .../factions/cmd/CmdFactionsMoney.java | 8 +- .../factions/cmd/CmdFactionsMoneyBalance.java | 2 + .../factions/cmd/arg/ARFaction.java | 2 +- .../massivecraft/factions/entity/Board.java | 8 +- 13 files changed, 478 insertions(+), 332 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java diff --git a/plugin.yml b/plugin.yml index 8828e93a..369eaba2 100644 --- a/plugin.yml +++ b/plugin.yml @@ -10,9 +10,10 @@ permissions: # -------------------------------------------- # # THE REAL NODES # -------------------------------------------- # - factions.access: {description: grant territory access, with the proper fperm, default: false} - factions.access.any: {description: grant territory access for another faction, default: false} - factions.access.view: {description: view territory access, default: false} + factions.access: {description: manage access, with the proper fperm, default: false} + factions.access.faction: {description: grant faction access, with the proper fperm, default: false} + factions.access.player: {description: grant player access, with the proper fperm, default: false} + factions.access.view: {description: view access, default: false} factions.admin: {description: enable adminmode, default: false} factions.autoclaim: {description: autoclaim when walking, default: false} factions.claim: {description: claim land where you stand, default: false} @@ -35,6 +36,7 @@ permissions: factions.leave: {description: leave your faction, default: false} factions.list: {description: list all factions, default: false} factions.map: {description: show territory map, default: false} + factions.money: {description: manage faction money, default: false} factions.money.balance: {description: show faction money, default: false} factions.money.balance.any: {description: show another factions money, default: false} factions.money.deposit: {description: deposit to faction, default: false} @@ -64,7 +66,8 @@ permissions: factions.*: children: factions.access: true - factions.access.any: true + factions.access.faction: true + factions.access.player: true factions.access.view: true factions.admin: true factions.autoclaim: true @@ -88,7 +91,14 @@ permissions: factions.leave: true factions.list: true factions.map: true - factions.money.*: true + factions.money: true + factions.money.balance: true + factions.money.balance.any: true + factions.money.deposit: true + factions.money.f2f: true + factions.money.f2p: true + factions.money.p2f: true + factions.money.withdraw: true factions.officer: true factions.officer.any: true factions.open: true @@ -105,15 +115,6 @@ permissions: factions.unclaim: true factions.unclaimall: true factions.version: true - factions.money.*: - children: - factions.money.balance: true - factions.money.balance.any: true - factions.money.deposit: true - factions.money.f2f: true - factions.money.f2p: true - factions.money.p2f: true - factions.money.withdraw: true # -------------------------------------------- # # KITS # -------------------------------------------- # @@ -144,12 +145,14 @@ permissions: factions.kit.rank0: default: false children: - factions.create: true factions.access: true + factions.access.faction: true + factions.access.player: true factions.access.view: true factions.autoclaim: true factions.claim: true factions.claim.radius: true + factions.create: true factions.demote: true factions.description: true factions.disband: true @@ -163,7 +166,14 @@ permissions: factions.leave: true factions.list: true factions.map: true - factions.money.*: true + factions.money: true + factions.money.balance: true + factions.money.balance.any: true + factions.money.deposit: true + factions.money.f2f: true + factions.money.f2p: true + factions.money.p2f: true + factions.money.withdraw: true factions.officer: true factions.open: true factions.perm: true diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index 5a8e2d63..d665fca3 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -11,8 +11,9 @@ public enum Perm // -------------------------------------------- // ACCESS("access"), - ACCESS_ANY("access.any"), ACCESS_VIEW("access.view"), + ACCESS_PLAYER("access.player"), + ACCESS_FACTION("access.faction"), ADMIN("admin"), AUTOCLAIM("autoclaim"), CLAIM("claim"), @@ -35,6 +36,7 @@ public enum Perm LEAVE("leave"), LIST("list"), MAP("map"), + MONEY("money"), MONEY_BALANCE("money.balance"), MONEY_BALANCE_ANY("money.balance.any"), MONEY_DEPOSIT("money.deposit"), diff --git a/src/com/massivecraft/factions/TerritoryAccess.java b/src/com/massivecraft/factions/TerritoryAccess.java index 966761eb..684a69d4 100644 --- a/src/com/massivecraft/factions/TerritoryAccess.java +++ b/src/com/massivecraft/factions/TerritoryAccess.java @@ -1,14 +1,21 @@ package com.massivecraft.factions; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; +import java.util.TreeSet; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.UPlayer; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.FactionColls; +import com.massivecraft.factions.entity.UPlayerColl; +import com.massivecraft.factions.entity.UPlayerColls; public class TerritoryAccess { @@ -16,167 +23,236 @@ public class TerritoryAccess // FIELDS: RAW // -------------------------------------------- // - private String hostFactionId; + // no default value, can't be null + private final String hostFactionId; public String getHostFactionId() { return this.hostFactionId; } - public void setHostFactionId(String hostFactionId) { this.hostFactionId = hostFactionId; } - private boolean hostFactionAllowed = true; + // default is true + private final boolean hostFactionAllowed; public boolean isHostFactionAllowed() { return this.hostFactionAllowed; } - public void setHostFactionAllowed(boolean hostFactionAllowed) { this.hostFactionAllowed = hostFactionAllowed; } - private Set factionIds = new LinkedHashSet(); + // default is empty + private final Set factionIds; public Set getFactionIds() { return this.factionIds; } - private Set fplayerIds = new LinkedHashSet(); - public Set getFPlayerIds() { return this.fplayerIds; } - + // default is empty + private final Set playerIds; + public Set getPlayerIds() { return this.playerIds; } + // -------------------------------------------- // - // CONSTRUCT + // FIELDS: DELTA // -------------------------------------------- // - public TerritoryAccess(String hostFactionId) - { - this.hostFactionId = hostFactionId; - } - - public TerritoryAccess() + // The simple ones + public TerritoryAccess withHostFactionId(String hostFactionId) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } + public TerritoryAccess withHostFactionAllowed(Boolean hostFactionAllowed) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } + public TerritoryAccess withFactionIds(Collection factionIds) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } + public TerritoryAccess withPlayerIds(Collection playerIds) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } + + // The intermediate ones + public TerritoryAccess withFactionId(String factionId, boolean with) { + if (this.getHostFactionId().equals(factionId)) + { + return valueOf(hostFactionId, with, factionIds, playerIds); + } + Set factionIds = new HashSet(this.getFactionIds()); + if (with) + { + factionIds.add(factionId); + } + else + { + factionIds.remove(factionId); + } + return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); + } + + public TerritoryAccess withPlayerId(String playerId, boolean with) + { + playerId = playerId.toLowerCase(); + Set playerIds = new HashSet(this.getPlayerIds()); + if (with) + { + playerIds.add(playerId); + } + else + { + playerIds.remove(playerId); + } + return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); + } + + // The complex ones + public TerritoryAccess toggleFactionId(String factionId) + { + return this.withFactionId(factionId, !this.isFactionIdGranted(factionId)); + } + + public TerritoryAccess togglePlayerId(String playerId) + { + return this.withPlayerId(playerId, !this.isPlayerIdGranted(playerId)); } // -------------------------------------------- // - // FIELDS: UTILS + // FIELDS: UNIVERSED // -------------------------------------------- // + + public Faction getHostFaction(Object universe) + { + return FactionColls.get().get(universe).get(this.getHostFactionId()); + } + + public LinkedHashSet getGrantedUPlayers(Object universe) + { + LinkedHashSet ret = new LinkedHashSet(); + UPlayerColl coll = UPlayerColls.get().get(universe); + for (String playerId : this.getPlayerIds()) + { + ret.add(coll.get(playerId)); + } + return ret; + } + + public LinkedHashSet getGrantedFactions(Object universe) + { + LinkedHashSet ret = new LinkedHashSet(); + FactionColl coll = FactionColls.get().get(universe); + for (String factionId : this.getFactionIds()) + { + ret.add(coll.get(factionId)); + } + return ret; + } - 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()); } + // -------------------------------------------- // + // PRIVATE CONSTRUCTOR + // -------------------------------------------- // - // return true if faction was added, false if it was removed - public boolean toggleFaction(String factionId) + private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection factionIds, Collection playerIds) { - // if the host faction, special handling - if (this.doesHostFactionMatch(factionId)) + if (hostFactionId == null) throw new IllegalArgumentException("hostFactionId was null"); + this.hostFactionId = hostFactionId; + + Set factionIdsInner = new TreeSet(); + if (factionIds != null) { - this.hostFactionAllowed ^= true; - return this.hostFactionAllowed; + factionIdsInner.addAll(factionIds); + if (factionIdsInner.remove(hostFactionId)) + { + hostFactionAllowed = true; + } } - - if (this.getFactionIds().contains(factionId)) + this.factionIds = Collections.unmodifiableSet(factionIdsInner); + + Set playerIdsInner = new TreeSet(String.CASE_INSENSITIVE_ORDER); + if (playerIds != null) { - this.removeFaction(factionId); - return false; + for (String playerId : playerIds) + { + playerIdsInner.add(playerId.toLowerCase()); + } } - this.addFaction(factionId); - return true; - } - public boolean toggleFaction(Faction faction) - { - return this.toggleFaction(faction.getId()); - } - - - public void addFPlayer(String fplayerID) { this.getFPlayerIds().add(fplayerID); } - public void addFPlayer(UPlayer fplayer) { this.addFPlayer(fplayer.getId()); } - public void removeFPlayer(String fplayerID) { this.getFPlayerIds().remove(fplayerID); } - public void removeFPlayer(UPlayer fplayer) { this.removeFPlayer(fplayer.getId()); } - - public boolean toggleFPlayer(String fplayerID) - { - if (this.getFPlayerIds().contains(fplayerID)) - { - this.removeFPlayer(fplayerID); - return false; - } - this.addFPlayer(fplayerID); - return true; - } - public boolean toggleFPlayer(UPlayer fplayer) - { - return this.toggleFPlayer(fplayer.getId()); - } - - - public boolean doesHostFactionMatch(Object testSubject) - { - if (testSubject instanceof String) - return hostFactionId.equals((String)testSubject); - else if (testSubject instanceof CommandSender) - return hostFactionId.equals(UPlayer.get(testSubject).getFactionId()); - else if (testSubject instanceof UPlayer) - return hostFactionId.equals(((UPlayer)testSubject).getFactionId()); - else if (testSubject instanceof Faction) - return hostFactionId.equals(((Faction)testSubject).getId()); - return false; + this.playerIds = Collections.unmodifiableSet(playerIdsInner); + + this.hostFactionAllowed = (hostFactionAllowed == null || hostFactionAllowed); } // -------------------------------------------- // - // UTILS + // FACTORY: VALUE OF // -------------------------------------------- // - // considered "default" if host faction is still allowed and nobody has been granted access + public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection factionIds, Collection playerIds) + { + return new TerritoryAccess(hostFactionId, hostFactionAllowed, factionIds, playerIds); + } + + public static TerritoryAccess valueOf(String hostFactionId) + { + return valueOf(hostFactionId, null, null, null); + } + + // -------------------------------------------- // + // INSTANCE METHODS + // -------------------------------------------- // + + public boolean isFactionIdGranted(String factionId) + { + if (this.getHostFactionId().equals(factionId)) + { + return this.isHostFactionAllowed(); + } + return this.getFactionIds().contains(factionId); + } + + // Note that the player can have access without being specifically granted. + // The player could for example be a member of a granted faction. + public boolean isPlayerIdGranted(String playerId) + { + return this.getPlayerIds().contains(playerId); + } + + // A "default" TerritoryAccess could be serialized as a simple string only. + // The host faction is still allowed (default) and no faction or player has been granted explicit access (default). public boolean isDefault() { - return this.hostFactionAllowed && this.factionIds.isEmpty() && this.fplayerIds.isEmpty(); + return this.isHostFactionAllowed() && this.getFactionIds().isEmpty() && this.getPlayerIds().isEmpty(); } - public void setDefault(String factionId) + // TODO: This looks like an extractor in my eyes. + // TODO: Perhaps create a factionId extractor? + public boolean doesHostFactionMatch(Object testSubject) { - this.hostFactionId = factionId; - this.hostFactionAllowed = true; - this.factionIds.clear(); - this.fplayerIds.clear(); + String factionId = null; + if (testSubject instanceof String) + { + factionId = (String)testSubject; + } + else if (testSubject instanceof CommandSender) + { + factionId = UPlayer.get(testSubject).getFactionId(); + } + else if (testSubject instanceof UPlayer) + { + factionId = ((UPlayer)testSubject).getFactionId(); + } + else if (testSubject instanceof Faction) + { + factionId = ((Faction)testSubject).getId(); + } + return this.getHostFactionId().equals(factionId); } - public String factionList(Faction universeExtractable) - { - StringBuilder list = new StringBuilder(); - for (String factionID : factionIds) - { - if (list.length() > 0) - list.append(", "); - list.append(FactionColls.get().get(universeExtractable).get(factionID).getName()); - } - 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(); - } + // -------------------------------------------- // + // DERPINGTON CHECKS + // -------------------------------------------- // // 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(UPlayer.get(testSubject)); + } else if (testSubject instanceof UPlayer) + { return fPlayerHasAccess((UPlayer)testSubject); + } else if (testSubject instanceof Faction) + { return factionHasAccess((Faction)testSubject); + } return false; } public boolean fPlayerHasAccess(UPlayer fplayer) { - if (factionHasAccess(fplayer.getFactionId())) return true; - return fplayerIds.contains(fplayer.getId()); + return this.isPlayerIdGranted(fplayer.getId()) || this.isFactionIdGranted(fplayer.getFaction().getId()); } public boolean factionHasAccess(Faction faction) { - return factionHasAccess(faction.getId()); - } - public boolean factionHasAccess(String factionId) - { - return factionIds.contains(factionId); + return this.isFactionIdGranted(faction.getId()); } // this should normally only be checked after running subjectHasAccess() or fPlayerHasAccess() above to see if they have access explicitly granted @@ -185,25 +261,5 @@ public class TerritoryAccess Faction hostFaction = FactionColls.get().get(testSubject).get(this.getHostFactionId()); return ( ! this.isHostFactionAllowed() && this.doesHostFactionMatch(testSubject) && ! FPerm.ACCESS.has(testSubject, hostFaction, false)); } - - //----------------------------------------------// - // 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; - } + } \ No newline at end of file diff --git a/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java b/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java index 19389c0e..f3399165 100644 --- a/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java +++ b/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java @@ -1,10 +1,8 @@ package com.massivecraft.factions.adapter; import java.lang.reflect.Type; -import java.util.Iterator; -import java.util.logging.Level; +import java.util.Set; -import com.massivecraft.mcore.xlib.gson.JsonArray; import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; import com.massivecraft.mcore.xlib.gson.JsonDeserializer; import com.massivecraft.mcore.xlib.gson.JsonElement; @@ -13,21 +11,23 @@ import com.massivecraft.mcore.xlib.gson.JsonParseException; import com.massivecraft.mcore.xlib.gson.JsonPrimitive; import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; import com.massivecraft.mcore.xlib.gson.JsonSerializer; +import com.massivecraft.mcore.xlib.gson.reflect.TypeToken; -import com.massivecraft.factions.Factions; import com.massivecraft.factions.TerritoryAccess; public class TerritoryAccessAdapter implements JsonDeserializer, JsonSerializer { - //----------------------------------------------// + // -------------------------------------------- // // 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"; + public static final String HOST_FACTION_ID = "hostFactionId"; + public static final String HOST_FACTION_ALLOWED = "hostFactionAllowed"; + public static final String FACTION_IDS = "factionIds"; + public static final String PLAYER_IDS = "playerIds"; + public static final Type SET_OF_STRING_TYPE = new TypeToken>(){}.getType(); + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // @@ -42,95 +42,76 @@ public class TerritoryAccessAdapter implements JsonDeserializer @Override public TerritoryAccess deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - try + // isDefault <=> simple hostFactionId string + if (json.isJsonPrimitive()) { - // 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 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; + String hostFactionId = json.getAsString(); + return TerritoryAccess.valueOf(hostFactionId); } + + // Otherwise object + JsonObject obj = json.getAsJsonObject(); + + // Prepare variables + String hostFactionId = null; + Boolean hostFactionAllowed = null; + Set factionIds = null; + Set playerIds = null; + + // Read variables (test old values first) + JsonElement element = null; + + element = obj.get("ID"); + if (element == null) element = obj.get(HOST_FACTION_ID); + hostFactionId = element.getAsString(); + + element = obj.get("open"); + if (element == null) element = obj.get(HOST_FACTION_ALLOWED); + if (element != null) hostFactionAllowed = element.getAsBoolean(); + + element = obj.get("factions"); + if (element == null) element = obj.get(FACTION_IDS); + if (element != null) factionIds = context.deserialize(element, SET_OF_STRING_TYPE); + + element = obj.get("fplayers"); + if (element == null) element = obj.get(PLAYER_IDS); + if (element != null) playerIds = context.deserialize(element, SET_OF_STRING_TYPE); + + return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } @Override public JsonElement serialize(TerritoryAccess src, Type typeOfSrc, JsonSerializationContext context) { - try + if (src == null) return null; + + // isDefault <=> simple hostFactionId string + if (src.isDefault()) { - if (src == null) return null; - - // if default values, store as simple string - if (src.isDefault()) - { - return new JsonPrimitive(src.getHostFactionId()); - } - - // otherwise, store all data - JsonObject obj = new JsonObject(); - - JsonArray factions = new JsonArray(); - JsonArray fplayers = new JsonArray(); - - Iterator iter = src.getFactionIds().iterator(); - while (iter.hasNext()) - { - factions.add(new JsonPrimitive(iter.next())); - } - - iter = src.getFPlayerIds().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; - + return new JsonPrimitive(src.getHostFactionId()); } - catch (Exception ex) + + // Otherwise object + JsonObject obj = new JsonObject(); + + obj.addProperty(HOST_FACTION_ID, src.getHostFactionId()); + + if (!src.isHostFactionAllowed()) { - ex.printStackTrace(); - Factions.get().log(Level.WARNING, "Error encountered while serializing TerritoryAccess data."); - return null; + obj.addProperty(HOST_FACTION_ALLOWED, src.isHostFactionAllowed()); } + + if (!src.getFactionIds().isEmpty()) + { + obj.add(FACTION_IDS, context.serialize(src.getFactionIds(), SET_OF_STRING_TYPE)); + } + + if (!src.getPlayerIds().isEmpty()) + { + obj.add(PLAYER_IDS, context.serialize(src.getPlayerIds(), SET_OF_STRING_TYPE)); + } + + return obj; } } \ No newline at end of file diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java index 7104e6c8..ab87dfd5 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java @@ -1,111 +1,36 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.FPerm; import com.massivecraft.factions.Perm; -import com.massivecraft.factions.TerritoryAccess; -import com.massivecraft.factions.cmd.arg.ARUPlayer; -import com.massivecraft.factions.cmd.arg.ARFaction; import com.massivecraft.factions.cmd.req.ReqFactionsEnabled; -import com.massivecraft.factions.entity.BoardColls; -import com.massivecraft.factions.entity.UPlayer; -import com.massivecraft.factions.entity.Faction; +import com.massivecraft.mcore.cmd.HelpCommand; +import com.massivecraft.mcore.cmd.req.ReqHasPerm; import com.massivecraft.mcore.cmd.req.ReqIsPlayer; -import com.massivecraft.mcore.ps.PS; -import com.massivecraft.mcore.util.Txt; public class CmdFactionsAccess extends FCommand { + public CmdFactionsAccessView cmdFactionsAccessView = new CmdFactionsAccessView(); + public CmdFactionsAccessPlayer cmdFactionsAccessPlayer = new CmdFactionsAccessPlayer(); + public CmdFactionsAccessFaction cmdFactionsAccessFaction = new CmdFactionsAccessFaction(); + public CmdFactionsAccess() { this.addAliases("access"); - this.addOptionalArg("view|p|player|f|faction", "view"); - this.addOptionalArg("name", "you"); - - this.setDesc("view or grant access for the claimed territory you are in"); - - // TODO: Missing permission node here!? this.addRequirements(ReqFactionsEnabled.get()); this.addRequirements(ReqIsPlayer.get()); + this.addRequirements(ReqHasPerm.get(Perm.ACCESS.node)); + + this.addSubCommand(this.cmdFactionsAccessView); + this.addSubCommand(this.cmdFactionsAccessPlayer); + this.addSubCommand(this.cmdFactionsAccessFaction); } @Override public void perform() - { - String type = this.arg(0); - type = (type == null) ? "" : type.toLowerCase(); - PS chunk = PS.valueOf(me).getChunk(true); - - TerritoryAccess territory = BoardColls.get().getTerritoryAccessAt(chunk); - Faction locFaction = BoardColls.get().getFactionAt(chunk); - boolean accessAny = Perm.ACCESS_ANY.has(sender, false); - - if (type.isEmpty() || type.equals("view")) - { - if ( ! accessAny && ! Perm.ACCESS_VIEW.has(sender, true)) return; - if ( ! accessAny && ! territory.doesHostFactionMatch(usender)) - { - msg("This territory isn't controlled by your faction, so you can't view the access list."); - return; - } - showAccessList(territory, locFaction); - return; - } - - if ( ! accessAny && ! Perm.ACCESS.has(sender, true)) return; - if ( ! accessAny && ! FPerm.ACCESS.has(usender, locFaction, true)) return; - - boolean doPlayer = true; - if (type.equals("f") || type.equals("faction")) - { - doPlayer = false; - } - else if (!type.equals("p") && !type.equals("player")) - { - msg("You must specify \"p\" or \"player\" to indicate a player or \"f\" or \"faction\" to indicate a faction."); - msg("ex. /f access p SomePlayer -or- /f access f SomeFaction"); - msg("Alternately, you can use the command with nothing (or \"view\") specified to simply view the access list."); - return; - } - - String target = ""; - boolean added; - - if (doPlayer) - { - UPlayer targetPlayer = this.arg(1, ARUPlayer.getStartAny(usender), usender); - if (targetPlayer == null) return; - added = territory.toggleFPlayer(targetPlayer); - target = "Player \""+targetPlayer.getName()+"\""; - } - else - { - Faction targetFaction = this.arg(1, ARFaction.get(usenderFaction), usenderFaction); - if (targetFaction == null) return; - added = territory.toggleFaction(targetFaction); - target = "Faction \""+targetFaction.getName()+"\""; - } - - msg("%s has been %s the access list for this territory.", target, Txt.parse(added ? "added to" : "removed from")); - showAccessList(territory, locFaction); - } - - private void showAccessList(TerritoryAccess territory, Faction locFaction) { - msg("Host faction %s has %s in this territory.", locFaction.getName(), Txt.parse(territory.isHostFactionAllowed() ? "normal access" : "restricted access")); - - String players = territory.fplayerList(); - String factions = territory.factionList(locFaction); - - if (factions.isEmpty()) - msg("No factions have been explicitly granted access."); - else - msg("Factions with explicit access: " + factions); - - if (players.isEmpty()) - msg("No players have been explicitly granted access."); - else - msg("Players with explicit access: " + players); + this.getCommandChain().add(this); + HelpCommand.getInstance().execute(this.sender, this.args, this.commandChain); } + } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java new file mode 100644 index 00000000..9193a4cb --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java @@ -0,0 +1,61 @@ +package com.massivecraft.factions.cmd; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import com.massivecraft.factions.RelationParticipator; +import com.massivecraft.factions.TerritoryAccess; +import com.massivecraft.factions.cmd.req.ReqFactionsEnabled; +import com.massivecraft.factions.entity.BoardColls; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.mcore.cmd.req.ReqIsPlayer; +import com.massivecraft.mcore.ps.PS; +import com.massivecraft.mcore.ps.PSFormatHumanSpace; +import com.massivecraft.mcore.util.Txt; + + +public abstract class CmdFactionsAccessAbstract extends FCommand +{ + public PS chunk; + public TerritoryAccess ta; + public Faction hostFaction; + + public CmdFactionsAccessAbstract() + { + this.addRequirements(ReqFactionsEnabled.get()); + this.addRequirements(ReqIsPlayer.get()); + } + + @Override + public void perform() + { + chunk = PS.valueOf(me).getChunk(true); + ta = BoardColls.get().getTerritoryAccessAt(chunk); + hostFaction = ta.getHostFaction(usender); + + this.innerPerform(); + } + + public abstract void innerPerform(); + + public void sendAccessInfo() + { + sendMessage(Txt.titleize("Access at " + chunk.toString(PSFormatHumanSpace.get()))); + msg("Host Faction: %s", hostFaction.describeTo(usender, true)); + msg("Host Faction Allowed: %s", ta.isHostFactionAllowed() ? Txt.parse("TRUE") : Txt.parse("FALSE")); + msg("Granted Players: %s", describeRelationParticipators(ta.getGrantedUPlayers(usender), usender)); + msg("Granted Factions: %s", describeRelationParticipators(ta.getGrantedFactions(usender), usender)); + } + + public static String describeRelationParticipators(Collection relationParticipators, RelationParticipator observer) + { + if (relationParticipators.size() == 0) return Txt.parse("none"); + List descriptions = new ArrayList(); + for (RelationParticipator relationParticipator : relationParticipators) + { + descriptions.add(relationParticipator.describeTo(observer)); + } + return Txt.implodeCommaAnd(descriptions, Txt.parse(", "), Txt.parse(" and ")); + } +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java new file mode 100644 index 00000000..d76d8d83 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java @@ -0,0 +1,43 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.FPerm; +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.cmd.arg.ARFaction; +import com.massivecraft.factions.entity.BoardColls; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.mcore.cmd.arg.ARBoolean; +import com.massivecraft.mcore.cmd.req.ReqHasPerm; + +public class CmdFactionsAccessFaction extends CmdFactionsAccessAbstract +{ + public CmdFactionsAccessFaction() + { + this.addAliases("f", "faction"); + + this.addRequiredArg("faction"); + this.addOptionalArg("yes/no", "toggle"); + + this.addRequirements(ReqHasPerm.get(Perm.ACCESS_FACTION.node)); + } + + @Override + public void innerPerform() + { + // Args + Faction faction = this.arg(0, ARFaction.get(usender)); + if (faction == null) return; + + Boolean newValue = this.arg(1, ARBoolean.get(), !ta.isFactionIdGranted(faction.getId())); + if (newValue == null) return; + + // FPerm + if (FPerm.ACCESS.has(usender, hostFaction, true)) return; + + // Apply + ta = ta.withFactionId(faction.getId(), newValue); + BoardColls.get().setTerritoryAccessAt(chunk, ta); + + // Inform + this.sendAccessInfo(); + } +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java new file mode 100644 index 00000000..9f47cc4a --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java @@ -0,0 +1,43 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.FPerm; +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.cmd.arg.ARUPlayer; +import com.massivecraft.factions.entity.BoardColls; +import com.massivecraft.factions.entity.UPlayer; +import com.massivecraft.mcore.cmd.arg.ARBoolean; +import com.massivecraft.mcore.cmd.req.ReqHasPerm; + +public class CmdFactionsAccessPlayer extends CmdFactionsAccessAbstract +{ + public CmdFactionsAccessPlayer() + { + this.addAliases("p", "player"); + + this.addRequiredArg("player"); + this.addOptionalArg("yes/no", "toggle"); + + this.addRequirements(ReqHasPerm.get(Perm.ACCESS_PLAYER.node)); + } + + @Override + public void innerPerform() + { + // Args + UPlayer uplayer = this.arg(0, ARUPlayer.getStartAny(usender)); + if (uplayer == null) return; + + Boolean newValue = this.arg(1, ARBoolean.get(), !ta.isPlayerIdGranted(uplayer.getId())); + if (newValue == null) return; + + // FPerm + if (FPerm.ACCESS.has(usender, hostFaction, true)) return; + + // Apply + ta = ta.withPlayerId(uplayer.getId(), newValue); + BoardColls.get().setTerritoryAccessAt(chunk, ta); + + // Inform + this.sendAccessInfo(); + } +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java new file mode 100644 index 00000000..320db24a --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java @@ -0,0 +1,21 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Perm; +import com.massivecraft.mcore.cmd.req.ReqHasPerm; + + +public class CmdFactionsAccessView extends CmdFactionsAccessAbstract +{ + public CmdFactionsAccessView() + { + this.addAliases("v", "view"); + + this.addRequirements(ReqHasPerm.get(Perm.ACCESS_VIEW.node)); + } + + @Override + public void innerPerform() + { + this.sendAccessInfo(); + } +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsMoney.java b/src/com/massivecraft/factions/cmd/CmdFactionsMoney.java index c981696a..dc20a56c 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsMoney.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsMoney.java @@ -1,7 +1,10 @@ package com.massivecraft.factions.cmd; +import com.massivecraft.factions.Perm; import com.massivecraft.factions.cmd.req.ReqBankCommandsEnabled; +import com.massivecraft.factions.cmd.req.ReqFactionsEnabled; import com.massivecraft.mcore.cmd.HelpCommand; +import com.massivecraft.mcore.cmd.req.ReqHasPerm; public class CmdFactionsMoney extends FCommand { @@ -16,10 +19,9 @@ public class CmdFactionsMoney extends FCommand { this.addAliases("money"); - this.setDesc("faction money commands"); - this.setHelp("The faction money commands."); - + this.addRequirements(ReqFactionsEnabled.get()); this.addRequirements(ReqBankCommandsEnabled.get()); + this.addRequirements(ReqHasPerm.get(Perm.MONEY.node)); this.addSubCommand(this.cmdMoneyBalance); this.addSubCommand(this.cmdMoneyDeposit); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsMoneyBalance.java b/src/com/massivecraft/factions/cmd/CmdFactionsMoneyBalance.java index 6317d92e..9b12c33b 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsMoneyBalance.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsMoneyBalance.java @@ -2,6 +2,7 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.cmd.arg.ARFaction; import com.massivecraft.factions.cmd.req.ReqBankCommandsEnabled; +import com.massivecraft.factions.cmd.req.ReqFactionsEnabled; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.Perm; @@ -15,6 +16,7 @@ public class CmdFactionsMoneyBalance extends FCommand this.addOptionalArg("faction", "you"); + this.addRequirements(ReqFactionsEnabled.get()); this.addRequirements(ReqHasPerm.get(Perm.MONEY_BALANCE.node)); this.addRequirements(ReqBankCommandsEnabled.get()); } diff --git a/src/com/massivecraft/factions/cmd/arg/ARFaction.java b/src/com/massivecraft/factions/cmd/arg/ARFaction.java index 57eb5313..119bbb2f 100644 --- a/src/com/massivecraft/factions/cmd/arg/ARFaction.java +++ b/src/com/massivecraft/factions/cmd/arg/ARFaction.java @@ -17,7 +17,7 @@ public class ARFaction extends ArgReaderAbstract // INSTANCE & CONSTRUCT // -------------------------------------------- // - public static ARFaction get(Object o) { return new ARFaction(FactionColls.get().get(o)); } + public static ARFaction get(Object universe) { return new ARFaction(FactionColls.get().get(universe)); } private ARFaction(FactionColl coll) { this.coll = coll; diff --git a/src/com/massivecraft/factions/entity/Board.java b/src/com/massivecraft/factions/entity/Board.java index ea0e033f..7bf76b77 100644 --- a/src/com/massivecraft/factions/entity/Board.java +++ b/src/com/massivecraft/factions/entity/Board.java @@ -90,7 +90,7 @@ public class Board extends Entity implements BoardInterface if (ps == null) return null; ps = ps.getChunkCoords(true); TerritoryAccess ret = this.map.get(ps); - if (ret == null) ret = new TerritoryAccess(UConf.get(this).factionIdNone); + if (ret == null) ret = TerritoryAccess.valueOf(UConf.get(this).factionIdNone); return ret; } @@ -98,8 +98,8 @@ public class Board extends Entity implements BoardInterface public Faction getFactionAt(PS ps) { if (ps == null) return null; - String factionId = this.getTerritoryAccessAt(ps).getHostFactionId(); - return FactionColls.get().get(this).get(factionId); + TerritoryAccess ta = this.getTerritoryAccessAt(ps); + return ta.getHostFaction(ps); } // SET @@ -127,7 +127,7 @@ public class Board extends Entity implements BoardInterface TerritoryAccess territoryAccess = null; if (faction != null) { - territoryAccess = new TerritoryAccess(faction.getId()); + territoryAccess = TerritoryAccess.valueOf(faction.getId()); } this.setTerritoryAccessAt(ps, territoryAccess); }