From 6b459391ef1ad878f5ce6d80ff394da3d168e753 Mon Sep 17 00:00:00 2001 From: ulumulu1510 Date: Sat, 11 Mar 2017 20:27:40 +0100 Subject: [PATCH] Implement creatable ranks. --- src/com/massivecraft/factions/Factions.java | 7 +- src/com/massivecraft/factions/Perm.java | 7 + .../massivecraft/factions/PermissibleId.java | 20 + .../factions/PermissionIdentifiable.java | 6 + ...ateMPlayerRole.java => PredicateRank.java} | 20 +- src/com/massivecraft/factions/Rank.java | 158 ++++++ src/com/massivecraft/factions/Rel.java | 45 +- .../factions/chat/tag/ChatTagRole.java | 2 +- .../factions/chat/tag/ChatTagRoleprefix.java | 35 -- .../chat/tag/ChatTagRoleprefixforce.java | 30 -- .../factions/cmd/CmdFactionsCreate.java | 6 +- .../factions/cmd/CmdFactionsInviteList.java | 16 +- .../factions/cmd/CmdFactionsKick.java | 9 +- .../factions/cmd/CmdFactionsPermSet.java | 15 +- .../factions/cmd/CmdFactionsRank.java | 390 +------------- .../factions/cmd/CmdFactionsRankCreate.java | 46 ++ .../factions/cmd/CmdFactionsRankDelete.java | 69 +++ .../factions/cmd/CmdFactionsRankList.java | 79 +++ .../factions/cmd/CmdFactionsRankName.java | 65 +++ .../factions/cmd/CmdFactionsRankOld.java | 7 +- .../factions/cmd/CmdFactionsRankOrder.java | 66 +++ .../factions/cmd/CmdFactionsRankSet.java | 226 ++++++++ .../factions/cmd/CmdFactionsRankShow.java | 42 ++ .../factions/cmd/CmdFactionsTitle.java | 2 +- .../factions/cmd/req/ReqRoleIsAtLeast.java | 52 -- .../factions/cmd/type/TypeRank.java | 152 +++--- .../factions/cmd/type/TypeRankName.java | 68 +++ .../comparator/ComparatorMPlayerRole.java | 16 +- .../factions/engine/EnginePlayerData.java | 12 +- .../massivecraft/factions/entity/Faction.java | 490 ++++++++++++------ .../factions/entity/FactionColl.java | 55 +- .../massivecraft/factions/entity/MConf.java | 41 +- .../massivecraft/factions/entity/MPerm.java | 97 ++-- .../massivecraft/factions/entity/MPlayer.java | 54 +- .../event/EventFactionsRankChange.java | 13 +- .../herochat/ChannelFactionsAllies.java | 11 +- .../herochat/ChannelFactionsFaction.java | 11 +- .../massivecraft/factions/update/OldConf.java | 223 ++++++++ .../factions/util/RelationUtil.java | 6 +- 39 files changed, 1717 insertions(+), 952 deletions(-) create mode 100644 src/com/massivecraft/factions/PermissibleId.java create mode 100644 src/com/massivecraft/factions/PermissionIdentifiable.java rename src/com/massivecraft/factions/{predicate/PredicateMPlayerRole.java => PredicateRank.java} (56%) create mode 100644 src/com/massivecraft/factions/Rank.java delete mode 100644 src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java delete mode 100644 src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsRankCreate.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsRankDelete.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsRankList.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsRankName.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsRankOrder.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsRankShow.java delete mode 100644 src/com/massivecraft/factions/cmd/req/ReqRoleIsAtLeast.java create mode 100644 src/com/massivecraft/factions/cmd/type/TypeRankName.java create mode 100644 src/com/massivecraft/factions/update/OldConf.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 8e7bbeea..139a1733 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -1,5 +1,7 @@ package com.massivecraft.factions; +import org.bukkit.ChatColor; + import com.massivecraft.factions.adapter.BoardAdapter; import com.massivecraft.factions.adapter.BoardMapAdapter; import com.massivecraft.factions.adapter.RelAdapter; @@ -14,8 +16,6 @@ import com.massivecraft.factions.chat.tag.ChatTagName; import com.massivecraft.factions.chat.tag.ChatTagNameforce; import com.massivecraft.factions.chat.tag.ChatTagRelcolor; import com.massivecraft.factions.chat.tag.ChatTagRole; -import com.massivecraft.factions.chat.tag.ChatTagRoleprefix; -import com.massivecraft.factions.chat.tag.ChatTagRoleprefixforce; import com.massivecraft.factions.chat.tag.ChatTagTitle; import com.massivecraft.factions.cmd.CmdFactions; import com.massivecraft.factions.cmd.type.TypeFactionChunkChangeType; @@ -65,7 +65,6 @@ import com.massivecraft.massivecore.MassivePlugin; import com.massivecraft.massivecore.command.type.RegistryType; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.xlib.gson.GsonBuilder; -import org.bukkit.ChatColor; public class Factions extends MassivePlugin { @@ -188,8 +187,6 @@ public class Factions extends MassivePlugin // ChatTags, ChatTagRelcolor.class, ChatTagRole.class, - ChatTagRoleprefix.class, - ChatTagRoleprefixforce.class, ChatTagName.class, ChatTagNameforce.class, ChatTagTitle.class diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index aa764bc4..d7e4fe7b 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -1,6 +1,7 @@ package com.massivecraft.factions; import com.massivecraft.massivecore.Identified; +import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.util.PermissionUtil; import org.bukkit.permissions.Permissible; @@ -115,6 +116,12 @@ public enum Perm implements Identified // HAS // -------------------------------------------- // + public void hasThrow(Permissible permissible, boolean verboose) throws MassiveException + { + boolean has = this.has(permissible, verboose); + if (!has) throw new MassiveException(); + } + public boolean has(Permissible permissible, boolean verboose) { return PermissionUtil.hasPermission(permissible, this, verboose); diff --git a/src/com/massivecraft/factions/PermissibleId.java b/src/com/massivecraft/factions/PermissibleId.java new file mode 100644 index 00000000..9965bd47 --- /dev/null +++ b/src/com/massivecraft/factions/PermissibleId.java @@ -0,0 +1,20 @@ +package com.massivecraft.factions; + + +public class PermissibleId +{ + // "Default" ranks + public static final String LEADER = "leader"; + public static final String OFFICER = "officer"; + public static final String MEMBER = "member"; + public static final String RECRUIT = "recruit"; + + // Relations + public static final String NEUTRAL = "neutral"; + public static final String TRUCE = "truce"; + public static final String ALLY = "ally"; + public static final String ENEMY = "enemy"; + + public static final String[] ALL = new String[] {LEADER, OFFICER, MEMBER, RECRUIT, NEUTRAL, TRUCE, ALLY, ENEMY}; + +} diff --git a/src/com/massivecraft/factions/PermissionIdentifiable.java b/src/com/massivecraft/factions/PermissionIdentifiable.java new file mode 100644 index 00000000..f32e55ff --- /dev/null +++ b/src/com/massivecraft/factions/PermissionIdentifiable.java @@ -0,0 +1,6 @@ +package com.massivecraft.factions; + +public interface PermissionIdentifiable +{ + String getPermissibleId(); +} diff --git a/src/com/massivecraft/factions/predicate/PredicateMPlayerRole.java b/src/com/massivecraft/factions/PredicateRank.java similarity index 56% rename from src/com/massivecraft/factions/predicate/PredicateMPlayerRole.java rename to src/com/massivecraft/factions/PredicateRank.java index b56eeb21..b6974ab7 100644 --- a/src/com/massivecraft/factions/predicate/PredicateMPlayerRole.java +++ b/src/com/massivecraft/factions/PredicateRank.java @@ -1,26 +1,25 @@ -package com.massivecraft.factions.predicate; +package com.massivecraft.factions; -import com.massivecraft.factions.Rel; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.massivecore.predicate.Predicate; -public class PredicateMPlayerRole implements Predicate +public class PredicateRank implements Predicate { // -------------------------------------------- // // FIELDS // -------------------------------------------- // - private final Rel role; - public Rel getRole() { return this.role; } + private final Rank rank; + public Rank getRank() { return this.rank; } // -------------------------------------------- // - // INSTANCE AND CONTRUCT + // INSTANCE & CONSTRUCT // -------------------------------------------- // - public static PredicateMPlayerRole get(Rel role) { return new PredicateMPlayerRole(role); } - public PredicateMPlayerRole(Rel role) + public static PredicateRank get(Rank role) { return new PredicateRank(role); } + public PredicateRank(Rank rank) { - this.role = role; + this.rank = rank; } // -------------------------------------------- // @@ -31,6 +30,7 @@ public class PredicateMPlayerRole implements Predicate public boolean apply(MPlayer mplayer) { if (mplayer == null) return false; - return mplayer.getRole() == this.role; + return mplayer.getRank().equals(this.getRank()); } + } diff --git a/src/com/massivecraft/factions/Rank.java b/src/com/massivecraft/factions/Rank.java new file mode 100644 index 00000000..036b3f53 --- /dev/null +++ b/src/com/massivecraft/factions/Rank.java @@ -0,0 +1,158 @@ +package com.massivecraft.factions; + +import java.util.Objects; +import java.util.Set; + +import com.massivecraft.massivecore.Named; +import com.massivecraft.massivecore.Prioritized; +import com.massivecraft.massivecore.collections.MassiveSet; +import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.store.MStore; +import com.massivecraft.massivecore.util.MUtil; + +public class Rank implements Named, Comparable, Prioritized, PermissionIdentifiable +{ + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + private static Rank LEADER = new Rank(1, "Leader"); + private static Rank OFFICER = new Rank(2, "Officer"); + private static Rank MEMBER = new Rank(3, "Member"); + private static Rank RECRUIT = new Rank(4, "Recruit"); + public static Rank DEFAULT = new Rank(null, null); + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private transient String id; + public String getId() { return this.id; } + public void setId(String id) { this.id = id; } + + private transient Integer order; + public Integer getOrder() { return this.order; } + @Override public int getPriority() { return this.getOrder(); } + public void setOrder(Integer order) { this.order = order; } + + private String name; + @Override public String getName() { return this.name; } + public void setName(String name) { this.name = name; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public Rank(Integer order, String name) + { + this.id = MStore.createId(); + this.order = order; + this.name = name; + } + + // -------------------------------------------- // + // CONVENIENCE + // -------------------------------------------- // + + public Rank copy() + { + return new Rank(order, name); + } + + public boolean isLeader() + { + return this.getOrder() == 1; + } + + public boolean isHigherThan(Rank rank) + { + return this.getOrder() < rank.getOrder(); + } + + public boolean isLessThan(Rank rank) + { + return this.getOrder() > rank.getOrder(); + } + + @Override + public String getPermissibleId() + { + return "rank" + this.getOrder(); + } + + // -------------------------------------------- // + // CREATE DEFAULT + // -------------------------------------------- // + + public static Set createDefaultRanks() + { + // Create + Set ret = new MassiveSet<>(4); + + // Fill + ret.add(LEADER.copy()); + ret.add(OFFICER.copy()); + ret.add(MEMBER.copy()); + ret.add(RECRUIT.copy()); + + // Return + return ret; + } + + // -------------------------------------------- // + // VISUALIZE + // -------------------------------------------- // + + public Mson visualize() + { + return Mson.mson( + String.valueOf(this.getOrder()), + Mson.DOT, + Mson.SPACE, + this.getName() + ); + } + + // -------------------------------------------- // + // EQUALS & HASHCODE + // -------------------------------------------- // + + @Override + public boolean equals(Object object) + { + if (this == object) return true; + if (!(object instanceof Rank)) return false; + Rank that = (Rank) object; + + return MUtil.equals( + this.order, that.order, + this.name, that.name + ); + } + + @Override + public int hashCode() + { + return Objects.hash(order, name); + } + + // -------------------------------------------- // + // COMPARE TO + // -------------------------------------------- // + + @Override + public int compareTo(Rank that) + { + if (that == null) return +1; + + int orderOne = this.getOrder(); + int orderTwo = that.getOrder(); + + if (orderOne == orderTwo) return 0; + if (orderOne > orderTwo) return +1; + if (orderOne < orderTwo) return -1; + + return 0; + } + +} diff --git a/src/com/massivecraft/factions/Rel.java b/src/com/massivecraft/factions/Rel.java index 965649d5..e41ec0fb 100644 --- a/src/com/massivecraft/factions/Rel.java +++ b/src/com/massivecraft/factions/Rel.java @@ -9,7 +9,7 @@ import org.bukkit.ChatColor; import java.util.Collections; import java.util.Set; -public enum Rel implements Colorized, Named +public enum Rel implements Colorized, Named, PermissionIdentifiable { // -------------------------------------------- // // ENUM @@ -35,25 +35,10 @@ public enum Rel implements Colorized, Named "Ally" ) { @Override public ChatColor getColor() { return MConf.get().colorAlly; } }, - RECRUIT( - "a recruit in your faction", "recruits in your faction", "", "", - "Recruit" - ) { @Override public String getPrefix() { return MConf.get().prefixRecruit; } }, - - MEMBER( - "a member in your faction", "members in your faction", "your faction", "your factions", - "Member" - ) { @Override public String getPrefix() { return MConf.get().prefixMember; } }, - - OFFICER( - "an officer in your faction", "officers in your faction", "", "", - "Officer", "Moderator" - ) { @Override public String getPrefix() { return MConf.get().prefixOfficer; } }, - - LEADER( - "your faction leader", "your faction leader", "", "", - "Leader", "Admin", "Owner" - ) { @Override public String getPrefix() { return MConf.get().prefixLeader; } }, + FACTION( + "someone in your faction", "a faction member", "faction members", + "Faction" + ) { @Override public ChatColor getColor() { return MConf.get().colorMember; } }, // END OF LIST ; @@ -93,16 +78,6 @@ public enum Rel implements Colorized, Named this.names = Collections.unmodifiableSet(new MassiveSet<>(names)); } - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public ChatColor getColor() - { - return MConf.get().colorMember; - } - // -------------------------------------------- // // UTIL // -------------------------------------------- // @@ -127,20 +102,16 @@ public enum Rel implements Colorized, Named return this.getValue() > rel.getValue(); } - public boolean isRank() - { - return this.isAtLeast(Rel.RECRUIT); - } - // Used for friendly fire. public boolean isFriend() { return this.isAtLeast(TRUCE); } - public String getPrefix() + @Override + public String getPermissibleId() { - return ""; + return this.getName().toLowerCase(); } } diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRole.java b/src/com/massivecraft/factions/chat/tag/ChatTagRole.java index 4b2f3df9..74430f25 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRole.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagRole.java @@ -25,7 +25,7 @@ public class ChatTagRole extends ChatTag // Get entities MPlayer usender = MPlayer.get(sender); - return Txt.upperCaseFirst(usender.getRole().toString().toLowerCase()); + return Txt.upperCaseFirst(usender.getRank().toString().toLowerCase()); } } diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java b/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java deleted file mode 100644 index da491d92..00000000 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.massivecraft.factions.chat.tag; - -import com.massivecraft.factions.chat.ChatTag; -import com.massivecraft.factions.entity.Faction; -import com.massivecraft.factions.entity.MPlayer; -import org.bukkit.command.CommandSender; - -public class ChatTagRoleprefix extends ChatTag -{ - // -------------------------------------------- // - // INSTANCE & CONSTRUCT - // -------------------------------------------- // - - private ChatTagRoleprefix() { super("factions_roleprefix"); } - private static ChatTagRoleprefix i = new ChatTagRoleprefix(); - public static ChatTagRoleprefix get() { return i; } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public String getReplacement(CommandSender sender, CommandSender recipient) - { - // Get entities - MPlayer usender = MPlayer.get(sender); - - // No "force" - Faction faction = usender.getFaction(); - if (faction.isNone()) return ""; - - return usender.getRole().getPrefix(); - } - -} diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java b/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java deleted file mode 100644 index f54c407e..00000000 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.massivecraft.factions.chat.tag; - -import com.massivecraft.factions.chat.ChatTag; -import com.massivecraft.factions.entity.MPlayer; -import org.bukkit.command.CommandSender; - -public class ChatTagRoleprefixforce extends ChatTag -{ - // -------------------------------------------- // - // INSTANCE & CONSTRUCT - // -------------------------------------------- // - - private ChatTagRoleprefixforce() { super("factions_roleprefixforce"); } - private static ChatTagRoleprefixforce i = new ChatTagRoleprefixforce(); - public static ChatTagRoleprefixforce get() { return i; } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public String getReplacement(CommandSender sender, CommandSender recipient) - { - // Get entities - MPlayer usender = MPlayer.get(sender); - - return usender.getRole().getPrefix(); - } - -} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java b/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java index f31ff844..c5393a1a 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java @@ -1,7 +1,7 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Factions; -import com.massivecraft.factions.Rel; +import com.massivecraft.factions.Rank; import com.massivecraft.factions.cmd.req.ReqHasntFaction; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.FactionColl; @@ -70,8 +70,10 @@ public class CmdFactionsCreate extends FactionsCommand // Apply Faction faction = FactionColl.get().create(factionId); faction.setName(newName); + faction.setRanks(Rank.createDefaultRanks()); + faction.setRankManager(faction.getRankLeader()); - msender.setRole(Rel.LEADER); + msender.setRank(faction.getRankLeader()); msender.setFaction(faction); EventFactionsMembershipChange joinEvent = new EventFactionsMembershipChange(sender, msender, faction, MembershipChangeReason.CREATE); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java b/src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java index 994a14cd..6384082c 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java @@ -1,7 +1,7 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Perm; -import com.massivecraft.factions.Rel; +import com.massivecraft.factions.Rank; import com.massivecraft.factions.cmd.type.TypeFaction; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MPerm; @@ -50,19 +50,23 @@ public class CmdFactionsInviteList extends FactionsCommand { public String toString(MPlayer target, int index) { - // TODO: Madus would like to implement this in MPlayer + String targetName = target.getDisplayName(msender); String isAre = target == msender ? "are" : "is"; - Rel targetRank = target.getRole(); + Rank targetRank = target.getRank(); + boolean isLeader = targetRank.isLeader(); + Faction targetFaction = target.getFaction(); - String theAan = targetRank == Rel.LEADER ? "the" : Txt.aan(targetRank.name()); - String rankName = Txt.getNicedEnum(targetRank).toLowerCase(); - String ofIn = targetRank == Rel.LEADER ? "of" : "in"; + String rankName = targetRank.getName(); + String theAan = isLeader ? "the" : Txt.aan(rankName); + String ofIn = isLeader ? "of" : "in"; + String factionName = targetFaction.describeTo(msender, true); if (targetFaction == msenderFaction) { factionName = factionName.toLowerCase(); } + return Txt.parse("%s %s %s %s %s %s.", targetName, isAre, theAan, rankName, ofIn, factionName); } }); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsKick.java b/src/com/massivecraft/factions/cmd/CmdFactionsKick.java index c1e22716..369fd1db 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsKick.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsKick.java @@ -1,7 +1,6 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Factions; -import com.massivecraft.factions.Rel; import com.massivecraft.factions.cmd.type.TypeMPlayer; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.FactionColl; @@ -44,17 +43,17 @@ public class CmdFactionsKick extends FactionsCommand return; } - if (mplayer.getRole() == Rel.LEADER && !msender.isOverriding()) + if (mplayer.getRank().isLeader() && !msender.isOverriding()) { throw new MassiveException().addMsg("The leader cannot be kicked."); } - if (mplayer.getRole().isMoreThan(msender.getRole()) && ! msender.isOverriding()) + if (mplayer.getRank().isHigherThan(msender.getRank()) && ! msender.isOverriding()) { throw new MassiveException().addMsg("You can't kick people of higher rank than yourself."); } - if (mplayer.getRole() == msender.getRole() && ! msender.isOverriding()) + if (mplayer.getRank().equals(msender.getRank())&& ! msender.isOverriding()) { throw new MassiveException().addMsg("You can't kick people of the same rank as yourself."); } @@ -88,7 +87,7 @@ public class CmdFactionsKick extends FactionsCommand } // Apply - if (mplayer.getRole() == Rel.LEADER) + if (mplayer.getRank().isLeader()) { mplayerFaction.promoteNewLeader(); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java index d97feaf4..dc75443f 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java @@ -1,5 +1,9 @@ package com.massivecraft.factions.cmd; +import java.util.ArrayList; +import java.util.List; + +import com.massivecraft.factions.PermissibleId; import com.massivecraft.factions.Rel; import com.massivecraft.factions.cmd.type.TypeFaction; import com.massivecraft.factions.cmd.type.TypeMPerm; @@ -59,20 +63,23 @@ public class CmdFactionsPermSet extends FactionsCommand if (event.isCancelled()) return; value = event.getNewValue(); + // TODO: Change and use a TypePermissibleID or something like it + String permissibleId = rel.getName().toLowerCase(); + // No change - if (faction.getPermitted(perm).contains(rel) == value) + if (faction.getPermitted(perm).contains(permissibleId) == value) { msg("%s already has %s set to %s for %s.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "YES" : "NOO"), rel.getColor() + rel.getDescPlayerMany()); return; } // Apply - faction.setRelationPermitted(perm, rel, value); + faction.setRelationPermitted(perm, permissibleId, value); // The following is to make sure the leader always has the right to change perms if that is our goal. - if (perm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains(Rel.LEADER)) + if (perm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains(PermissibleId.LEADER)) { - faction.setRelationPermitted(MPerm.getPermPerms(), Rel.LEADER, true); + faction.setRelationPermitted(MPerm.getPermPerms(), PermissibleId.LEADER, true); } // Create messages diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRank.java b/src/com/massivecraft/factions/cmd/CmdFactionsRank.java index b9e0098a..eb44bd62 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsRank.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRank.java @@ -1,51 +1,18 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.Factions; -import com.massivecraft.factions.Perm; -import com.massivecraft.factions.Rel; -import com.massivecraft.factions.cmd.type.TypeFaction; -import com.massivecraft.factions.cmd.type.TypeMPlayer; -import com.massivecraft.factions.cmd.type.TypeRank; -import com.massivecraft.factions.entity.Faction; -import com.massivecraft.factions.entity.MConf; -import com.massivecraft.factions.entity.MFlag; -import com.massivecraft.factions.entity.MPlayer; -import com.massivecraft.factions.entity.MPlayerColl; -import com.massivecraft.factions.event.EventFactionsMembershipChange; -import com.massivecraft.factions.event.EventFactionsMembershipChange.MembershipChangeReason; -import com.massivecraft.factions.event.EventFactionsRankChange; -import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.util.Txt; - -import java.util.HashSet; -import java.util.Set; - public class CmdFactionsRank extends FactionsCommand { - // -------------------------------------------- // - // CONSTANTS - // -------------------------------------------- // - - // The rank required to do any rank changes. - final static Rel rankReq = Rel.OFFICER; - // -------------------------------------------- // // FIELDS // -------------------------------------------- // - // These fields are set upon perform() and unset afterwards. - // Target - private Faction targetFaction = null; - private MPlayer target = null; - - // End faction (the faction they are changed to) - private Faction endFaction = null; - private boolean factionChange = false; - - // Ranks - private Rel senderRank = null; - private Rel targetRank = null; - private Rel rank = null; + public CmdFactionsRankList cmdFactionsRankList = new CmdFactionsRankList(); + public CmdFactionsRankShow cmdFactionsRankShow = new CmdFactionsRankShow(); + public CmdFactionsRankCreate cmdFactionsRankCreate = new CmdFactionsRankCreate(); + public CmdFactionsRankName cmdFactionsRankName = new CmdFactionsRankName(); + public CmdFactionsRankOrder cmdFactionsRankOrder = new CmdFactionsRankOrder(); + public CmdFactionsRankDelete cmdFactionsRankDelete = new CmdFactionsRankDelete(); + public CmdFactionsRankSet cmdFactionsRankSet = new CmdFactionsRankSet(); // -------------------------------------------- // // CONSTRUCT @@ -53,341 +20,14 @@ public class CmdFactionsRank extends FactionsCommand public CmdFactionsRank() { - // Parameters - this.addParameter(TypeMPlayer.get(), "player"); - this.addParameter(TypeRank.get(), "action", "show"); - this.addParameter(TypeFaction.get(), "faction", "their"); - } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public void perform() throws MassiveException - { - // This sets target and much other. - this.registerFields(); - - // Sometimes we just want to show the rank. - if ( ! this.argIsSet(1)) - { - if ( ! Perm.RANK_SHOW.has(sender, true)) return; - this.showRank(); - return; - } - - // Permission check. - if ( ! Perm.RANK_ACTION.has(sender, true)) return; - - // Is the player allowed or not. Method can be found later down. - this.ensureAllowed(); - - if (factionChange) - { - this.changeFaction(); - } - - // Does the change make sense. - this.ensureMakesSense(); - - // Event - EventFactionsRankChange event = new EventFactionsRankChange(sender, target, rank); - event.run(); - if (event.isCancelled()) return; - rank = event.getNewRank(); - - // Change the rank. - this.changeRank(); - } - - // This is always run after performing a MassiveCommand. - @Override - public void senderFields(boolean set) - { - super.senderFields(set); - - if ( ! set) - { - this.unregisterFields(); - } - } - - // -------------------------------------------- // - // PRIVATE: REGISTER & UNREGISTER - // -------------------------------------------- // - - private void registerFields() throws MassiveException - { - // Getting the target and faction. - target = this.readArg(msender); - targetFaction = target.getFaction(); - - - // Ranks - senderRank = msender.getRole(); - targetRank = target.getRole(); - - // Rank if any passed. - if (this.argIsSet(1)) - { - this.setParameterType(1, TypeRank.get(targetRank)); - rank = this.readArg(); - } - - // Changing peoples faction. - endFaction = this.readArgAt(2, targetFaction); - factionChange = (endFaction != targetFaction); - - } - - private void unregisterFields() - { - targetFaction = null; - target = null; - - senderRank = null; - targetRank = null; - rank = null; - } - - // -------------------------------------------- // - // PRIVATE: ENSURE - // -------------------------------------------- // - - private void ensureAllowed() throws MassiveException - { - // People with permission don't follow the normal rules. - if (msender.isOverriding()) return; - - // If somone gets the leadership of wilderness (Which has happened before). - // We can at least try to limit their powers. - if (endFaction.isNone()) - { - throw new MassiveException().addMsg("%s doesn't use ranks sorry :(", targetFaction.getName() ); - } - - if (target == msender) - { - // Don't change your own rank. - throw new MassiveException().addMsg("The target player mustn't be yourself."); - } - - if (targetFaction != msenderFaction) - { - // Don't change ranks outside of your faction. - throw new MassiveException().addMsg("%s is not in the same faction as you.", target.describeTo(msender, true)); - } - - if (factionChange) - { - // Don't change peoples faction - throw new MassiveException().addMsg("You can't change %s's faction.", target.describeTo(msender)); - } - - if (senderRank.isLessThan(rankReq)) - { - // You need a specific rank to change ranks. - throw new MassiveException().addMsg("You must be %s or higher to change ranks.", Txt.getNicedEnum(rankReq).toLowerCase()); - } - - // The following two if statements could be merged. - // But isn't for the sake of nicer error messages. - if (senderRank == targetRank) - { - // You can't change someones rank if it is equal to yours. - throw new MassiveException().addMsg("%s can't manage eachother.", Txt.getNicedEnum(rankReq)+"s"); - } - - if (senderRank.isLessThan(targetRank)) - { - // You can't change someones rank if it is higher than yours. - throw new MassiveException().addMsg("You can't manage people of higher rank."); - } - - // The following two if statements could be merged. - // But isn't for the sake of nicer error messages. - if (senderRank == rank && senderRank != Rel.LEADER) - { - // You can't set ranks equal to your own. Unless you are the leader. - throw new MassiveException().addMsg("You can't set ranks equal to your own."); - } - - if (senderRank.isLessThan(rank)) - { - // You can't set ranks higher than your own. - throw new MassiveException().addMsg("You can't set ranks higher than your own."); - } - } - - private void ensureMakesSense() throws MassiveException - { - // Don't change their rank to something they already are. - if (target.getRole() == rank) - { - throw new MassiveException().addMsg("%s is already %s.", target.describeTo(msender), rank.getDescPlayerOne()); - } - } - - // -------------------------------------------- // - // PRIVATE: SHOW - // -------------------------------------------- // - - private void showRank() - { - // Damn you grammar, causing all these checks. - String targetName = target.describeTo(msender, true); - String isAre = (target == msender) ? "are" : "is"; // "you are" or "he is" - String theAan = (targetRank == Rel.LEADER) ? "the" : Txt.aan(targetRank.name()); // "a member", "an officer" or "the leader" - String rankName = Txt.getNicedEnum(targetRank).toLowerCase(); - String ofIn = (targetRank == Rel.LEADER) ? "of" : "in"; // "member in" or "leader of" - String factionName = targetFaction.describeTo(msender, true); - if (targetFaction == msenderFaction) - { - // Having the "Y" in "Your faction" being uppercase in the middle of a sentence makes no sense. - factionName = factionName.toLowerCase(); - } - if (targetFaction.isNone()) - { - // Wilderness aka none doesn't use ranks - msg("%s %s factionless", targetName, isAre); - } - else - { - // Derp is a member in Faction - msg("%s %s %s %s %s %s.", targetName, isAre, theAan, rankName, ofIn, factionName); - } - } - - // -------------------------------------------- // - // PRIVATE: CHANGE FACTION - // -------------------------------------------- // - - private void changeFaction() throws MassiveException - { - // Don't change a leader to a new faction. - if (targetRank == Rel.LEADER) - { - throw new MassiveException().addMsg("You cannot remove the present leader. Demote them first."); - } - - // Event - EventFactionsMembershipChange membershipChangeEvent = new EventFactionsMembershipChange(sender, msender, endFaction, MembershipChangeReason.RANK); - membershipChangeEvent.run(); - if (membershipChangeEvent.isCancelled()) throw new MassiveException(); - - // Apply - target.resetFactionData(); - target.setFaction(endFaction); - - // No longer invited. - endFaction.setInvited(target, false); - - // Create recipients - Set recipients = new HashSet<>(); - recipients.addAll(targetFaction.getMPlayersWhereOnline(true)); - recipients.addAll(endFaction.getMPlayersWhereOnline(true)); - recipients.add(msender); - - // Send message - for (MPlayer recipient : recipients) - { - recipient.msg("%s was moved from %s to %s.", target.describeTo(recipient), targetFaction.describeTo(recipient), endFaction.describeTo(recipient)); - } - - // Derplog - if (MConf.get().logFactionJoin) - { - Factions.get().log(Txt.parse("%s moved %s from %s to %s.", msender.getName(), target.getName(), targetFaction.getName(), endFaction.getName())); - } - - // Now we don't need the old values. - targetFaction = target.getFaction(); - targetRank = target.getRole(); - senderRank = msender.getRole(); // In case they changed their own rank - } - - // -------------------------------------------- // - // PRIVATE: CHANGE RANK - // -------------------------------------------- // - - private void changeRank() throws MassiveException - { - // In case of leadership change, we do special things not done in other rank changes. - if (rank == Rel.LEADER) - { - this.changeRankLeader(); - } - else - { - this.changeRankOther(); - } - } - - private void changeRankLeader() - { - // If there is a current leader. Demote & inform them. - MPlayer targetFactionCurrentLeader = targetFaction.getLeader(); - if (targetFactionCurrentLeader != null) - { - // Inform & demote the old leader. - targetFactionCurrentLeader.setRole(Rel.OFFICER); - if (targetFactionCurrentLeader != msender) - { - // They kinda know if they fired the command themself. - targetFactionCurrentLeader.msg("You have been demoted from the position of faction leader by %s.", msender.describeTo(targetFactionCurrentLeader, true)); - } - } - - // Promote the new leader. - target.setRole(Rel.LEADER); - - // Inform everyone, this includes sender and target. - for (MPlayer recipient : MPlayerColl.get().getAllOnline()) - { - String changerName = senderIsConsole ? "A server admin" : msender.describeTo(recipient); - recipient.msg("%s gave %s the leadership of %s.", changerName, target.describeTo(recipient), targetFaction.describeTo(recipient)); - } - } - - private void changeRankOther() throws MassiveException - { - // If the target is currently the leader and faction isn't permanent a new leader should be promoted. - // Sometimes a bug occurs and multiple leaders exist. Then we should be able to demote without promoting new leader - if (targetRank == Rel.LEADER && ( ! MConf.get().permanentFactionsDisableLeaderPromotion || ! targetFaction.getFlag(MFlag.ID_PERMANENT)) && targetFaction.getMPlayersWhereRole(Rel.LEADER).size() == 1) - // This if statement is very long. Should I nest it for readability? - { - targetFaction.promoteNewLeader(); // This might disband the faction. - - // So if the faction disbanded... - if (targetFaction.detached()) - { - // ... we inform the sender. - target.resetFactionData(); - throw new MassiveException().addMsg("The target was a leader and got demoted. The faction disbanded and no rank was set."); - } - } - - // Create recipients - Set recipients = new HashSet<>(); - recipients.addAll(targetFaction.getMPlayers()); - recipients.add(msender); - - // Were they demoted or promoted? - String change = (rank.isLessThan(targetRank) ? "demoted" : "promoted"); - - // The rank will be set before the msg, so they have the appropriate prefix. - target.setRole(rank); - String oldRankName = Txt.getNicedEnum(targetRank).toLowerCase(); - String rankName = Txt.getNicedEnum(rank).toLowerCase(); - - // Send message - for(MPlayer recipient : recipients) - { - String targetName = target.describeTo(recipient, true); - String wasWere = (recipient == target) ? "were" : "was"; - recipient.msg("%s %s %s from %s to %s in %s.", targetName, wasWere, change, oldRankName, rankName, targetFaction.describeTo(msender)); - } + // Children + this.addChild(this.cmdFactionsRankList); + this.addChild(this.cmdFactionsRankShow); + this.addChild(this.cmdFactionsRankCreate); + this.addChild(this.cmdFactionsRankDelete); + this.addChild(this.cmdFactionsRankName); + this.addChild(this.cmdFactionsRankOrder); + this.addChild(this.cmdFactionsRankSet); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankCreate.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankCreate.java new file mode 100644 index 00000000..99c2ef89 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankCreate.java @@ -0,0 +1,46 @@ +package com.massivecraft.factions.cmd; + +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.cmd.type.TypeRank; +import com.massivecraft.factions.cmd.type.TypeRankName; +import com.massivecraft.massivecore.MassiveException; + +public class CmdFactionsRankCreate extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsRankCreate() + { + // Parameters + this.addParameter(TypeRankName.getStrict(), "name"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + String name = this.readArg(); + + // Create + Rank rank = msenderFaction.createRank(name); + + // Add + msenderFaction.addRank(rank); + + // Inform + message(mson( + "The rank ", + TypeRank.get().getVisualMson(rank), + " has been created." + ).color(ChatColor.YELLOW)); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankDelete.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankDelete.java new file mode 100644 index 00000000..19bb0312 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankDelete.java @@ -0,0 +1,69 @@ +package com.massivecraft.factions.cmd; + +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.cmd.type.TypeRank; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.type.primitive.TypeBooleanTrue; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsRankDelete extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsRankDelete() + { + // Parameters + this.addParameter(TypeRank.get(), "rank"); + this.addParameter(TypeBooleanTrue.get(), "ensure"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + Rank rank = this.readArg(); + boolean ensure = this.readArg(false); + + // Ensure + if (!ensure) throw new MassiveException().setMsg("You must ensure to delete the rank."); + + // Delete + boolean success = msenderFaction.removeRank(rank); + + // On Success + if (success) this.demoteMembersWithRank(rank); + + // Inform + message(mson( + "The Rank ", + TypeRank.get().getVisualMson(rank), + success ? " could not be " : " has been ", + "removed." + ).color(success ? ChatColor.GREEN : ChatColor.RED)); + } + + private void demoteMembersWithRank(Rank rank) + { + // Prepare + String visualRankOld = TypeRank.get().getVisual(rank); + String visualRankNew = TypeRank.get().getVisual(msenderFaction.getNextLowerRank(rank)); + String message = Txt.parse("You have been demoted from %s to %s, because your rank has been deleted.", visualRankOld, visualRankNew); + + // Demote & Inform + for (MPlayer mplayer : msenderFaction.getMPlayersWhereRank(rank)) + { + msenderFaction.demote(mplayer); + mplayer.msg(message); + } + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankList.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankList.java new file mode 100644 index 00000000..09cd8bcf --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankList.java @@ -0,0 +1,79 @@ +package com.massivecraft.factions.cmd; + +import java.util.Collection; +import java.util.List; + +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.cmd.type.TypeFaction; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.Parameter; +import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.pager.Msonifier; +import com.massivecraft.massivecore.pager.Pager; +import com.massivecraft.massivecore.util.MUtil; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsRankList extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsRankList() + { + // Parameters + this.addParameter(Parameter.getPage()); + this.addParameter(TypeFaction.get(), "faction", "their"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + int page = this.readArg(); + Faction faction = this.readArg(msenderFaction); + + // Are we trying to show other? + this.tryShowOther(); + + // Pager Prepare + Collection ranks = faction.getRankCollection(); + String title = Txt.parse("Ranks for %s", faction.describeTo(msender)); + List args = MUtil.list(String.valueOf(page), faction.getName()); + + // Pager Create + final Pager pager = new Pager<>(this, title, page, ranks, new Msonifier() + { + @Override + public Mson toMson(Rank rank, int index) + { + return rank.visualize(); + } + }); + + // Pager Args + pager.setArgs(args); + + // Pager message + pager.messageAsync(); + } + + private void tryShowOther() throws MassiveException + { + // Are we trying to show other? + if (!this.argIsSet(0)) return; + + // Are we overriding? + if (msender.isOverriding()) return; + + // Throw if they don't have the permission to show + Perm.RANK_SHOW.hasThrow(sender, true); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankName.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankName.java new file mode 100644 index 00000000..c606372c --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankName.java @@ -0,0 +1,65 @@ +package com.massivecraft.factions.cmd; + +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.cmd.type.TypeRank; +import com.massivecraft.factions.cmd.type.TypeRankName; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.mson.Mson; + +public class CmdFactionsRankName extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsRankName() + { + // Parameters + this.addParameter(TypeRank.get(), "rank"); + this.addParameter(TypeRankName.getLenient(), "name", "show"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + Rank rank = this.readArg(); + Rank oldRank = rank.copy(); + + // Are we showing? + if (!this.argIsSet(1)) + { + this.inform(rank, false); + return; + } + + String name = this.readArg(); + + // Change + rank.setName(name); + + // Add + msenderFaction.addRank(rank); + + // Inform + this.inform(oldRank, true); + } + + private void inform(Rank rank, boolean updated) + { + message(mson( + "The rank name of ", + TypeRank.get().getVisualMson(rank), + " is ", + updated ? "now " : Mson.EMPTY, + mson(String.valueOf(rank.getOrder())).color(ChatColor.LIGHT_PURPLE) + ).color(ChatColor.YELLOW)); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankOld.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankOld.java index 0dfa072c..6e596de9 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsRankOld.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankOld.java @@ -1,6 +1,5 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.cmd.type.TypeFaction; import com.massivecraft.factions.cmd.type.TypeMPlayer; import com.massivecraft.massivecore.command.Visibility; import com.massivecraft.massivecore.util.MUtil; @@ -28,7 +27,6 @@ public class CmdFactionsRankOld extends FactionsCommand // Parameters this.addParameter(TypeMPlayer.get(), "player"); - this.addParameter(TypeFaction.get(), "faction", "their"); // Visibility this.setVisibility(Visibility.INVISIBLE); @@ -41,10 +39,9 @@ public class CmdFactionsRankOld extends FactionsCommand @Override public void perform() { - CmdFactions.get().cmdFactionsRank.execute(sender, MUtil.list( + CmdFactions.get().cmdFactionsRank.cmdFactionsRankSet.execute(sender, MUtil.list( this.argAt(0), - this.rankName, - this.argAt(1) + this.rankName )); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankOrder.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankOrder.java new file mode 100644 index 00000000..41243aee --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankOrder.java @@ -0,0 +1,66 @@ +package com.massivecraft.factions.cmd; + +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.cmd.type.TypeRank; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.type.primitive.TypeInteger; +import com.massivecraft.massivecore.mson.Mson; + +public class CmdFactionsRankOrder extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsRankOrder() + { + // Parameters + this.addParameter(TypeRank.get(), "rank"); + this.addParameter(TypeInteger.get(), "order", "show"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + Rank rank = this.readArg(); + + // Are we showing? + if (!this.argIsSet(1)) + { + this.inform(rank, false); + return; + } + + int order = this.readArg(); + Faction faction = msenderFaction; + + // Already this rank? + if (rank.getOrder() == order) throw new MassiveException().setMsg("Rank order for %s is already %d.", rank.getName(), rank.getOrder()); + + // Adjust rank + faction.adjustRankOrder(rank, order); + + // Inform + this.inform(rank, true); + } + + private void inform(Rank rank, boolean updated) + { + message(mson( + "The rank order of ", + TypeRank.get().getVisualMson(rank), + " is ", + updated ? "now " : Mson.EMPTY, + mson(String.valueOf(rank.getOrder())).color(ChatColor.LIGHT_PURPLE) + ).color(ChatColor.YELLOW)); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java new file mode 100644 index 00000000..6a64981e --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java @@ -0,0 +1,226 @@ +package com.massivecraft.factions.cmd; + +import java.util.Set; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.cmd.type.TypeMPlayer; +import com.massivecraft.factions.cmd.type.TypeRank; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.entity.MFlag; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.entity.MPlayerColl; +import com.massivecraft.factions.event.EventFactionsRankChange; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.MassiveSet; +import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsRankSet extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTANT + // -------------------------------------------- // + + private static final String MESSAGE_FORMAT = "%s Rank has been set to %s."; + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsRankSet() + { + // Parameters + this.addParameter(TypeMPlayer.get(), "player"); + this.addParameter(TypeRank.get(), "rank"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + MPlayer mplayer = this.readArg(); + Rank rank = this.readArg(); + + // Make sure sender can set this rank + this.ensureRankSet(mplayer, rank); + this.ensureRankUpdate(mplayer, rank); + rank = this.ensureRankChangeEvent(mplayer, rank); + + // Set Rank + this.setRank(mplayer, rank); + + // Prepare inform + boolean same = mplayer == msender; + String visualRank = TypeRank.get().getVisual(rank); + String visualPlayer = mplayer.describeTo(msender); + if (same) visualPlayer += "r"; + + // Inform + msg(MESSAGE_FORMAT, visualPlayer, visualRank); + if (!same) mplayer.msg(MESSAGE_FORMAT, mplayer.describeTo(mplayer), visualRank); + } + + // -------------------------------------------- // + // ENSURE + // -------------------------------------------- // + + private void ensureRankSet(MPlayer mplayer, Rank rank) throws MassiveException + { + // People with permission don't follow the normal rules. + if (msender.isOverriding()) return; + + // Prepare + MassiveException exception = new MassiveException(); + Faction faction = mplayer.getFaction(); + String describe = mplayer.describeTo(msender, true); + Rank rankPlayer = mplayer.getRank(); + Rank rankSender = msender.getRank(); + Rank rankManager = faction.getRankManager(); + String rankManagerName = rankManager.getName(); + + boolean isPermanent = faction.getFlag(MFlag.ID_PERMANENT); + boolean isPermanentDemoteDisabled = MConf.get().permanentFactionsDisableLeaderPromotion; + + // Can't change rank in Wilderness + if (faction.isNone()) throw exception.addMsg("%s doesn't use ranks, sorry :(.", faction.getName()); + + // Don't change your own rank. + if (mplayer == msender) throw exception.addMsg("The target player mustn't be yourself."); + + // Don't change ranks outside of your faction. + if (faction != msenderFaction) throw exception.addMsg("%s is not in the same faction as you.", describe); + + // You need to be rank manager to change ranks. + if (msender.getRank().isLessThan(rankManager)) throw exception.addMsg("You must be %s or higher to change ranks.", rankManagerName); + + // You can't change someones rank if it is equal to yours. + if (rankPlayer == rank) throw exception.addMsg("%s can't manage eachother.", rankManagerName + "s"); + + // You can't change someones rank if it is higher than yours. + if (rankPlayer.isLessThan(rank)) throw exception.addMsg("You can't manage people of higher rank."); + + // You can't set ranks equal to your own. Unless you are the leader. + if (rankSender == rank && !rankSender.isLeader()) throw exception.addMsg("You can't set ranks equal to your own."); + + // You can't promote new leaders for permanent factions if its disabled + if (rank.isLeader() && isPermanent && isPermanentDemoteDisabled) throw exception.setMsg("You can't promote a new leader for permanent factions."); + } + + private void ensureRankUpdate(MPlayer mplayer, Rank rank) throws MassiveException + { + // Don't change their rank to something they already are. + if (!mplayer.getRank().equals(rank)) return; + + // Prepare + String describe = mplayer.describeTo(msender); + String name = rank.getName(); + String aan = Txt.aan(name); + + // Inform + throw new MassiveException().addMsg("%s is already %s %s.", describe, aan, name); + } + + private Rank ensureRankChangeEvent(MPlayer mplayer, Rank rank) throws MassiveException + { + // Create and Run + EventFactionsRankChange event = new EventFactionsRankChange(sender, mplayer, rank); + event.run(); + + // Check + if (event.isCancelled()) throw new MassiveException().setMsg("Another plugin has prohibited the rank change."); + + // Return + return event.getNewRank(); + } + + // -------------------------------------------- // + // SET RANK + // -------------------------------------------- // + + private void setRank(MPlayer mplayer, Rank rank) throws MassiveException + { + if (rank.isLeader()) + { + this.setRankLeader(mplayer, rank); + } + else + { + this.setRankOther(mplayer, rank); + } + } + + private void setRankLeader(MPlayer mplayer, Rank rank) throws MassiveException + { + Faction faction = mplayer.getFaction(); + MPlayer leaderCurrent = faction.getLeader(); + if (leaderCurrent != null) + { + faction.demote(leaderCurrent); + if (leaderCurrent != msender) + { + String visualSender = msender.describeTo(leaderCurrent, true); + leaderCurrent.msg("You have been demoted from the position of faction leader by %s.", visualSender); + } + } + + // NOTE: Set before inform for prefix change + mplayer.setRank(rank); + + // Inform everyone, this includes sender and target. + for (MPlayer recipient : MPlayerColl.get().getAllOnline()) + { + String changerName = senderIsConsole ? "A server admin" : msender.describeTo(recipient); + String visualSender = mplayer.describeTo(recipient); + String visualFaction = faction.describeTo(recipient); + String message = Txt.parse("%s gave %s the leadership of %s.", changerName, visualSender, visualFaction); + + recipient.message(message); + } + } + + private void setRankOther(MPlayer mplayer, Rank rank) throws MassiveException + { + Faction faction = mplayer.getFaction(); + Rank rankPlayer = mplayer.getRank(); + + // NOTE: There has been occasions where there were more than one leader + if (rankPlayer.isLeader() && faction.getMPlayersWhereLeader().size() == 1) + { + faction.promoteNewLeader(); + + // So if the faction disbanded... + if (faction.detached()) + { + // ... we inform the sender. + mplayer.resetFactionData(); + throw new MassiveException().addMsg("The target was a leader and got demoted. The faction disbanded and no rank was set."); + } + } + + // Get recipients + Set recipients = new MassiveSet<>(faction.getMPlayers()); + recipients.add(msender); + + // Prepare inform + String change = (mplayer.getRank().isLessThan(rank) ? "demoted" : "promoted"); + + // NOTE: Set before inform for prefix change + mplayer.setRank(rank); + Mson rankNameOld = TypeRank.get().getVisualMson(rankPlayer); + Mson rankNameNew = TypeRank.get().getVisualMson(rank); + + // Inform + for (MPlayer recipient : recipients) + { + String describe = mplayer.describeTo(recipient, true); + String wasWere = (recipient == mplayer) ? "were" : "was"; + recipient.msg("%s %s %s from %s to %s.", describe, wasWere, change, rankNameOld, rankNameNew); + } + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankShow.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankShow.java new file mode 100644 index 00000000..808013e1 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankShow.java @@ -0,0 +1,42 @@ +package com.massivecraft.factions.cmd; + +import java.util.List; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.cmd.type.TypeMPlayer; +import com.massivecraft.factions.cmd.type.TypeRank; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsRankShow extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsRankShow() + { + // Parameters + this.addParameter(TypeMPlayer.get(), "player"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + Rank rank = this.readArg(msender.getRank()); + + // Prepare + String title = Txt.parse("Rank show %s", TypeRank.get().getVisual(rank)); + List show = TypeRank.get().getShow(rank); + + // Inform + message(Txt.getPage(show, 0, title, sender)); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsTitle.java b/src/com/massivecraft/factions/cmd/CmdFactionsTitle.java index 9e3eb0bc..dedf6065 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsTitle.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsTitle.java @@ -44,7 +44,7 @@ public class CmdFactionsTitle extends FactionsCommand if ( ! MPerm.getPermTitle().has(msender, you.getFaction(), true)) return; // Rank Check - if (!msender.isOverriding() && you.getRole().isMoreThan(msender.getRole())) + if (!msender.isOverriding() && you.getRank().isHigherThan(msender.getRank())) { msg("You can not edit titles for higher ranks."); return; diff --git a/src/com/massivecraft/factions/cmd/req/ReqRoleIsAtLeast.java b/src/com/massivecraft/factions/cmd/req/ReqRoleIsAtLeast.java deleted file mode 100644 index e52eea71..00000000 --- a/src/com/massivecraft/factions/cmd/req/ReqRoleIsAtLeast.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.massivecraft.factions.cmd.req; - -import com.massivecraft.factions.Rel; -import com.massivecraft.factions.entity.MPlayer; -import com.massivecraft.massivecore.command.MassiveCommand; -import com.massivecraft.massivecore.command.requirement.RequirementAbstract; -import com.massivecraft.massivecore.util.MUtil; -import com.massivecraft.massivecore.util.Txt; -import org.bukkit.command.CommandSender; - -public class ReqRoleIsAtLeast extends RequirementAbstract -{ - // -------------------------------------------- // - // SERIALIZABLE - // -------------------------------------------- // - - private static final long serialVersionUID = 1L; - - // -------------------------------------------- // - // FIELDS - // -------------------------------------------- // - - private final Rel rel; - public Rel getRel() { return this.rel; } - - // -------------------------------------------- // - // INSTANCE & CONSTRUCT - // -------------------------------------------- // - - public static ReqRoleIsAtLeast get(Rel rel) { return new ReqRoleIsAtLeast(rel); } - private ReqRoleIsAtLeast(Rel rel) { this.rel = rel; } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public boolean apply(CommandSender sender, MassiveCommand command) - { - if (MUtil.isntSender(sender)) return false; - - MPlayer mplayer = MPlayer.get(sender); - return mplayer.getRole().isAtLeast(this.getRel()); - } - - @Override - public String createErrorMessage(CommandSender sender, MassiveCommand command) - { - return Txt.parse("You must be %s or higher to %s.", Txt.getNicedEnum(this.getRel()), getDesc(command)); - } - -} diff --git a/src/com/massivecraft/factions/cmd/type/TypeRank.java b/src/com/massivecraft/factions/cmd/type/TypeRank.java index 3885ae2a..72863391 100644 --- a/src/com/massivecraft/factions/cmd/type/TypeRank.java +++ b/src/com/massivecraft/factions/cmd/type/TypeRank.java @@ -1,18 +1,21 @@ package com.massivecraft.factions.cmd.type; -import com.massivecraft.factions.Rel; -import com.massivecraft.massivecore.collections.MassiveMap; -import com.massivecraft.massivecore.collections.MassiveSet; -import com.massivecraft.massivecore.command.type.enumeration.TypeEnum; -import com.massivecraft.massivecore.util.MUtil; - -import java.util.Collections; -import java.util.Iterator; +import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Set; -public class TypeRank extends TypeEnum +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.collections.MassiveSet; +import com.massivecraft.massivecore.command.type.TypeAbstract; +import com.massivecraft.massivecore.mson.Mson; + +public class TypeRank extends TypeAbstract { // -------------------------------------------- // // CONSTANTS @@ -31,98 +34,99 @@ public class TypeRank extends TypeEnum "Minus", "Down" ); - + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final Rank startRank; + public Rank getStartRank() { return this.startRank; } + + private Faction faction; + public Faction getFaction() { return this.faction; } + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // // Because of the caching in TypeAbstractChoice, we want only one of each instance. // Null instance, doesn't allow promote and demote. - private static final TypeRank i = new TypeRank(null); + private static final TypeRank i = new TypeRank(null, null); public static TypeRank get() { return i; } - // Cached instances, does allow promote and demote. - private static final Map instances; - static - { - Map result = new MassiveMap<>(); - for (Rel rel : Rel.values()) - { - if ( ! rel.isRank()) continue; - result.put(rel, new TypeRank(rel)); - } - result.put(null, i); - instances = Collections.unmodifiableMap(result); - } - public static TypeRank get(Rel rank) { return instances.get(rank); } - // Constructor - public TypeRank(Rel rank) + public TypeRank(Rank rank, Faction faction) { - super(Rel.class); - if (rank != null && ! rank.isRank()) throw new IllegalArgumentException(rank + " is not a valid rank"); + super(Rank.class); + this.startRank = rank; - - // Do setAll with only ranks. - List all = MUtil.list(Rel.values()); - for (Iterator it = all.iterator(); it.hasNext(); ) - { - if ( ! it.next().isRank()) it.remove(); - } - - this.setAll(all); + this.faction = faction; } - // -------------------------------------------- // - // FIELDS - // -------------------------------------------- // - - // This must be final, for caching in TypeAbstractChoice to work. - private final Rel startRank; - public Rel getStartRank() { return this.startRank; } - // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // @Override - public String getName() - { - return "rank"; - } - - @Override - public String getNameInner(Rel value) - { - return value.getName(); - } - - @Override - public Set getNamesInner(Rel value) + public Set getNamesInner(Rank rank) { // Create - Set ret = new MassiveSet<>(); - - // Fill Exact - ret.addAll(value.getNames()); + Set ret = new MassiveSet(); // Fill Relative - Rel start = this.getStartRank(); + Rank start = this.getStartRank(); if (start != null) { - if (value == Rel.LEADER && start == Rel.OFFICER) ret.addAll(NAMES_PROMOTE); + if (start.isHigherThan(rank)) ret.addAll(NAMES_PROMOTE); - if (value == Rel.OFFICER && start == Rel.MEMBER) ret.addAll(NAMES_PROMOTE); - if (value == Rel.OFFICER && start == Rel.LEADER) ret.addAll(NAMES_DEMOTE); - - if (value == Rel.MEMBER && start == Rel.RECRUIT) ret.addAll(NAMES_PROMOTE); - if (value == Rel.MEMBER && start == Rel.OFFICER) ret.addAll(NAMES_DEMOTE); - - if (value == Rel.RECRUIT && start == Rel.MEMBER) ret.addAll(NAMES_DEMOTE); + if (start.isLessThan(rank)) ret.addAll(NAMES_DEMOTE); } // Return return ret; } - + + @Override + public Rank read(String arg, CommandSender sender) throws MassiveException + { + return null; + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return null; + } + + @Override + public Mson getVisualMson(Rank value) + { + // Create + Mson mson = Mson.mson("[", value.getName(), "]"); + + // Fill + mson = mson.tooltip(Mson.toPlain(this.getShow(value), true)); + + // Return + return mson; + } + + @Override + public List getShowInner(Rank value, CommandSender sender) + { + Mson order = getShowLine("Order", value.getOrder()); + Mson prefix = getShowLine("Prefix", value.getPrefix()); + + return new MassiveList<>(order, prefix); + } + + private static Mson getShowLine(String key, Object value) + { + return Mson.mson( + Mson.mson(key).color(ChatColor.AQUA), + Mson.mson(":").color(ChatColor.GRAY), + Mson.SPACE, + value.toString() + ).color(ChatColor.YELLOW); + } } diff --git a/src/com/massivecraft/factions/cmd/type/TypeRankName.java b/src/com/massivecraft/factions/cmd/type/TypeRankName.java new file mode 100644 index 00000000..2d3cb51d --- /dev/null +++ b/src/com/massivecraft/factions/cmd/type/TypeRankName.java @@ -0,0 +1,68 @@ +package com.massivecraft.factions.cmd.type; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.Named; +import com.massivecraft.massivecore.command.type.TypeNameAbstract; + +public class TypeRankName extends TypeNameAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TypeRankName iStrict = new TypeRankName(true); + private static TypeRankName iLenient = new TypeRankName(false); + public static TypeRankName getStrict() { return iStrict; } + public static TypeRankName getLenient() { return iLenient; } + + private TypeRankName(boolean strict) + { + super(strict); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Integer getLengthMin() + { + return MConf.get().nameLengthRankMin; + } + + @Override + public Integer getLengthMax() + { + return MConf.get().nameLengthRankMax; + } + + @Override + public Named getCurrent(CommandSender sender, String arg) throws MassiveException + { + return TypeRank.get().read(arg, sender); + } + + @Override + public boolean isNameTaken(CommandSender sender, String name) throws MassiveException + { + MPlayer mplayer = MPlayer.get(sender); + if (mplayer == null) return false; + + // TODO: Adjust later for "used" faction + Faction faction = mplayer.getFaction(); + + for (Rank rank : faction.getRankCollection()) + { + if (!rank.getName().equalsIgnoreCase(name)) continue; + return true; + } + + return false; + } +} diff --git a/src/com/massivecraft/factions/comparator/ComparatorMPlayerRole.java b/src/com/massivecraft/factions/comparator/ComparatorMPlayerRole.java index 874f32f2..01b6d52e 100644 --- a/src/com/massivecraft/factions/comparator/ComparatorMPlayerRole.java +++ b/src/com/massivecraft/factions/comparator/ComparatorMPlayerRole.java @@ -1,11 +1,11 @@ package com.massivecraft.factions.comparator; -import com.massivecraft.factions.Rel; +import java.util.Comparator; + +import com.massivecraft.factions.Rank; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.massivecore.Named; -import java.util.Comparator; - public class ComparatorMPlayerRole implements Comparator, Named { // -------------------------------------------- // @@ -38,11 +38,9 @@ public class ComparatorMPlayerRole implements Comparator, Named else if (m2 == null) return +1; // Rank - Rel r1 = m1.getRole(); - Rel r2 = m2.getRole(); - return r2.getValue() - r1.getValue(); + Rank r1 = m1.getRank(); + Rank r2 = m2.getRank(); + return r2.getOrder() - r1.getOrder(); } - - - + } diff --git a/src/com/massivecraft/factions/engine/EnginePlayerData.java b/src/com/massivecraft/factions/engine/EnginePlayerData.java index b225d057..2393ea89 100644 --- a/src/com/massivecraft/factions/engine/EnginePlayerData.java +++ b/src/com/massivecraft/factions/engine/EnginePlayerData.java @@ -1,15 +1,15 @@ package com.massivecraft.factions.engine; -import com.massivecraft.factions.Rel; -import com.massivecraft.factions.entity.MConf; -import com.massivecraft.factions.entity.MPlayer; -import com.massivecraft.factions.entity.MPlayerColl; -import com.massivecraft.massivecore.Engine; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerKickEvent; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.entity.MPlayerColl; +import com.massivecraft.massivecore.Engine; + public class EnginePlayerData extends Engine { // -------------------------------------------- // @@ -40,7 +40,7 @@ public class EnginePlayerData extends Engine MPlayer mplayer = MPlayerColl.get().get(player, false); if (mplayer == null) return; - if (mplayer.getRole() == Rel.LEADER) + if (mplayer.getRank().isLeader()) { mplayer.getFaction().promoteNewLeader(); } diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 693518ac..4ad84b1f 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -1,12 +1,25 @@ package com.massivecraft.factions.entity; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + import com.massivecraft.factions.Factions; import com.massivecraft.factions.FactionsIndex; import com.massivecraft.factions.FactionsParticipator; +import com.massivecraft.factions.PermissionIdentifiable; +import com.massivecraft.factions.PredicateRank; +import com.massivecraft.factions.Rank; import com.massivecraft.factions.Rel; import com.massivecraft.factions.RelationParticipator; import com.massivecraft.factions.predicate.PredicateCommandSenderFaction; -import com.massivecraft.factions.predicate.PredicateMPlayerRole; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.massivecore.collections.MassiveList; @@ -25,18 +38,6 @@ import com.massivecraft.massivecore.store.SenderColl; import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; public class Faction extends Entity implements FactionsParticipator { @@ -73,6 +74,7 @@ public class Faction extends Entity implements FactionsParticipator this.setRelationWishes(that.relationWishes); this.setFlagIds(that.flags); this.setPermIds(that.perms); + this.setRanks(that.ranks); return this; } @@ -87,7 +89,7 @@ public class Faction extends Entity implements FactionsParticipator if (Money.exists(this)) { // ... remove it. - Money.set(this, null, 0); + Money.set(this, null, 0); } } @@ -144,10 +146,15 @@ public class Faction extends Entity implements FactionsParticipator // The flag overrides are modifications to the default values. // Null means default. private MassiveMapDef flags = new MassiveMapDef<>(); - + // The perm overrides are modifications to the default values. // Null means default. - private MassiveMapDef> perms = new MassiveMapDef<>(); + private MassiveMapDef> perms = new MassiveMapDef<>(); + + private MassiveMapDef ranks = new MassiveMapDef<>(); + //private MassiveTreeSetDef ranks = new MassiveTreeSetDef<>(ComparatorPrioritized.get()); + + private Rank rankManager; // -------------------------------------------- // // FIELD: id @@ -162,7 +169,7 @@ public class Faction extends Entity implements FactionsParticipator public boolean isNormal() { - return ! this.isNone(); + return !this.isNone(); } // -------------------------------------------- // @@ -191,7 +198,7 @@ public class Faction extends Entity implements FactionsParticipator // Detect Nochange if (MUtil.equals(this.name, target)) return; - + // Apply this.name = target; @@ -249,7 +256,7 @@ public class Faction extends Entity implements FactionsParticipator // Detect Nochange if (MUtil.equals(this.description, target)) return; - + // Apply this.description = target; @@ -289,7 +296,7 @@ public class Faction extends Entity implements FactionsParticipator // Detect Nochange if (MUtil.equals(this.motd, target)) return; - + // Apply this.motd = target; @@ -334,7 +341,7 @@ public class Faction extends Entity implements FactionsParticipator // Detect Nochange if (MUtil.equals(this.createdAtMillis, createdAtMillis)) return; - + // Apply this.createdAtMillis = target; @@ -509,7 +516,7 @@ public class Faction extends Entity implements FactionsParticipator List mplayers = new MassiveList<>(); for (String id : this.getInvitedPlayerIds()) - { + { MPlayer mplayer = MPlayer.get(id); mplayers.add(mplayer); } @@ -599,7 +606,7 @@ public class Faction extends Entity implements FactionsParticipator Entry entry = iter.next(); // ... extract id and remove null values ... - String id = entry.getKey(); + String id = entry.getKey(); if (id == null) { iter.remove(); @@ -642,7 +649,7 @@ public class Faction extends Entity implements FactionsParticipator target.put(key, value); } - + // Detect Nochange if (MUtil.equals(this.flags, target)) return; @@ -707,62 +714,22 @@ public class Faction extends Entity implements FactionsParticipator // -------------------------------------------- // // RAW - - public Map> getPerms() + public MassiveMapDef> getPermIds() { - // We start with default values ... - Map> ret = new MassiveMap<>(); - for (MPerm mperm : MPerm.getAll()) - { - ret.put(mperm, new MassiveSet<>(mperm.getStandard())); - } - - // ... and if anything is explicitly set we use that info ... - Iterator>> iter = this.perms.entrySet().iterator(); - while (iter.hasNext()) - { - // ... for each entry ... - Entry> entry = iter.next(); - - // ... extract id and remove null values ... - String id = entry.getKey(); - if (id == null) - { - iter.remove(); - continue; - } - - // ... resolve object and skip unknowns ... - MPerm mperm = MPerm.get(id); - if (mperm == null) continue; - - ret.put(mperm, new MassiveSet<>(entry.getValue())); - } - - return ret; + return this.perms; } - public void setPerms(Map> perms) - { - Map> permIds = new MassiveMap<>(); - for (Entry> entry : perms.entrySet()) - { - permIds.put(entry.getKey().getId(), entry.getValue()); - } - setPermIds(permIds); - } - - public void setPermIds(Map> perms) + public void setPermIds(MassiveMapDef> perms) { // Clean input - MassiveMapDef> target = new MassiveMapDef<>(); - for (Entry> entry : perms.entrySet()) + MassiveMapDef> target = new MassiveMapDef>(); + for (Entry> entry : perms.entrySet()) { String key = entry.getKey(); if (key == null) continue; key = key.toLowerCase(); // Lowercased Keys Version 2.6.0 --> 2.7.0 - Set value = entry.getValue(); + Set value = entry.getValue(); if (value == null) continue; target.put(key, value); @@ -778,54 +745,78 @@ public class Faction extends Entity implements FactionsParticipator this.changed(); } - // FINER + // Finer + public Map> getPerms() + { + // We start with default values ... + Map> ret = new MassiveMap<>(); + for (MPerm mperm : MPerm.getAll()) + { + ret.put(mperm, new MassiveSet<>(mperm.getStandard())); + } + + // ... and if anything is explicitly set we use that info ... + for (Iterator>> it = this.perms.entrySet().iterator(); it.hasNext(); ) + { + // ... for each entry ... + Entry> entry = it.next(); + + // ... extract id and remove null values ... + String id = entry.getKey(); + if (id == null) + { + it.remove(); + continue; + } + + // ... resolve object and skip unknowns ... + MPerm mperm = MPerm.get(id); + if (mperm == null) continue; + + ret.put(mperm, new MassiveSet<>(entry.getValue())); + } + + return ret; + } - public boolean isPermitted(String permId, Rel rel) + public void setPerms(Map> perms) + { + // Create + MassiveMapDef> permIds = new MassiveMapDef<>(); + + // Fill + for (Entry> entry : perms.entrySet()) + { + permIds.put(entry.getKey().getId(), entry.getValue()); + } + + // Set + this.setPermIds(permIds); + } + + // FINEST + private boolean isPermitted(String permId, String permissibleId) { if (permId == null) throw new NullPointerException("permId"); - Set rels = this.perms.get(permId); - if (rels != null) return rels.contains(rel); + Set rels = this.perms.get(permId); + if (rels != null) return rels.contains(permissibleId); MPerm perm = MPerm.get(permId); if (perm == null) throw new NullPointerException("perm"); - return perm.getStandard().contains(rel); + return perm.getStandard().contains(permissibleId); } - public boolean isPermitted(MPerm perm, Rel rel) + public boolean isPermitted(MPerm perm, PermissionIdentifiable permissible) { if (perm == null) throw new NullPointerException("perm"); - - String permId = perm.getId(); - if (permId == null) throw new NullPointerException("permId"); - - Set rels = this.perms.get(permId); - if (rels != null) return rels.contains(rel); - - return perm.getStandard().contains(rel); + return this.isPermitted(perm.getId(), permissible.getPermissibleId()); } - // --- - - public Set getPermitted(MPerm perm) + private Set getPermitted(String permId) { - if (perm == null) throw new NullPointerException("perm"); - - String permId = perm.getId(); - if (permId == null) throw new NullPointerException("permId"); - - Set rels = this.perms.get(permId); - if (rels != null) return rels; - - return perm.getStandard(); - } - - public Set getPermitted(String permId) - { - if (permId == null) throw new NullPointerException("permId"); - - Set rels = this.perms.get(permId); + Set rels = this.perms.get(permId); if (rels != null) return rels; MPerm perm = MPerm.get(permId); @@ -834,49 +825,223 @@ public class Faction extends Entity implements FactionsParticipator return perm.getStandard(); } - @Deprecated - // Use getPermitted instead. It's much quicker although not immutable. - public Set getPermittedRelations(MPerm perm) + public Set getPermitted(MPerm perm) { - return this.getPerms().get(perm); + if (perm == null) throw new NullPointerException("perm"); + return this.getPermitted(perm.getId()); } - // --- - // TODO: Fix these below. They are reworking the whole map. - - public void setPermittedRelations(MPerm perm, Set rels) + public void setPermittedRelations(MPerm perm, Set permissibleIds) { - Map> perms = this.getPerms(); - perms.put(perm, rels); - this.setPerms(perms); + this.getPermIds().put(perm.getId(), permissibleIds); + this.changed(); } - public void setPermittedRelations(MPerm perm, Rel... rels) + public void setPermittedRelations(MPerm perm, String... permissibleIds) { - Set temp = new HashSet<>(); - temp.addAll(Arrays.asList(rels)); - this.setPermittedRelations(perm, temp); + this.setPermittedRelations(perm, new MassiveSet<>(permissibleIds)); } - public void setRelationPermitted(MPerm perm, Rel rel, boolean permitted) + public void setRelationPermitted(MPerm perm, String permissibleId, boolean permitted) { - Map> perms = this.getPerms(); + // Get + Map> perms = this.getPermIds(); + String permId = perm.getId(); + Set permissibleIds = perms.get(permId); - Set rels = perms.get(perm); - - boolean changed; + // Change if (permitted) { - changed = rels.add(rel); + permissibleIds.add(permissibleId); } else { - changed = rels.remove(rel); + permissibleIds.remove(permissibleId); } - this.setPerms(perms); + // Set + this.setPermittedRelations(perm, permissibleIds); + } + + // -------------------------------------------- // + // FIELD: ranks + // -------------------------------------------- // + + // RAW + public Map getRanks() + { + return this.ranks; + } + + public void setRanks(Map ranks) + { + // NoChange + if (MUtil.equals(this.ranks, ranks)) return; - if (changed) this.changed(); + // Apply + this.ranks = new MassiveMapDef<>(ranks); + + // Changed + this.changed(); + } + + // FINER + public Collection getRankCollection() + { + return this.getRanks().values(); + } + + private List getRankIdsOrdered() + { + return new MassiveList<>(this.ranks.keySet()); + } + + public Rank getRankLeader() + { + return this.ranks.get(this.getRankIdsOrdered().get(0)); + } + + public Rank getRankLowest() + { + List ids = this.getRankIdsOrdered(); + return this.ranks.get(ids.get(ids.size() - 1)); + } + + public void addRank(Rank rank) + { + if (rank == null) return; + + // Add + this.getRanks().put(rank.getId(), rank); + + // Changed + this.changed(); + } + + public boolean removeRank(Rank rank) + { + // Check + if (rank == null) return false; + if (rank.equals(this.getRankLeader())) return false; + if (rank.equals(this.getRankLowest())) return false; + + // Remove + Rank ret = this.getRanks().remove(rank.getId()); + + // Changed + boolean success = ret != null; + if (success) + { + this.adjustRankOrder(rank); + this.changed(); + } + + // Return + return success; + } + + public boolean demote(MPlayer mplayer) + { + if (mplayer.getFaction() != this) return false; + if (mplayer.getRank().equals(this.getRankLowest())) return false; + + // Gather + Rank current = mplayer.getRank(); + Rank demoted = this.getNextLowerRank(current); + + // Set + mplayer.setRank(demoted); + return true; + } + + public Rank getNextLowerRank(Rank rank) + { + // Get order + Rank ret = null; + + // Find next lower order + for (Rank otherRank : this.getRankCollection()) + { + if (!otherRank.isLessThan(rank)) continue; + + ret = otherRank; + break; + } + + // Return + return ret; + } + + public Rank createRank(String name) + { + // Prepare + int order = this.getRankLowest().getOrder() + 1; + + // Create + Rank ret = Rank.DEFAULT.copy(); + + // Fill + ret.setName(name); + ret.setOrder(order); + + // Return + return ret; + } + + private void adjustRankOrder(Rank rank) + { + for (Rank other : this.getRankCollection()) + { + if (other.isHigherThan(rank)) continue; + + other.setOrder(other.getOrder() - 1); + } + } + + public void adjustRankOrder(Rank rank, int order) + { + // Get List + List ids = this.getRankIdsOrdered(); + + // Sort in rank + String rankId = rank.getId(); + ids.remove(rankId); + ids.add(order - 1, rankId); + + // Create new order + Map rankOrdered = new MassiveMap<>(); + + for (int i = 0; i < ids.size(); i++) + { + String id = ids.get(i); + Rank ordered = this.ranks.get(id); + ordered.setOrder(i); + rankOrdered.put(id, ordered); + } + + this.setRanks(rankOrdered); + } + + // -------------------------------------------- // + // FIELD: rankManager + // -------------------------------------------- // + + // RAW + public Rank getRankManager() + { + return this.rankManager; + } + + public void setRankManager(Rank rankManager) + { + // NoChange + if (MUtil.equals(this.rankManager, rankManager)) return; + + // Apply + this.rankManager = rankManager; + + // Changed + this.changed(); } // -------------------------------------------- // @@ -937,7 +1102,7 @@ public class Faction extends Entity implements FactionsParticipator public double getPowerMax() { if (this.getFlag(MFlag.getFlagInfpower())) return 999999; - + double ret = 0; for (MPlayer mplayer : this.getMPlayers()) { @@ -972,6 +1137,7 @@ public class Faction extends Entity implements FactionsParticipator { return BoardColl.get().getCount(this); } + public int getLandCountInWorld(String worldName) { return Board.get(worldName).getCount(this); @@ -1003,9 +1169,9 @@ public class Faction extends Entity implements FactionsParticipator public List getMPlayersWhere(Predicate predicate) { List ret = this.getMPlayers(); - for (Iterator it = ret.iterator(); it.hasNext();) + for (Iterator it = ret.iterator(); it.hasNext(); ) { - if ( ! predicate.apply(it.next())) it.remove(); + if (!predicate.apply(it.next())) it.remove(); } return ret; } @@ -1014,20 +1180,25 @@ public class Faction extends Entity implements FactionsParticipator { return this.getMPlayersWhere(online ? SenderColl.PREDICATE_ONLINE : SenderColl.PREDICATE_OFFLINE); } - + public List getMPlayersWhereOnlineTo(Object senderObject) { return this.getMPlayersWhere(PredicateAnd.get(SenderColl.PREDICATE_ONLINE, PredicateVisibleTo.get(senderObject))); } - public List getMPlayersWhereRole(Rel role) + public List getMPlayersWhereRank(Rank rank) { - return this.getMPlayersWhere(PredicateMPlayerRole.get(role)); + return this.getMPlayersWhere(PredicateRank.get(rank)); + } + + public List getMPlayersWhereLeader() + { + return this.getMPlayersWhere(PredicateRank.get(this.getRankLeader())); } public MPlayer getLeader() { - List ret = this.getMPlayersWhereRole(Rel.LEADER); + List ret = this.getMPlayersWhereLeader(); if (ret.size() == 0) return null; return ret.get(0); } @@ -1071,66 +1242,57 @@ public class Faction extends Entity implements FactionsParticipator // Return Ret return ret; } - + // used when current leader is about to be removed from the faction; promotes new leader, or disbands faction if no other members left public void promoteNewLeader() { - if ( ! this.isNormal()) return; + if (!this.isNormal()) return; if (this.getFlag(MFlag.getFlagPermanent()) && MConf.get().permanentFactionsDisableLeaderPromotion) return; - + MPlayer oldLeader = this.getLeader(); - + Rank rankLeader = this.getRankLeader(); + Rank nextLower = this.getNextLowerRank(rankLeader); + // get list of officers, or list of normal members if there are no officers - List replacements = this.getMPlayersWhereRole(Rel.OFFICER); - if (replacements == null || replacements.isEmpty()) - { - replacements = this.getMPlayersWhereRole(Rel.MEMBER); - } - + List replacements = this.getMPlayersWhereRank(nextLower); if (replacements == null || replacements.isEmpty()) { // faction leader is the only member; one-man faction if (this.getFlag(MFlag.getFlagPermanent())) { - if (oldLeader != null) - { - // TODO: Where is the logic in this? Why MEMBER? Why not LEADER again? And why not OFFICER or RECRUIT? - oldLeader.setRole(Rel.MEMBER); - } + if (oldLeader != null) oldLeader.setRank(rankLeader); return; } - + // no members left and faction isn't permanent, so disband it if (MConf.get().logFactionDisband) { - Factions.get().log("The faction "+this.getName()+" ("+this.getId()+") has been disbanded since it has no members left."); + Factions.get().log("The faction " + this.getName() + " (" + this.getId() + ") has been disbanded since it has no members left."); } - + for (MPlayer mplayer : MPlayerColl.get().getAllOnline()) { mplayer.msg("The faction %s was disbanded.", this.getName(mplayer)); } - + this.detach(); } else { // promote new faction leader - if (oldLeader != null) - { - oldLeader.setRole(Rel.MEMBER); - } - - replacements.get(0).setRole(Rel.LEADER); + if (oldLeader != null) this.demote(oldLeader); + replacements.get(0).setRank(rankLeader); + + // Inform this.msg("Faction leader %s has been removed. %s has been promoted as the new faction leader.", oldLeader == null ? "" : oldLeader.getName(), replacements.get(0).getName()); - Factions.get().log("Faction "+this.getName()+" ("+this.getId()+") leader was removed. Replacement leader: "+replacements.get(0).getName()); + Factions.get().log("Faction " + this.getName() + " (" + this.getId() + ") leader was removed. Replacement leader: " + replacements.get(0).getName()); } } // -------------------------------------------- // // FACTION ONLINE STATE // -------------------------------------------- // - + public boolean isAllMPlayersOffline() { return this.getMPlayersWhereOnline(true).size() == 0; @@ -1155,10 +1317,10 @@ public class Faction extends Entity implements FactionsParticipator { boolean explosions = this.getFlag(MFlag.getFlagExplosions()); boolean offlineexplosions = this.getFlag(MFlag.getFlagOfflineexplosions()); - + if (explosions && offlineexplosions) return true; - if ( ! explosions && ! offlineexplosions) return false; - + if (!explosions && !offlineexplosions) return false; + boolean online = this.isFactionConsideredOnline(); return (online && explosions) || (!online && offlineexplosions); diff --git a/src/com/massivecraft/factions/entity/FactionColl.java b/src/com/massivecraft/factions/entity/FactionColl.java index 61a8019a..a6e5b0ef 100644 --- a/src/com/massivecraft/factions/entity/FactionColl.java +++ b/src/com/massivecraft/factions/entity/FactionColl.java @@ -1,12 +1,5 @@ package com.massivecraft.factions.entity; -import com.massivecraft.factions.Factions; -import com.massivecraft.factions.Rel; -import com.massivecraft.factions.integration.Econ; -import com.massivecraft.factions.util.MiscUtil; -import com.massivecraft.massivecore.store.Coll; -import com.massivecraft.massivecore.util.Txt; - import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; @@ -14,6 +7,14 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.massivecraft.factions.PermissibleId; +import com.massivecraft.massivecore.store.Coll; +import com.massivecraft.massivecore.util.Txt; +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.integration.Econ; +import com.massivecraft.factions.util.MiscUtil; + public class FactionColl extends Coll { // -------------------------------------------- // @@ -84,12 +85,12 @@ public class FactionColl extends Coll faction.setFlag(MFlag.getFlagEndergrief(), true); faction.setFlag(MFlag.getFlagZombiegrief(), true); - faction.setPermittedRelations(MPerm.getPermBuild(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermDoor(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermContainer(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermButton(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermLever(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermDeposit(), Rel.LEADER, Rel.OFFICER); // Wilderness deposit should be limited as an anti spam meassure. + faction.setPermittedRelations(MPerm.getPermBuild(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermDoor(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermContainer(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermButton(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermLever(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermDeposit(), PermissibleId.LEADER, PermissibleId.OFFICER); // Wilderness deposit should be limited as an anti spam meassure. return faction; } @@ -120,11 +121,11 @@ public class FactionColl extends Coll faction.setFlag(MFlag.getFlagEndergrief(), false); faction.setFlag(MFlag.getFlagZombiegrief(), false); - faction.setPermittedRelations(MPerm.getPermDoor(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermContainer(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermButton(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermLever(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermTerritory(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER); + faction.setPermittedRelations(MPerm.getPermDoor(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermContainer(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermButton(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermLever(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermTerritory(), PermissibleId.LEADER, PermissibleId.OFFICER, PermissibleId.MEMBER); return faction; } @@ -155,11 +156,11 @@ public class FactionColl extends Coll faction.setFlag(MFlag.getFlagEndergrief(), true); faction.setFlag(MFlag.getFlagZombiegrief(), true); - faction.setPermittedRelations(MPerm.getPermDoor(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermContainer(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermButton(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermLever(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY); - faction.setPermittedRelations(MPerm.getPermTerritory(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER); + faction.setPermittedRelations(MPerm.getPermDoor(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermContainer(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermButton(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermLever(), PermissibleId.ALL); + faction.setPermittedRelations(MPerm.getPermTerritory(), PermissibleId.LEADER, PermissibleId.OFFICER, PermissibleId.MEMBER); return faction; } @@ -218,15 +219,15 @@ public class FactionColl extends Coll // Fill // Check minimum length - if (MiscUtil.getComparisonString(str).length() < MConf.get().factionNameLengthMin) + if (MiscUtil.getComparisonString(str).length() < MConf.get().nameLengthFactionMin) { - errors.add(Txt.parse("The faction name can't be shorter than %s chars.", MConf.get().factionNameLengthMin)); + errors.add(Txt.parse("The faction name can't be shorter than %s chars.", MConf.get().nameLengthFactionMin)); } // Check maximum length - if (str.length() > MConf.get().factionNameLengthMax) + if (str.length() > MConf.get().nameLengthFactionMax) { - errors.add(Txt.parse("The faction name can't be longer than %s chars.", MConf.get().factionNameLengthMax)); + errors.add(Txt.parse("The faction name can't be longer than %s chars.", MConf.get().nameLengthFactionMax)); } // Check characters used diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index 1e52388e..4d874935 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -1,5 +1,17 @@ package com.massivecraft.factions.entity; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventPriority; + import com.massivecraft.factions.Factions; import com.massivecraft.factions.Rel; import com.massivecraft.factions.engine.EngineChat; @@ -13,17 +25,6 @@ import com.massivecraft.massivecore.command.type.TypeMillisDiff; import com.massivecraft.massivecore.store.Entity; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.TimeUnit; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.entity.EntityType; -import org.bukkit.event.EventPriority; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; @EditorName("config") public class MConf extends Entity @@ -134,10 +135,6 @@ public class MConf extends Entity // "none" means Wilderness. Remember to specify the id, like "3defeec7-b3b1-48d9-82bb-2a8903df24e3" and not the name. public String defaultPlayerFactionId = Factions.ID_NONE; - // What rank should new players joining a faction get? - // If not RECRUIT then MEMBER might make sense. - public Rel defaultPlayerRole = Rel.RECRUIT; - // What power should the player start with? public double defaultPlayerPower = 0.0; @@ -190,8 +187,12 @@ public class MConf extends Entity public double factionPowerMax = 0.0; // Limit the length of faction names here. - public int factionNameLengthMin = 3; - public int factionNameLengthMax = 16; + public int nameLengthFactionMin = 3; + public int nameLengthFactionMax = 16; + + // Limit the length of rank names here. + public int nameLengthRankMin = 3; + public int nameLengthRankMax = 10; // Should faction names automatically be converted to upper case? // You probably don't want this feature. @@ -396,7 +397,7 @@ public class MConf extends Entity Rel.NEUTRAL, new ArrayList(), Rel.TRUCE, new ArrayList(), Rel.ALLY, new ArrayList(), - Rel.MEMBER, new ArrayList() + Rel.FACTION, new ArrayList() ); // The distance for denying the following commands. Set to -1 to disable. @@ -410,12 +411,12 @@ public class MConf extends Entity Rel.NEUTRAL, new ArrayList(), Rel.TRUCE, new ArrayList(), Rel.ALLY, new ArrayList(), - Rel.MEMBER, new ArrayList() + Rel.FACTION, new ArrayList() ); // Allow bypassing the above setting when in these territories. public List denyCommandsDistanceBypassIn = MUtil.list( - Rel.MEMBER, + Rel.FACTION, Rel.ALLY ); diff --git a/src/com/massivecraft/factions/entity/MPerm.java b/src/com/massivecraft/factions/entity/MPerm.java index 72b2ebe2..831f81fa 100644 --- a/src/com/massivecraft/factions/entity/MPerm.java +++ b/src/com/massivecraft/factions/entity/MPerm.java @@ -1,6 +1,7 @@ package com.massivecraft.factions.entity; import com.massivecraft.factions.Perm; +import com.massivecraft.factions.PermissionIdentifiable; import com.massivecraft.factions.Rel; import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.cmd.CmdFactions; @@ -8,11 +9,11 @@ import com.massivecraft.factions.event.EventFactionsCreatePerms; import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.Prioritized; import com.massivecraft.massivecore.Registerable; +import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.comparator.ComparatorSmart; import com.massivecraft.massivecore.predicate.PredicateIsRegistered; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.store.Entity; -import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; import org.bukkit.entity.Player; @@ -21,6 +22,8 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import static com.massivecraft.factions.PermissibleId.*; + public class MPerm extends Entity implements Prioritized, Registerable, Named { // -------------------------------------------- // @@ -79,6 +82,32 @@ public class MPerm extends Entity implements Prioritized, Registerable, N public final static transient int PRIORITY_PERMS = 23000; public final static transient int PRIORITY_STATUS = 24000; + public final static transient Set STANDARD_BUILD = new MassiveSet<>(LEADER, OFFICER, MEMBER); + public final static transient Set STANDARD_PAINBUILD = new MassiveSet<>(); + public final static transient Set STANDARD_DOOR = new MassiveSet<>(LEADER, OFFICER, MEMBER, RECRUIT, ALLY); + public final static transient Set STANDARD_BUTTON = new MassiveSet<>(LEADER, OFFICER, MEMBER, RECRUIT, ALLY); + public final static transient Set STANDARD_LEVER = new MassiveSet<>(LEADER, OFFICER, MEMBER, RECRUIT, ALLY); + public final static transient Set STANDARD_CONTAINER = new MassiveSet<>(LEADER, OFFICER, MEMBER); + + public final static transient Set STANDARD_NAME = new MassiveSet<>(LEADER); + public final static transient Set STANDARD_DESC = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_MOTD = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_INVITE = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_KICK = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_TITLE = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_HOME = new MassiveSet<>(LEADER, OFFICER, MEMBER, RECRUIT); + public final static transient Set STANDARD_SETHOME = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_DEPOSIT = new MassiveSet<>(LEADER, OFFICER, MEMBER, RECRUIT, ALLY, TRUCE, NEUTRAL, ENEMY); + public final static transient Set STANDARD_WITHDRAW = new MassiveSet<>(LEADER); + public final static transient Set STANDARD_TERRITORY = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_ACCESS = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_CLAIMNEAR = new MassiveSet<>(LEADER, OFFICER, MEMBER, RECRUIT, ALLY); + public final static transient Set STANDARD_REL = new MassiveSet<>(LEADER, OFFICER); + public final static transient Set STANDARD_DISBAND = new MassiveSet<>(LEADER); + public final static transient Set STANDARD_FLAGS = new MassiveSet<>(LEADER); + public final static transient Set STANDARD_PERMS = new MassiveSet<>(LEADER); + public final static transient Set STANDARD_STATUS = new MassiveSet<>(LEADER, OFFICER, MEMBER, RECRUIT); + // -------------------------------------------- // // META: CORE // -------------------------------------------- // @@ -130,33 +159,33 @@ public class MPerm extends Entity implements Prioritized, Registerable, N getPermPerms(); } - public static MPerm getPermBuild() { return getCreative(PRIORITY_BUILD, ID_BUILD, ID_BUILD, "edit the terrain", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER), true, true, true); } - public static MPerm getPermPainbuild() { return getCreative(PRIORITY_PAINBUILD, ID_PAINBUILD, ID_PAINBUILD, "edit, take damage", new LinkedHashSet(), true, true, true); } - public static MPerm getPermDoor() { return getCreative(PRIORITY_DOOR, ID_DOOR, ID_DOOR, "use doors", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), true, true, true); } - public static MPerm getPermButton() { return getCreative(PRIORITY_BUTTON, ID_BUTTON, ID_BUTTON, "use stone buttons", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), true, true, true); } - public static MPerm getPermLever() { return getCreative(PRIORITY_LEVER, ID_LEVER, ID_LEVER, "use levers", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), true, true, true); } - public static MPerm getPermContainer() { return getCreative(PRIORITY_CONTAINER, ID_CONTAINER, ID_CONTAINER, "use containers", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER), true, true, true); } + public static MPerm getPermBuild() { return getCreative(PRIORITY_BUILD, ID_BUILD, ID_BUILD, "edit the terrain", STANDARD_BUILD, true, true, true); } + public static MPerm getPermPainbuild() { return getCreative(PRIORITY_PAINBUILD, ID_PAINBUILD, ID_PAINBUILD, "edit, take damage", STANDARD_PAINBUILD, true, true, true); } + public static MPerm getPermDoor() { return getCreative(PRIORITY_DOOR, ID_DOOR, ID_DOOR, "use doors", STANDARD_DOOR, true, true, true); } + public static MPerm getPermButton() { return getCreative(PRIORITY_BUTTON, ID_BUTTON, ID_BUTTON, "use stone buttons", STANDARD_BUTTON, true, true, true); } + public static MPerm getPermLever() { return getCreative(PRIORITY_LEVER, ID_LEVER, ID_LEVER, "use levers", STANDARD_LEVER, true, true, true); } + public static MPerm getPermContainer() { return getCreative(PRIORITY_CONTAINER, ID_CONTAINER, ID_CONTAINER, "use containers", STANDARD_CONTAINER, true, true, true); } - public static MPerm getPermName() { return getCreative(PRIORITY_NAME, ID_NAME, ID_NAME, "set name", MUtil.set(Rel.LEADER), false, true, true); } - public static MPerm getPermDesc() { return getCreative(PRIORITY_DESC, ID_DESC, ID_DESC, "set description", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermMotd() { return getCreative(PRIORITY_MOTD, ID_MOTD, ID_MOTD, "set motd", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermInvite() { return getCreative(PRIORITY_INVITE, ID_INVITE, ID_INVITE, "invite players", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermStatus() { return getCreative(PRIORITY_STATUS, ID_STATUS, ID_STATUS, "show status", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermKick() { return getCreative(PRIORITY_KICK, ID_KICK, ID_KICK, "kick members", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermTitle() { return getCreative(PRIORITY_TITLE, ID_TITLE, ID_TITLE, "set titles", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermHome() { return getCreative(PRIORITY_HOME, ID_HOME, ID_HOME, "teleport home", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), false, true, true); } - public static MPerm getPermSethome() { return getCreative(PRIORITY_SETHOME, ID_SETHOME, ID_SETHOME, "set the home", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermDeposit() { return getCreative(PRIORITY_DEPOSIT, ID_DEPOSIT, ID_DEPOSIT, "deposit money", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY), false, false, false); } // non editable, non visible. - public static MPerm getPermWithdraw() { return getCreative(PRIORITY_WITHDRAW, ID_WITHDRAW, ID_WITHDRAW, "withdraw money", MUtil.set(Rel.LEADER), false, true, true); } - public static MPerm getPermTerritory() { return getCreative(PRIORITY_TERRITORY, ID_TERRITORY, ID_TERRITORY, "claim or unclaim", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermAccess() { return getCreative(PRIORITY_ACCESS, ID_ACCESS, ID_ACCESS, "grant territory", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermClaimnear() { return getCreative(PRIORITY_CLAIMNEAR, ID_CLAIMNEAR, ID_CLAIMNEAR, "claim nearby", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), false, false, false); } // non editable, non visible. - public static MPerm getPermRel() { return getCreative(PRIORITY_REL, ID_REL, ID_REL, "change relations", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } - public static MPerm getPermDisband() { return getCreative(PRIORITY_DISBAND, ID_DISBAND, ID_DISBAND, "disband the faction", MUtil.set(Rel.LEADER), false, true, true); } - public static MPerm getPermFlags() { return getCreative(PRIORITY_FLAGS, ID_FLAGS, ID_FLAGS, "manage flags", MUtil.set(Rel.LEADER), false, true, true); } - public static MPerm getPermPerms() { return getCreative(PRIORITY_PERMS, ID_PERMS, ID_PERMS, "manage permissions", MUtil.set(Rel.LEADER), false, true, true); } + public static MPerm getPermName() { return getCreative(PRIORITY_NAME, ID_NAME, ID_NAME, "set name", STANDARD_NAME, false, true, true); } + public static MPerm getPermDesc() { return getCreative(PRIORITY_DESC, ID_DESC, ID_DESC, "set description", STANDARD_DESC, false, true, true); } + public static MPerm getPermMotd() { return getCreative(PRIORITY_MOTD, ID_MOTD, ID_MOTD, "set motd", STANDARD_MOTD, false, true, true); } + public static MPerm getPermInvite() { return getCreative(PRIORITY_INVITE, ID_INVITE, ID_INVITE, "invite players", STANDARD_INVITE, false, true, true); } + public static MPerm getPermStatus() { return getCreative(PRIORITY_STATUS, ID_STATUS, ID_STATUS, "show status", STANDARD_STATUS, false, true, true); } + public static MPerm getPermKick() { return getCreative(PRIORITY_KICK, ID_KICK, ID_KICK, "kick members", STANDARD_KICK, false, true, true); } + public static MPerm getPermTitle() { return getCreative(PRIORITY_TITLE, ID_TITLE, ID_TITLE, "set titles", STANDARD_TITLE, false, true, true); } + public static MPerm getPermHome() { return getCreative(PRIORITY_HOME, ID_HOME, ID_HOME, "teleport home", STANDARD_HOME, false, true, true); } + public static MPerm getPermSethome() { return getCreative(PRIORITY_SETHOME, ID_SETHOME, ID_SETHOME, "set the home", STANDARD_SETHOME, false, true, true); } + public static MPerm getPermDeposit() { return getCreative(PRIORITY_DEPOSIT, ID_DEPOSIT, ID_DEPOSIT, "deposit money", STANDARD_DEPOSIT, false, false, false); } // non editable, non visible. + public static MPerm getPermWithdraw() { return getCreative(PRIORITY_WITHDRAW, ID_WITHDRAW, ID_WITHDRAW, "withdraw money", STANDARD_WITHDRAW, false, true, true); } + public static MPerm getPermTerritory() { return getCreative(PRIORITY_TERRITORY, ID_TERRITORY, ID_TERRITORY, "claim or unclaim", STANDARD_TERRITORY, false, true, true); } + public static MPerm getPermAccess() { return getCreative(PRIORITY_ACCESS, ID_ACCESS, ID_ACCESS, "grant territory", STANDARD_ACCESS, false, true, true); } + public static MPerm getPermClaimnear() { return getCreative(PRIORITY_CLAIMNEAR, ID_CLAIMNEAR, ID_CLAIMNEAR, "claim nearby", STANDARD_CLAIMNEAR, false, false, false); } // non editable, non visible. + public static MPerm getPermRel() { return getCreative(PRIORITY_REL, ID_REL, ID_REL, "change relations", STANDARD_REL, false, true, true); } + public static MPerm getPermDisband() { return getCreative(PRIORITY_DISBAND, ID_DISBAND, ID_DISBAND, "disband the faction", STANDARD_DISBAND, false, true, true); } + public static MPerm getPermFlags() { return getCreative(PRIORITY_FLAGS, ID_FLAGS, ID_FLAGS, "manage flags", STANDARD_FLAGS, false, true, true); } + public static MPerm getPermPerms() { return getCreative(PRIORITY_PERMS, ID_PERMS, ID_PERMS, "manage permissions", STANDARD_PERMS, false, true, true); } - public static MPerm getCreative(int priority, String id, String name, String desc, Set standard, boolean territory, boolean editable, boolean visible) + public static MPerm getCreative(int priority, String id, String name, String desc, Set standard, boolean territory, boolean editable, boolean visible) { MPerm ret = MPermColl.get().get(id, false); if (ret != null) @@ -231,9 +260,9 @@ public class MPerm extends Entity implements Prioritized, Registerable, N // What is the standard (aka default) perm value? // This value will be set for factions from the beginning. // Example: ... set of relations ... - private Set standard = new LinkedHashSet<>(); - public Set getStandard() { return this.standard; } - public MPerm setStandard(Set standard) { this.standard = standard; this.changed(); return this; } + private Set standard = new LinkedHashSet(); + public Set getStandard() { return this.standard; } + public MPerm setStandard(Set standard) { this.standard = standard; this.changed(); return this; } // Is this a territory perm meaning it has to do with territory construction, modification or interaction? // True Examples: build, container, door, lever etc. @@ -271,7 +300,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N // No argument constructor for GSON } - public MPerm(int priority, String name, String desc, Set standard, boolean territory, boolean editable, boolean visible) + public MPerm(int priority, String name, String desc, Set standard, boolean territory, boolean editable, boolean visible) { this.priority = priority; this.name = name; @@ -357,7 +386,8 @@ public class MPerm extends Entity implements Prioritized, Registerable, N if (mplayer.isOverriding()) return true; Rel rel = mplayer.getRelationTo(hostFaction); - if (hostFaction.isPermitted(this, rel)) return true; + PermissionIdentifiable permissible = rel == Rel.FACTION ? mplayer.getRank() :rel; + if (hostFaction.isPermitted(this, permissible)) return true; if (verboose) mplayer.message(this.createDeniedMessage(mplayer, hostFaction)); @@ -408,13 +438,14 @@ public class MPerm extends Entity implements Prioritized, Registerable, N return ret; } - public String getStateInfo(Set value, boolean withDesc) + // TODO: get rid of this state info in an earlier commit by re-doing Perm - list/show + public String getStateInfo(Set value, boolean withDesc) { String ret = ""; for (Rel rel : Rel.values()) { - if (value.contains(rel)) + if (value.contains(rel.getName().toLowerCase())) { ret += "YES"; } diff --git a/src/com/massivecraft/factions/entity/MPlayer.java b/src/com/massivecraft/factions/entity/MPlayer.java index 6e52fc4e..0e297a5b 100644 --- a/src/com/massivecraft/factions/entity/MPlayer.java +++ b/src/com/massivecraft/factions/entity/MPlayer.java @@ -1,9 +1,21 @@ package com.massivecraft.factions.entity; +import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.Set; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + import com.massivecraft.factions.Factions; import com.massivecraft.factions.FactionsIndex; import com.massivecraft.factions.FactionsParticipator; import com.massivecraft.factions.Perm; +import com.massivecraft.factions.Rank; import com.massivecraft.factions.Rel; import com.massivecraft.factions.RelationParticipator; import com.massivecraft.factions.event.EventFactionsChunkChangeType; @@ -23,16 +35,6 @@ import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.lang.ref.WeakReference; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map.Entry; -import java.util.Set; public class MPlayer extends SenderEntity implements FactionsParticipator { @@ -60,7 +62,7 @@ public class MPlayer extends SenderEntity implements FactionsParticipat { this.setLastActivityMillis(that.lastActivityMillis); this.setFactionId(that.factionId); - this.setRole(that.role); + this.setRank(that.rank); this.setTitle(that.title); this.setPowerBoost(that.powerBoost); this.setPower(that.power); @@ -130,7 +132,7 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // What role does the player have in the faction? // Null means default. - private Rel role = null; + private Rank rank = null; // What title does the player have in the faction? // The title is just for fun. It's not connected to any game mechanic. @@ -196,7 +198,7 @@ public class MPlayer extends SenderEntity implements FactionsParticipat { // The default neutral faction this.setFactionId(null); - this.setRole(null); + this.setRank(null); this.setTitle(null); this.setAutoClaimFaction(null); } @@ -301,27 +303,21 @@ public class MPlayer extends SenderEntity implements FactionsParticipat } // -------------------------------------------- // - // FIELD: role + // FIELD: rank // -------------------------------------------- // - - public Rel getRole() + + public Rank getRank() { - if (this.isFactionOrphan()) return Rel.RECRUIT; - - if (this.role == null) return MConf.get().defaultPlayerRole; - return this.role; + return this.rank; } - public void setRole(Rel role) + public void setRank(Rank rank) { - // Clean input - Rel target = role; - // Detect Nochange - if (MUtil.equals(this.role, target)) return; + if (MUtil.equals(this.rank, rank)) return; // Apply - this.role = target; + this.rank = rank; // Mark as changed this.changed(); @@ -598,12 +594,10 @@ public class MPlayer extends SenderEntity implements FactionsParticipat } // Base concatenations: - public String getNameAndSomething(String color, String something) { String ret = ""; ret += color; - ret += this.getRole().getPrefix(); if (something != null && something.length() > 0) { ret += something; @@ -745,7 +739,7 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // Apply // Promote a new leader if required. - if (this.getRole() == Rel.LEADER) + if (this.getRank().isLeader()) { Faction faction = this.getFaction(); if (faction != null) @@ -772,7 +766,7 @@ public class MPlayer extends SenderEntity implements FactionsParticipat if (myFaction.getMPlayers().size() > 1) { - if (!permanent && this.getRole() == Rel.LEADER) + if (!permanent && this.getRank().isLeader()) { msg("You must give the leader role to someone else first."); return; diff --git a/src/com/massivecraft/factions/event/EventFactionsRankChange.java b/src/com/massivecraft/factions/event/EventFactionsRankChange.java index 67802ce3..71983509 100644 --- a/src/com/massivecraft/factions/event/EventFactionsRankChange.java +++ b/src/com/massivecraft/factions/event/EventFactionsRankChange.java @@ -1,10 +1,11 @@ package com.massivecraft.factions.event; -import com.massivecraft.factions.Rel; -import com.massivecraft.factions.entity.MPlayer; import org.bukkit.command.CommandSender; import org.bukkit.event.HandlerList; +import com.massivecraft.factions.Rank; +import com.massivecraft.factions.entity.MPlayer; + public class EventFactionsRankChange extends EventFactionsAbstractSender { // -------------------------------------------- // @@ -22,15 +23,15 @@ public class EventFactionsRankChange extends EventFactionsAbstractSender private final MPlayer mplayer; public MPlayer getMPlayer() { return this.mplayer; } - private Rel newRank; - public Rel getNewRank() { return this.newRank; } - public void setNewRank(Rel newRole) { this.newRank = newRole; } + private Rank newRank; + public Rank getNewRank() { return this.newRank; } + public void setNewRank(Rank newRole) { this.newRank = newRole; } // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // - public EventFactionsRankChange(CommandSender sender, MPlayer mplayer, Rel newRank) + public EventFactionsRankChange(CommandSender sender, MPlayer mplayer, Rank newRank) { super(sender); this.mplayer = mplayer; diff --git a/src/com/massivecraft/factions/integration/herochat/ChannelFactionsAllies.java b/src/com/massivecraft/factions/integration/herochat/ChannelFactionsAllies.java index b93fc91b..4c5b721b 100644 --- a/src/com/massivecraft/factions/integration/herochat/ChannelFactionsAllies.java +++ b/src/com/massivecraft/factions/integration/herochat/ChannelFactionsAllies.java @@ -1,15 +1,16 @@ package com.massivecraft.factions.integration.herochat; -import com.massivecraft.factions.Rel; -import com.massivecraft.factions.entity.MConf; -import org.bukkit.ChatColor; - import java.util.EnumSet; import java.util.Set; +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.entity.MConf; + public class ChannelFactionsAllies extends ChannelFactionsAbstract { - public static final Set targetRelations = EnumSet.of(Rel.MEMBER, Rel.RECRUIT, Rel.ALLY); + public static final Set targetRelations = EnumSet.of(Rel.FACTION, Rel.ALLY); @Override public Set getTargetRelations() { return targetRelations; } @Override public String getName() { return MConf.get().herochatAlliesName; } diff --git a/src/com/massivecraft/factions/integration/herochat/ChannelFactionsFaction.java b/src/com/massivecraft/factions/integration/herochat/ChannelFactionsFaction.java index ce2d002e..0b080fdd 100644 --- a/src/com/massivecraft/factions/integration/herochat/ChannelFactionsFaction.java +++ b/src/com/massivecraft/factions/integration/herochat/ChannelFactionsFaction.java @@ -1,16 +1,17 @@ package com.massivecraft.factions.integration.herochat; -import com.massivecraft.factions.Rel; -import com.massivecraft.factions.entity.MConf; -import org.bukkit.ChatColor; - import java.util.EnumSet; import java.util.HashSet; import java.util.Set; +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.entity.MConf; + public class ChannelFactionsFaction extends ChannelFactionsAbstract { - public static final Set targetRelations = EnumSet.of(Rel.MEMBER, Rel.RECRUIT); + public static final Set targetRelations = EnumSet.of(Rel.FACTION); @Override public Set getTargetRelations() { return targetRelations; } @Override public String getName() { return MConf.get().herochatFactionName; } diff --git a/src/com/massivecraft/factions/update/OldConf.java b/src/com/massivecraft/factions/update/OldConf.java new file mode 100644 index 00000000..f7ae2eb3 --- /dev/null +++ b/src/com/massivecraft/factions/update/OldConf.java @@ -0,0 +1,223 @@ +package com.massivecraft.factions.update; + +import java.util.List; +import java.util.Map; + +import org.bukkit.event.EventPriority; + +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.event.EventFactionsChunkChangeType; +import com.massivecraft.massivecore.store.Entity; + +public class OldConf extends Entity +{ + // -------------------------------------------- // + // META + // -------------------------------------------- // + + public void transferTo(MConf mconf) + { + //mconf.enabled = this.enabled; + //mconf.factionIdNone = this.factionIdNone; + //mconf.factionIdSafezone = this.factionIdSafezone; + //mconf.factionIdWarzone = this.factionIdWarzone; + mconf.defaultPlayerFactionId = this.defaultPlayerFactionId; + //mconf.defaultPlayerRole = this.defaultPlayerRole; + mconf.defaultPlayerPower = this.defaultPlayerPower; + //mconf.defaultFactionOpen = this.defaultFactionOpen; + //mconf.defaultFactionFlags = this.defaultFactionFlags; + //mconf.defaultFactionPerms = this.defaultFactionPerms; + mconf.powerMax = this.powerMax; + mconf.powerMin = this.powerMin; + mconf.powerPerHour = this.powerPerHour; + mconf.powerPerDeath = this.powerPerDeath; + mconf.canLeaveWithNegativePower = this.canLeaveWithNegativePower; + mconf.factionMemberLimit = this.factionMemberLimit; + mconf.factionPowerMax = this.factionPowerMax; + mconf.nameLengthFactionMin = this.factionNameLengthMin; + mconf.nameLengthFactionMax = this.factionNameLengthMax; + mconf.factionNameForceUpperCase = this.factionNameForceUpperCase; + mconf.claimsMustBeConnected = this.claimsMustBeConnected; + mconf.claimingFromOthersAllowed = this.claimingFromOthersAllowed; + mconf.claimsCanBeUnconnectedIfOwnedByOtherFaction = this.claimsCanBeUnconnectedIfOwnedByOtherFaction; + mconf.claimsRequireMinFactionMembers = this.claimsRequireMinFactionMembers; + mconf.claimedLandsMax = this.claimedLandsMax; + mconf.homesEnabled = this.homesEnabled; + mconf.homesMustBeInClaimedTerritory = this.homesMustBeInClaimedTerritory; + mconf.homesTeleportCommandEnabled = this.homesTeleportCommandEnabled; + mconf.homesTeleportAllowedFromEnemyTerritory = this.homesTeleportAllowedFromEnemyTerritory; + mconf.homesTeleportAllowedFromDifferentWorld = this.homesTeleportAllowedFromDifferentWorld; + mconf.homesTeleportAllowedEnemyDistance = this.homesTeleportAllowedEnemyDistance; + mconf.homesTeleportIgnoreEnemiesIfInOwnTerritory = this.homesTeleportIgnoreEnemiesIfInOwnTerritory; + mconf.homesTeleportToOnDeathActive = this.homesTeleportToOnDeathActive; + mconf.homesTeleportToOnDeathPriority = this.homesTeleportToOnDeathPriority; + mconf.permanentFactionsDisableLeaderPromotion = this.permanentFactionsDisableLeaderPromotion; + mconf.actionDeniedPainAmount = this.actionDeniedPainAmount; + mconf.disablePVPForFactionlessPlayers = this.disablePVPForFactionlessPlayers; + mconf.enablePVPAgainstFactionlessInAttackersLand = this.enablePVPAgainstFactionlessInAttackersLand; + mconf.territoryShieldFactor = this.territoryShieldFactor; + mconf.denyCommandsPermanentFactionMember = this.denyCommandsPermanentFactionMember; + mconf.denyCommandsTerritoryRelation = this.denyCommandsTerritoryRelation; + mconf.lwcRemoveOnChange = this.lwcRemoveOnChange; + mconf.econEnabled = this.econEnabled; + mconf.econLandReward = this.econLandReward; + mconf.econUniverseAccount = this.econUniverseAccount; + mconf.econChunkCost = this.econChunkCost; + mconf.econCostCreate = this.econCostCreate; + mconf.econCostSethome = this.econCostSethome; + mconf.econCostJoin = this.econCostJoin; + mconf.econCostLeave = this.econCostLeave; + mconf.econCostKick = this.econCostKick; + mconf.econCostInvite = this.econCostInvite; + mconf.econCostDeinvite = this.econCostDeinvite; + mconf.econCostHome = this.econCostHome; + mconf.econCostName = this.econCostName; + mconf.econCostDescription = this.econCostDescription; + mconf.econCostTitle = this.econCostTitle; + mconf.econCostFlag = this.econCostOpen; + mconf.econRelCost = this.econRelCost; + mconf.bankEnabled = this.bankEnabled; + mconf.bankFactionPaysCosts = this.bankFactionPaysCosts; + mconf.bankFactionPaysLandCosts = this.bankFactionPaysLandCosts; + } + + // -------------------------------------------- // + // UNIVERSE ENABLE SWITCH + // -------------------------------------------- // + + public boolean enabled = true; + + // -------------------------------------------- // + // SPECIAL FACTION IDS + // -------------------------------------------- // + + public String factionIdNone = null; + public String factionIdSafezone = null; + public String factionIdWarzone = null; + + // -------------------------------------------- // + // DEFAULTS + // -------------------------------------------- // + + public String defaultPlayerFactionId = null; + public Rel defaultPlayerRole = null; + public double defaultPlayerPower = 0.0; + + //public boolean defaultFactionOpen = false; + //public Map defaultFactionFlags = null; + //public Map> defaultFactionPerms = null; + + // -------------------------------------------- // + // MESSAGES + // -------------------------------------------- // + + public boolean broadcastNameChange = false; + + // -------------------------------------------- // + // POWER + // -------------------------------------------- // + + public double powerMax = 10.0; + public double powerMin = 0.0; + public double powerPerHour = 2.0; + public double powerPerDeath = -2.0; + + public boolean canLeaveWithNegativePower = true; + + // -------------------------------------------- // + // CORE + // -------------------------------------------- // + + public int factionMemberLimit = 0; + public double factionPowerMax = 0.0; + + public int factionNameLengthMin = 3; + public int factionNameLengthMax = 16; + public boolean factionNameForceUpperCase = false; + + // -------------------------------------------- // + // CLAIMS + // -------------------------------------------- // + + public boolean claimsMustBeConnected = true; + public boolean claimingFromOthersAllowed = true; + public boolean claimsCanBeUnconnectedIfOwnedByOtherFaction = false; + public int claimsRequireMinFactionMembers = 1; + public int claimedLandsMax = 0; + + // -------------------------------------------- // + // HOMES + // -------------------------------------------- // + + public boolean homesEnabled = true; + public boolean homesMustBeInClaimedTerritory = true; + public boolean homesTeleportCommandEnabled = true; + public boolean homesTeleportAllowedFromEnemyTerritory = true; + public boolean homesTeleportAllowedFromDifferentWorld = true; + public double homesTeleportAllowedEnemyDistance = 32.0; + public boolean homesTeleportIgnoreEnemiesIfInOwnTerritory = true; + + public boolean homesTeleportToOnDeathActive = false; + public EventPriority homesTeleportToOnDeathPriority = null; + + // -------------------------------------------- // + // ASSORTED + // -------------------------------------------- // + + public boolean permanentFactionsDisableLeaderPromotion = false; + public double actionDeniedPainAmount = 2.0D; + public boolean disablePVPForFactionlessPlayers = false; + public boolean enablePVPAgainstFactionlessInAttackersLand = false; + public double territoryShieldFactor = 0.3D; + + // -------------------------------------------- // + // DENY COMMANDS + // -------------------------------------------- // + + // commands which will be prevented if the player is a member of a permanent faction + public List denyCommandsPermanentFactionMember = null; + + // commands which will be prevented when in claimed territory of another faction + public Map> denyCommandsTerritoryRelation = null; + + // -------------------------------------------- // + // INTEGRATION: LWC + // -------------------------------------------- // + + public Map lwcRemoveOnChange = null; + + // -------------------------------------------- // + // INTEGRATION: ECONOMY + // -------------------------------------------- // + + public boolean econEnabled = false; + + // TODO: Rename to include unit. + public double econLandReward = 0.00; + + public String econUniverseAccount = null; + + public Map econChunkCost = null; + + public double econCostCreate = 200.0; + public double econCostSethome = 0.0; + public double econCostJoin = 0.0; + public double econCostLeave = 0.0; + public double econCostKick = 0.0; + public double econCostInvite = 0.0; + public double econCostDeinvite = 0.0; + public double econCostHome = 0.0; + public double econCostName = 0.0; + public double econCostDescription = 0.0; + public double econCostTitle = 0.0; + public double econCostOpen = 0.0; + + public Map econRelCost = null; + + //Faction banks, to pay for land claiming and other costs instead of individuals paying for them + public boolean bankEnabled = true; + //public static boolean bankMembersCanWithdraw = false; //Have to be at least moderator to withdraw or pay money to another faction + public boolean bankFactionPaysCosts = true; //The faction pays for faction command costs, such as sethome + public boolean bankFactionPaysLandCosts = true; //The faction pays for land claiming costs. +} diff --git a/src/com/massivecraft/factions/util/RelationUtil.java b/src/com/massivecraft/factions/util/RelationUtil.java index 379c9089..75911f52 100644 --- a/src/com/massivecraft/factions/util/RelationUtil.java +++ b/src/com/massivecraft/factions/util/RelationUtil.java @@ -97,11 +97,7 @@ public class RelationUtil Faction thatFaction = getFaction(that); if (thatFaction == null) return Rel.NEUTRAL; // ERROR - if (myFaction.equals(thatFaction)) - { - if (that instanceof MPlayer) return ((MPlayer) that).getRole(); - return Rel.MEMBER; - } + if (myFaction.equals(thatFaction)) return Rel.FACTION; MFlag flagPeaceful = MFlag.getFlagPeaceful(); if (!ignorePeaceful && (thatFaction.getFlag(flagPeaceful) || myFaction.getFlag(flagPeaceful))) return Rel.TRUCE;