From 371261a07ff00680551783f5a6327d351c85d009 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 23 Oct 2011 23:17:02 +0200 Subject: [PATCH] Working on the faction perms as well now --- plugin.yml | 6 + src/com/massivecraft/factions/Conf.java | 10 ++ src/com/massivecraft/factions/Faction.java | 65 +++------- .../massivecraft/factions/cmd/CmdPerm.java | 88 +++++++++++++ .../massivecraft/factions/cmd/FCmdRoot.java | 2 + .../massivecraft/factions/cmd/FCommand.java | 35 ++++++ .../factions/struct/FactionPerm.java | 117 ++++++++++++++++-- .../factions/struct/Permission.java | 2 + 8 files changed, 272 insertions(+), 53 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdPerm.java diff --git a/plugin.yml b/plugin.yml index 159b5d48..ff1c5f9d 100644 --- a/plugin.yml +++ b/plugin.yml @@ -33,6 +33,7 @@ permissions: factions.kit.fullplayer: true factions.bypass: true factions.flag.any: true + factions.perm.any: true factions.kick.any: true factions.ownershipbypass: true factions.kit.fullplayer: @@ -52,6 +53,7 @@ permissions: factions.description: true factions.disband: true factions.flag: true + factions.perm: true factions.help: true factions.home: true factions.invite: true @@ -102,6 +104,10 @@ permissions: description: change faction flags factions.flag.any: description: change all flags for all factions + factions.perm: + description: change faction permissions + factions.perm.any: + description: change all permissions for all factions factions.help: description: display a help page factions.home: diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 0f4a6335..969c8436 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -6,6 +6,8 @@ import org.bukkit.*; import org.bukkit.entity.CreatureType; import com.massivecraft.factions.struct.FactionFlag; +import com.massivecraft.factions.struct.FactionPerm; +import com.massivecraft.factions.struct.Rel; public class Conf { @@ -27,6 +29,8 @@ public class Conf // REFACTOR ASJDKJASDKFJKASDF public static Map factionFlagDefaults; public static Map factionFlagIsChangeable; + public static Map> factionPermDefaults; + // REFACTOR ASJDKJASDKFJKASDF EEEEEENNNNDD @@ -285,6 +289,12 @@ public class Conf factionFlagIsChangeable.put(flag, flag.defaultDefaultChangeable); } + factionPermDefaults = new LinkedHashMap>(); + for (FactionPerm perm: FactionPerm.values()) + { + factionPermDefaults.put(perm, perm.defaultDefaultValue); + } + territoryEnemyDenyCommands.add("home"); territoryEnemyDenyCommands.add("sethome"); territoryEnemyDenyCommands.add("spawn"); diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index aeadefc3..c109adac 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -12,6 +12,7 @@ import com.massivecraft.factions.iface.EconomyParticipator; import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.struct.FactionFlag; +import com.massivecraft.factions.struct.FactionPerm; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.util.*; @@ -39,25 +40,6 @@ public class Faction extends Entity implements EconomyParticipator public boolean getOpen() { return open; } public void setOpen(boolean isOpen) { open = isOpen; } - // FIELD: peaceful - // "peaceful" status can only be set by server admins/moderators/ops, and prevents PvP and land capture to/from the faction - //private boolean peaceful; - //public boolean isPeaceful() { return this.peaceful; } - //public void setPeaceful(boolean isPeaceful) { this.peaceful = isPeaceful; } - - // FIELD: peacefulExplosionsEnabled - //private boolean peacefulExplosionsEnabled; - //public void setPeacefulExplosionsEnabled(boolean val) { peacefulExplosionsEnabled = val; } - //public boolean getPeacefulExplosionsEnabled(){ return this.peacefulExplosionsEnabled; } - - //public boolean noExplosionsInTerritory() { return this.peaceful && ! peacefulExplosionsEnabled; } - - // FIELD: permanent - // "permanent" status can only be set by server admins/moderators/ops, and allows the faction to remain even with 0 members - //private boolean permanent; - //public boolean isPermanent() { return permanent; } - //public void setPermanent(boolean isPermanent) { permanent = isPermanent; } - // FIELD: tag private String tag; public String getTag() { return this.tag; } @@ -140,15 +122,31 @@ public class Faction extends Entity implements EconomyParticipator } public void setFlag(FactionFlag flag, boolean value) { - if (Conf.factionFlagDefaults.get(flag) == value) + if (Conf.factionFlagDefaults.get(flag).equals(value)) { this.flagOverrides.remove(flag); return; } this.flagOverrides.put(flag, value); } - + // FIELDS: Permission <-> Groups management + private Map> permOverrides; // Contains the modifications to the default values + public Set getPerm(FactionPerm perm) + { + Set ret = this.permOverrides.get(perm); + if (ret == null) ret = perm.getDefault(); + return ret; + } + public void setPerm(FactionPerm perm, Set value) + { + if (Conf.factionPermDefaults.get(perm).equals(value)) + { + this.permOverrides.remove(perm); + return; + } + this.permOverrides.put(perm, value); + } // -------------------------------------------- // @@ -169,15 +167,6 @@ public class Faction extends Entity implements EconomyParticipator this.flagOverrides = new LinkedHashMap(); } - // -------------------------------------------- // - // Extra Getters And Setters - // -------------------------------------------- // - - //public boolean noPvPInTerritory() { return isSafeZone() || (peaceful && Conf.peacefulTerritoryDisablePVP); } - - //public boolean noMonstersInTerritory() { return isSafeZone() || (peaceful && Conf.peacefulTerritoryDisableMonsters); } - - // ------------------------------- // Understand the types @@ -196,22 +185,6 @@ public class Faction extends Entity implements EconomyParticipator return this.getId().equals("0"); } - /*public boolean isSafeZone() - { - return this.getId().equals("-1"); - } - - public boolean isWarZone() - { - return this.getId().equals("-2"); - } - - public boolean isPlayerFreeType() - { - return this.isSafeZone() || this.isWarZone(); - }*/ - - // ------------------------------- // Relation and relation colors // ------------------------------- diff --git a/src/com/massivecraft/factions/cmd/CmdPerm.java b/src/com/massivecraft/factions/cmd/CmdPerm.java new file mode 100644 index 00000000..c6d06743 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdPerm.java @@ -0,0 +1,88 @@ +package com.massivecraft.factions.cmd; + +import java.util.Set; + +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.struct.FactionPerm; +import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.struct.Rel; +import com.massivecraft.factions.zcore.util.TextUtil; + +public class CmdPerm extends FCommand +{ + + public CmdPerm() + { + super(); + this.aliases.add("perm"); + + //this.requiredArgs.add(""); + this.optionalArgs.put("faction", "your"); + this.optionalArgs.put("perm", "all"); + this.optionalArgs.put("relationdelta", "read"); + + this.permission = Permission.PERM.node; + this.disableOnLock = true; + + this.errorOnToManyArgs = false; + + senderMustBePlayer = false; + senderMustBeMember = false; + senderMustBeOfficer = false; + senderMustBeLeader = false; + } + + @Override + public void perform() + { + Faction faction = myFaction; + if (this.argIsSet(0)) + { + faction = this.argAsFaction(0); + } + if (faction == null) return; + + msg(p.txt.titleize("Perm(s) for " + faction.describeTo(fme))); + + if ( ! this.argIsSet(1)) + { + for (FactionPerm perm : FactionPerm.values()) + { + msg(perm.getStateInfo(faction.getPerm(perm), true)); + } + return; + } + + FactionPerm perm = this.argAsFactionPerm(1); + if (perm == null) return; + if ( ! this.argIsSet(2)) + { + msg(perm.getStateInfo(faction.getPerm(perm), true)); + return; + } + + // TODO: Awesomesause parser for deltas... + Set targetValue = FactionPerm.parseRelDeltas(TextUtil.implode(args.subList(2, args.size()-1), " "), faction.getPerm(perm)); + + // Do the sender have the right to change perms for this faction? + if (Permission.PERM_ANY.has(sender)) + { + // This sender may modify any perm for anyone + } + else if (faction != myFaction) + { + msg("You are not a member in that faction."); + return; + } + else if (fme.getRole().isLessThan(Rel.OFFICER)) + { + msg("You must be faction leader or officer to change your faction permissions."); + return; + } + + // Do the change + faction.setPerm(perm, targetValue); + msg(perm.getStateInfo(faction.getPerm(perm), true)); + } + +} diff --git a/src/com/massivecraft/factions/cmd/FCmdRoot.java b/src/com/massivecraft/factions/cmd/FCmdRoot.java index 883b0ad2..c513a5cb 100644 --- a/src/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/com/massivecraft/factions/cmd/FCmdRoot.java @@ -31,6 +31,7 @@ public class FCmdRoot extends FCommand public CmdOpen cmdOpen = new CmdOpen(); public CmdOwner cmdOwner = new CmdOwner(); public CmdOwnerList cmdOwnerList = new CmdOwnerList(); + public CmdPerm cmdPerm = new CmdPerm(); public CmdPower cmdPower = new CmdPower(); public CmdRelationAlly cmdRelationAlly = new CmdRelationAlly(); public CmdRelationEnemy cmdRelationEnemy = new CmdRelationEnemy(); @@ -93,6 +94,7 @@ public class FCmdRoot extends FCommand this.addSubCommand(this.cmdOpen); this.addSubCommand(this.cmdOwner); this.addSubCommand(this.cmdOwnerList); + this.addSubCommand(this.cmdPerm); this.addSubCommand(this.cmdPower); this.addSubCommand(this.cmdRelationAlly); this.addSubCommand(this.cmdRelationEnemy); diff --git a/src/com/massivecraft/factions/cmd/FCommand.java b/src/com/massivecraft/factions/cmd/FCommand.java index ebb5e593..e5e3350e 100644 --- a/src/com/massivecraft/factions/cmd/FCommand.java +++ b/src/com/massivecraft/factions/cmd/FCommand.java @@ -13,6 +13,7 @@ import com.massivecraft.factions.Faction; import com.massivecraft.factions.Factions; import com.massivecraft.factions.P; import com.massivecraft.factions.struct.FactionFlag; +import com.massivecraft.factions.struct.FactionPerm; import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.zcore.MCommand; @@ -308,6 +309,40 @@ public abstract class FCommand extends MCommand

return this.argAsFactionFlag(idx, null); } + // FACTION PERM ====================== + public FactionPerm strAsFactionPerm(String name, FactionPerm def, boolean msg) + { + FactionPerm ret = def; + + if (name != null) + { + FactionPerm perm = FactionPerm.parse(name); + if (perm != null) + { + ret = perm; + } + } + + if (msg && ret == null) + { + this.msg("The faction-perm \"

%s\" could not be found.", name); + } + + return ret; + } + public FactionPerm argAsFactionPerm(int idx, FactionPerm def, boolean msg) + { + return this.strAsFactionPerm(this.argAsString(idx), def, msg); + } + public FactionPerm argAsFactionPerm(int idx, FactionPerm def) + { + return this.argAsFactionPerm(idx, def, true); + } + public FactionPerm argAsFactionPerm(int idx) + { + return this.argAsFactionPerm(idx, null); + } + // -------------------------------------------- // // Commonly used logic diff --git a/src/com/massivecraft/factions/struct/FactionPerm.java b/src/com/massivecraft/factions/struct/FactionPerm.java index 7edce88e..7dfe75ec 100644 --- a/src/com/massivecraft/factions/struct/FactionPerm.java +++ b/src/com/massivecraft/factions/struct/FactionPerm.java @@ -1,16 +1,119 @@ package com.massivecraft.factions.struct; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.zcore.util.TextUtil; + /** * Permissions that you (a player) may or may not have in the territory of a certain faction. * Each faction have many Rel's assigned to each one of these Perms. */ public enum FactionPerm { - BUILD, // This player can build in the faction - PAINBUILD, // This player can build in the faction BUT will take damage each time. This is overridden by BUILD if player has both - DOOR, - WORKBENCH, - CONTAINER, - BUTTON, - LEVER, + BUILD("build", "edit the terrain", Rel.MEMBER), + PAINBUILD("painbuild", "edit but take damage", Rel.ALLY), + DOOR("door", "use doors etc.", Rel.MEMBER, Rel.ALLY), + CONTAINER("container", "use chests etc.", Rel.MEMBER), + BUTTON("button", "use stone buttons", Rel.MEMBER, Rel.ALLY), + LEVER("lever", "use levers", Rel.MEMBER, Rel.ALLY), + ; + + private final String nicename; + private final String desc; + public final Set defaultDefaultValue; + + private FactionPerm(final String nicename, final String desc, final Rel... rels) + { + this.nicename = nicename; + this.desc = desc; + this.defaultDefaultValue = new HashSet(); + this.defaultDefaultValue.addAll(Arrays.asList(rels)); + } + + public String getNicename() + { + return this.nicename; + } + + public String getDescription() + { + return this.desc; + } + + public Set getDefault() + { + Set ret = Conf.factionPermDefaults.get(this); + if (ret == null) return this.defaultDefaultValue; + return ret; + } + + public static FactionPerm parse(String str) + { + str = str.toLowerCase(); + if (str.startsWith("bui")) return BUILD; + if (str.startsWith("p")) return PAINBUILD; + if (str.startsWith("d")) return DOOR; + if (str.startsWith("c")) return CONTAINER; + if (str.startsWith("but")) return BUTTON; + if (str.startsWith("l")) return LEVER; + return null; + } + + public String getStateInfo(Set value, boolean withDesc) + { + String ret = ""+this.getNicename()+ " "; + + List rels = new ArrayList(); + for (Rel rel : value) + { + rels.add(""+rel.nicename); + } + ret += TextUtil.implode(rels, " ,"); + + if (withDesc) + { + ret += " " + this.getDescription(); + } + return ret; + } + + public static Set parseRelDeltas(String str, Set current) + { + Set ret = new HashSet(); + ret.addAll(current); + + List nodes = new ArrayList(Arrays.asList(str.split("\\s+"))); + + for (String node : nodes) + { + boolean add = true; + if (node.startsWith("-")) + { + add = false; + node = node.substring(1); + } + else if (node.startsWith("+")) + { + node = node.substring(1); + } + Rel rel = Rel.parse(node); + + if (rel == null) continue; + + if (add) + { + ret.add(rel); + } + else + { + ret.remove(rel); + } + } + return ret; + } } diff --git a/src/com/massivecraft/factions/struct/Permission.java b/src/com/massivecraft/factions/struct/Permission.java index a4df4210..4bae855a 100644 --- a/src/com/massivecraft/factions/struct/Permission.java +++ b/src/com/massivecraft/factions/struct/Permission.java @@ -20,6 +20,8 @@ public enum Permission DISBAND_ANY("disband.any"), FLAG("flag"), FLAG_ANY("flag.any"), + PERM("perm"), + PERM_ANY("perm.any"), HELP("help"), HOME("home"), INVITE("invite"),