From 0776e5ae553b81bd41f6c4b0f7de12a9c75a3442 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 17 Apr 2013 13:16:22 +0200 Subject: [PATCH] Fixing faction permOverides and making use of a predictate to send faction messages. --- src/com/massivecraft/factions/FPerm.java | 45 ++-- src/com/massivecraft/factions/Faction.java | 211 ++++++++++-------- .../factions/FactionEqualsPredictate.java | 40 ++++ 3 files changed, 184 insertions(+), 112 deletions(-) create mode 100644 src/com/massivecraft/factions/FactionEqualsPredictate.java diff --git a/src/com/massivecraft/factions/FPerm.java b/src/com/massivecraft/factions/FPerm.java index 08b9f17b..97174afc 100644 --- a/src/com/massivecraft/factions/FPerm.java +++ b/src/com/massivecraft/factions/FPerm.java @@ -1,8 +1,9 @@ package com.massivecraft.factions; import java.util.Arrays; +import java.util.Collections; import java.util.EnumSet; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; import org.bukkit.Location; @@ -18,6 +19,10 @@ import com.massivecraft.mcore.ps.PS; */ public enum FPerm { + // -------------------------------------------- // + // ENUM + // -------------------------------------------- // + BUILD("build", "edit the terrain", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), PAINBUILD("painbuild", "edit but take damage"), DOOR("door", "use doors", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), @@ -33,35 +38,47 @@ public enum FPerm ACCESS("access", "grant territory access", Rel.LEADER, Rel.OFFICER), DISBAND("disband", "disband the faction", Rel.LEADER), PERMS("perms", "manage permissions", Rel.LEADER), + + // END OF LIST ; + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + private final String nicename; + public String getNicename() { return this.nicename; } + private final String desc; + public String getDescription() { return this.desc; } + public final Set defaultDefaultValue; + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + private FPerm(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)); + + Set defaultDefaultValue = new LinkedHashSet(); + defaultDefaultValue.addAll(Arrays.asList(rels)); + defaultDefaultValue = Collections.unmodifiableSet(defaultDefaultValue); + this.defaultDefaultValue = defaultDefaultValue; } - public String getNicename() - { - return this.nicename; - } - - public String getDescription() - { - return this.desc; - } + // -------------------------------------------- // + // FROOODLDLLD + // -------------------------------------------- // public Set getDefault() { Set ret = ConfServer.factionPermDefaults.get(this); - if (ret == null) return this.defaultDefaultValue; - return ret; + if (ret == null) ret = this.defaultDefaultValue; + ret = new LinkedHashSet(ret); + return ret; } public static FPerm parse(String str) diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index 81843f03..518ee295 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -13,6 +13,7 @@ import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.util.*; +import com.massivecraft.mcore.mixin.Mixin; import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.util.SenderUtil; @@ -46,7 +47,7 @@ public class Faction extends Entity implements EconomyParticipator this.setInvitedPlayerIds(that.invitedPlayerIds); this.setRelationWishes(that.relationWish); this.setFlags(that.flagOverrides); - this.permOverrides = that.permOverrides; + this.setPerms(that.permOverrides); return this; } @@ -54,7 +55,6 @@ public class Faction extends Entity implements EconomyParticipator // -------------------------------------------- // // FIELDS: RAW // -------------------------------------------- // - // TODO // In this section of the source code we place the field declarations only. // Each field has it's own section further down since even the getter and setter logic takes up quite some place. @@ -92,66 +92,11 @@ public class Faction extends Entity implements EconomyParticipator // The keys in this map are factionIds. private Map relationWish = null; - // The flag overrides are the modifications to the default values. - private Map flagOverrides; + // The flag overrides are modifications to the default values. + private Map flagOverrides = null; - // FIELDS: Permission <-> Groups management - private Map> permOverrides; // Contains the modifications to the default values - public Set getPermittedRelations(FPerm perm) - { - Set ret = this.permOverrides.get(perm); - if (ret == null) ret = perm.getDefault(); - return ret; - } - - /* - public void addPermittedRelation(FPerm perm, Rel rel) - { - Set newPermittedRelations = EnumSet.noneOf(Rel.class); - newPermittedRelations.addAll(this.getPermittedRelations(perm)); - newPermittedRelations.add(rel); - this.setPermittedRelations(perm, newPermittedRelations); - } - - public void removePermittedRelation(FPerm perm, Rel rel) - { - Set newPermittedRelations = EnumSet.noneOf(Rel.class); - newPermittedRelations.addAll(this.getPermittedRelations(perm)); - newPermittedRelations.remove(rel); - this.setPermittedRelations(perm, newPermittedRelations); - }*/ - - public void setRelationPermitted(FPerm perm, Rel rel, boolean permitted) - { - Set newPermittedRelations = EnumSet.noneOf(Rel.class); - newPermittedRelations.addAll(this.getPermittedRelations(perm)); - if (permitted) - { - newPermittedRelations.add(rel); - } - else - { - newPermittedRelations.remove(rel); - } - this.setPermittedRelations(perm, newPermittedRelations); - } - - public void setPermittedRelations(FPerm perm, Set rels) - { - if (perm.getDefault().equals(rels)) - { - this.permOverrides.remove(perm); - return; - } - this.permOverrides.put(perm, rels); - } - - public void setPermittedRelations(FPerm perm, Rel... rels) - { - Set temp = new HashSet(); - temp.addAll(Arrays.asList(rels)); - this.setPermittedRelations(perm, temp); - } + // The perm overrides are modifications to the default values. + private Map> permOverrides = null; // -------------------------------------------- // // CONSTRUCT @@ -159,8 +104,7 @@ public class Faction extends Entity implements EconomyParticipator public Faction() { - this.flagOverrides = new LinkedHashMap(); - this.permOverrides = new LinkedHashMap>(); + } // -------------------------------------------- // @@ -275,8 +219,6 @@ public class Faction extends Entity implements EconomyParticipator // FIELD: home // -------------------------------------------- // - // TODO: Checkery is a bit weird? - public PS getHome() { this.verifyHomeIsValid(); @@ -592,7 +534,104 @@ public class Faction extends Entity implements EconomyParticipator } // -------------------------------------------- // - // RELATION AND COLORS + // FIELD: permOverrides + // -------------------------------------------- // + + // RAW + + public Map> getPerms() + { + Map> ret = new LinkedHashMap>(); + + for (FPerm fperm : FPerm.values()) + { + ret.put(fperm, fperm.getDefault()); + } + + if (this.permOverrides != null) + { + for (Entry> entry : this.permOverrides.entrySet()) + { + ret.put(entry.getKey(), new LinkedHashSet(entry.getValue())); + } + } + + return ret; + } + + public void setPerms(Map> perms) + { + Map> target = new LinkedHashMap>(); + + if (perms != null) + { + for (Entry> entry : perms.entrySet()) + { + target.put(entry.getKey(), new LinkedHashSet(entry.getValue())); + } + } + + Iterator>> iter = target.entrySet().iterator(); + while (iter.hasNext()) + { + Entry> entry = iter.next(); + if (entry.getKey().getDefault().equals(entry.getValue())) + { + iter.remove(); + } + } + + if (target == null || target.isEmpty()) + { + this.permOverrides = null; + } + else + { + this.permOverrides = target; + } + this.changed(); + } + + // FINER + + public Set getPermittedRelations(FPerm perm) + { + return this.getPerms().get(perm); + } + + public void setPermittedRelations(FPerm perm, Set rels) + { + Map> perms = this.getPerms(); + perms.put(perm, rels); + this.setPerms(perms); + } + + public void setPermittedRelations(FPerm perm, Rel... rels) + { + Set temp = new HashSet(); + temp.addAll(Arrays.asList(rels)); + this.setPermittedRelations(perm, temp); + } + + public void setRelationPermitted(FPerm perm, Rel rel, boolean permitted) + { + Map> perms = this.getPerms(); + Set rels = perms.get(perm); + + if (permitted) + { + rels.add(rel); + } + else + { + rels.remove(rel); + } + + this.setPerms(perms); + } + + // -------------------------------------------- // + // OVERRIDE: RelationParticipator // -------------------------------------------- // @Override @@ -693,7 +732,7 @@ public class Faction extends Entity implements EconomyParticipator } // -------------------------------------------- // - // FPLAYERS + // FOREIGN KEYS: FPLAYERS // -------------------------------------------- // public List getFPlayers() @@ -821,64 +860,40 @@ public class Faction extends Entity implements EconomyParticipator // -------------------------------------------- // // MESSAGES // -------------------------------------------- // - // These methods are simply proxied in from the SenderEntity class using a for loop. + // These methods are simply proxied in from the Mixin. // CONVENIENCE SEND MESSAGE public boolean sendMessage(String message) { - for (FPlayer fplayer : this.getFPlayers()) - { - fplayer.sendMessage(message); - } - return true; + return Mixin.message(new FactionEqualsPredictate(this), message); } public boolean sendMessage(String... messages) { - for (FPlayer fplayer : this.getFPlayers()) - { - fplayer.sendMessage(messages); - } - return true; + return Mixin.message(new FactionEqualsPredictate(this), messages); } public boolean sendMessage(Collection messages) { - for (FPlayer fplayer : this.getFPlayers()) - { - fplayer.sendMessage(messages); - } - return true; + return Mixin.message(new FactionEqualsPredictate(this), messages); } // CONVENIENCE MSG public boolean msg(String msg) { - for (FPlayer fplayer : this.getFPlayers()) - { - fplayer.msg(msg); - } - return true; + return Mixin.msg(new FactionEqualsPredictate(this), msg); } public boolean msg(String msg, Object... args) { - for (FPlayer fplayer : this.getFPlayers()) - { - fplayer.msg(msg, args); - } - return true; + return Mixin.msg(new FactionEqualsPredictate(this), msg, args); } public boolean msg(Collection msgs) { - for (FPlayer fplayer : this.getFPlayers()) - { - fplayer.msg(msgs); - } - return true; + return Mixin.msg(new FactionEqualsPredictate(this), msgs); } } diff --git a/src/com/massivecraft/factions/FactionEqualsPredictate.java b/src/com/massivecraft/factions/FactionEqualsPredictate.java new file mode 100644 index 00000000..a3c1751e --- /dev/null +++ b/src/com/massivecraft/factions/FactionEqualsPredictate.java @@ -0,0 +1,40 @@ +package com.massivecraft.factions; + +import java.io.Serializable; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.mcore.Predictate; + +public class FactionEqualsPredictate implements Predictate, Serializable +{ + private static final long serialVersionUID = 1L; + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final String factionId; + public String getFactionId() { return this.factionId; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public FactionEqualsPredictate(Faction faction) + { + this.factionId = faction.getId(); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public boolean apply(CommandSender sender) + { + FPlayer fplayer = FPlayer.get(sender); + return this.getFactionId().equals(fplayer.getFactionId()); + } + +} \ No newline at end of file