From 595a051a230d76c2f4999ca3635dd7b2cc429bd5 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Sat, 22 Dec 2018 01:55:46 +0100 Subject: [PATCH] Perm rework --- pom.xml | 1 - src/com/massivecraft/factions/Perm.java | 1 + src/com/massivecraft/factions/Rel.java | 6 + .../factions/cmd/CmdFactions.java | 1 - .../factions/cmd/CmdFactionsExpansions.java | 40 ------ .../factions/cmd/CmdFactionsPerm.java | 1 + .../factions/cmd/CmdFactionsPermList.java | 25 +--- .../factions/cmd/CmdFactionsPermSet.java | 41 ++----- .../factions/cmd/CmdFactionsPermShow.java | 58 ++++++--- .../factions/cmd/CmdFactionsPermShow2.java | 100 +++++++++++++++ .../cmd/CmdFactionsRankEditCreate.java | 3 +- .../factions/cmd/type/TypeMPermable.java | 114 ++++++++++++++++-- .../massivecraft/factions/entity/Faction.java | 73 +++++++++-- .../massivecraft/factions/entity/MConf.java | 4 +- .../massivecraft/factions/entity/MPerm.java | 22 +--- .../massivecraft/factions/entity/Rank.java | 31 +++++ 16 files changed, 370 insertions(+), 151 deletions(-) delete mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsExpansions.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsPermShow2.java diff --git a/pom.xml b/pom.xml index 2f7b8c2d..0c51e74e 100644 --- a/pom.xml +++ b/pom.xml @@ -76,7 +76,6 @@ org.apache.maven.plugins maven-compiler-plugin - 3.7.0 11 11 diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index 2acc5b7e..6aa9881f 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -58,6 +58,7 @@ public enum Perm implements Identified PERM_LIST, PERM_SET, PERM_SHOW, + PERM_SHOW2, PLAYER, POWERBOOST, POWERBOOST_PLAYER, diff --git a/src/com/massivecraft/factions/Rel.java b/src/com/massivecraft/factions/Rel.java index fc3d9922..cc4f7cb8 100644 --- a/src/com/massivecraft/factions/Rel.java +++ b/src/com/massivecraft/factions/Rel.java @@ -114,6 +114,12 @@ public enum Rel implements Colorized, Named, MPerm.MPermable { return name(); } + + @Override + public String getDisplayName(Object senderObject) + { + return this.getColor() + this.getName(); + } // -------------------------------------------- // // UTIL diff --git a/src/com/massivecraft/factions/cmd/CmdFactions.java b/src/com/massivecraft/factions/cmd/CmdFactions.java index b8b2c365..9b19529d 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/com/massivecraft/factions/cmd/CmdFactions.java @@ -55,7 +55,6 @@ public class CmdFactions extends FactionsCommand public CmdFactionsPerm cmdFactionsPerm = new CmdFactionsPerm(); public CmdFactionsFlag cmdFactionsFlag = new CmdFactionsFlag(); public CmdFactionsUnstuck cmdFactionsUnstuck = new CmdFactionsUnstuck(); - public CmdFactionsExpansions cmdFactionsExpansions = new CmdFactionsExpansions(); public CmdFactionsOverride cmdFactionsOverride = new CmdFactionsOverride(); public CmdFactionsDisband cmdFactionsDisband = new CmdFactionsDisband(); public CmdFactionsPowerBoost cmdFactionsPowerBoost = new CmdFactionsPowerBoost(); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsExpansions.java b/src/com/massivecraft/factions/cmd/CmdFactionsExpansions.java deleted file mode 100644 index f36f50d3..00000000 --- a/src/com/massivecraft/factions/cmd/CmdFactionsExpansions.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.massivecraft.factions.cmd; - -import com.massivecraft.factions.event.EventFactionsExpansions; -import com.massivecraft.massivecore.util.Txt; - -import java.util.Map.Entry; - -public class CmdFactionsExpansions extends FactionsCommand -{ - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public void perform() - { - // Event - EventFactionsExpansions event = new EventFactionsExpansions(sender); - event.run(); - - // Title - Object title = "Factions Expansions"; - title = Txt.titleize(title); - message(title); - - // Lines - for (Entry entry : event.getExpansions().entrySet()) - { - String name = entry.getKey(); - Boolean installed = entry.getValue(); - String format = (installed ? "[X] %s" : "[ ] %s"); - msg(format, name); - } - - // URL Suggestion - msg("Learn all about expansions in the online documentation:"); - msg("https://www.massivecraft.com/factions"); - } - -} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java b/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java index 48eab376..689fec9a 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java @@ -8,6 +8,7 @@ public class CmdFactionsPerm extends FactionsCommand CmdFactionsPermList cmdFactionsPermList = new CmdFactionsPermList(); CmdFactionsPermShow cmdFactionsPermShow = new CmdFactionsPermShow(); + CmdFactionsPermShow2 cmdFactionsPermShow2 = new CmdFactionsPermShow2(); CmdFactionsPermSet cmdFactionsPermSet = new CmdFactionsPermSet(); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java index cd2e3243..ba8057dd 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java @@ -14,19 +14,6 @@ import java.util.List; public class CmdFactionsPermList extends FactionsCommand { - // -------------------------------------------- // - // REUSABLE PREDICATE - // -------------------------------------------- // - - private static final Predicate PREDICATE_MPERM_VISIBLE = new Predicate() - { - @Override - public boolean apply(MPerm mperm) - { - return mperm.isVisible(); - } - }; - // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // @@ -49,16 +36,8 @@ public class CmdFactionsPermList extends FactionsCommand // Pager create String title = String.format("Perms for %s", msenderFaction.describeTo(msender)); - final Pager pager = new Pager<>(this, title, page, new Stringifier() - { - @Override - public String toString(MPerm mperm, int index) - { - return mperm.getDesc(true, true); - } - }); - - final Predicate predicate = msender.isOverriding() ? null : PREDICATE_MPERM_VISIBLE; + final Pager pager = new Pager<>(this, title, page, (Stringifier) (mp, i) -> mp.getDesc(true, true)); + final Predicate predicate = msender.isOverriding() ? null : MPerm::isVisible; Bukkit.getScheduler().runTaskAsynchronously(Factions.get(), new Runnable() { diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java index 118de24d..a33ec558 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java @@ -4,17 +4,12 @@ import com.massivecraft.factions.cmd.type.TypeFaction; import com.massivecraft.factions.cmd.type.TypeMPerm; import com.massivecraft.factions.cmd.type.TypeMPermable; import com.massivecraft.factions.entity.Faction; -import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MPerm; -import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.event.EventFactionsPermChange; import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.command.type.primitive.TypeBooleanYes; import com.massivecraft.massivecore.util.Txt; -import java.util.ArrayList; -import java.util.List; - public class CmdFactionsPermSet extends FactionsCommand { // -------------------------------------------- // @@ -43,7 +38,6 @@ public class CmdFactionsPermSet extends FactionsCommand Faction faction = this.readArgAt(3, msenderFaction); MPerm.MPermable permable = TypeMPermable.get(faction).read(this.argAt(1), sender); - // Do the sender have the right to change perms for this faction? if ( ! MPerm.getPermPerms().has(msender, faction, true)) return; @@ -51,8 +45,12 @@ public class CmdFactionsPermSet extends FactionsCommand // Is this perm editable? if ( ! msender.isOverriding() && ! perm.isEditable()) { - msg("The perm %s is not editable.", perm.getName()); - return; + throw new MassiveException().addMsg("The perm %s is not editable.", perm.getName()); + } + + if (permable == faction) + { + throw new MassiveException().addMsg("A faction can't have perms for itself. Perhaps try ranks."); } // Event @@ -67,35 +65,18 @@ public class CmdFactionsPermSet extends FactionsCommand // No change if (!change) { - msg("%s already has %s set to %s for %s.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "YES" : "NOO"), permable.getColor() + permable.getName() + "s"); - return; + throw new MassiveException().addMsg("%s already has %s set to %s for %s.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "YES" : "NOO"), permable.getDisplayName(msender)); } - // The following is to make sure the leader always has the right to change perms if that is our goal. - if (perm == MPerm.getPermPerms() && MConf.get().defaultPermsLeader.contains(MPerm.ID_PERMS)) + if (perm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains("LEADER")) { faction.setPermitted( faction.getLeaderRank(), MPerm.getPermPerms(), true); } - - // Create messages - List messages = new ArrayList<>(); - + // Inform sender - messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true))); - messages.add(MPerm.getStateHeaders(faction)); - messages.add(Txt.parse(perm.getStateInfo(faction, true))); - message(messages); - - // Inform faction (their message is slighly different) - List recipients = faction.getMPlayers(); - recipients.remove(msender); - - for (MPlayer recipient : recipients) - { - recipient.msg("%s set a perm for %s.", msender.describeTo(recipient, true), faction.describeTo(recipient, true)); - recipient.message(messages); - } + String yesNo = Txt.parse(value ? "YES" : "NOO"); + msg("Set perm %s to %s for %s in %s.", perm.getName(), yesNo, permable.getDisplayName(msender), faction.describeTo(msender)); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java index 944980e5..9bcf784b 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java @@ -1,16 +1,19 @@ package com.massivecraft.factions.cmd; +import com.massivecraft.factions.Rel; import com.massivecraft.factions.cmd.type.TypeFaction; import com.massivecraft.factions.cmd.type.TypeMPerm; import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.entity.MPlayerColl; +import com.massivecraft.factions.entity.Rank; import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.command.type.container.TypeSet; +import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.util.Txt; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; +import java.util.stream.Collectors; public class CmdFactionsPermShow extends FactionsCommand { @@ -21,8 +24,8 @@ public class CmdFactionsPermShow extends FactionsCommand public CmdFactionsPermShow() { // Parameters + this.addParameter(TypeMPerm.get(), "perm"); this.addParameter(TypeFaction.get(), "faction", "you"); - this.addParameter(TypeSet.get(TypeMPerm.get()), "perms", "all", true); } // -------------------------------------------- // @@ -33,21 +36,48 @@ public class CmdFactionsPermShow extends FactionsCommand public void perform() throws MassiveException { // Arg: Faction + MPerm mperm = this.readArg(); Faction faction = this.readArg(msenderFaction); - Collection mperms = this.readArg(MPerm.getAll()); + + var permittedIds = faction.getPerms().get(mperm.getId()); + var permables = new MassiveList(); + + for (var permitted : permittedIds) + { + permables.add(idToMPermable(permitted)); + } + + var removeString = Txt.parse(" of ") + faction.getDisplayName(msender); + var permableList = permables.stream() + .map(permable -> permable.getDisplayName(msender)) + .map(s -> s.replace(removeString, "")) + .collect(Collectors.toList()); + String permableNames = Txt.implodeCommaAnd(permableList, Txt.parse("")); // Create messages - List messages = new ArrayList<>(); + msg("In %s permission %s is granted to %s.", faction.describeTo(msender), mperm.getName(), permableNames); + } - messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true))); - messages.add(MPerm.getStateHeaders(faction)); - for (MPerm mperm : mperms) + public static MPerm.MPermable idToMPermable(String id) + { + MPlayer mplayer = MPlayerColl.get().get(id, false); + if (mplayer != null) return mplayer; + + Faction faction = Faction.get(id); + if (faction != null) return faction; + + for (var f : FactionColl.get().getAll()) { - messages.add(Txt.parse(mperm.getStateInfo(faction, true))); + Rank rank = f.getRank(id); + if (rank != null) return rank; } - - // Send messages - message(messages); + + if (Rel.ALLY.name().equalsIgnoreCase(id)) return Rel.ALLY; + if (Rel.TRUCE.name().equalsIgnoreCase(id)) return Rel.TRUCE; + if (Rel.NEUTRAL.name().equalsIgnoreCase(id)) return Rel.NEUTRAL; + if (Rel.ENEMY.name().equalsIgnoreCase(id)) return Rel.ENEMY; + + throw new RuntimeException(id); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermShow2.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermShow2.java new file mode 100644 index 00000000..9ed2f17d --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermShow2.java @@ -0,0 +1,100 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeFaction; +import com.massivecraft.factions.cmd.type.TypeMPermable; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.entity.Rank; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.util.MUtil; +import com.massivecraft.massivecore.util.Txt; +import org.bukkit.ChatColor; + +import java.util.stream.Collectors; + +public class CmdFactionsPermShow2 extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsPermShow2() + { + // Parameters + this.addParameter(TypeMPermable.get(), "rank/rel/player/faction"); + this.addParameter(TypeFaction.get(), "faction", "you"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Arg: Faction + Faction faction = this.readArgAt(1, msenderFaction); + var permableType = TypeMPermable.get(faction); + MPerm.MPermable permable = permableType.read(this.argAt(0), sender); + + if (permable == faction) + { + throw new MassiveException().addMsg("A faction can't have perms for itself."); + } + + var perms = new MassiveList(); + + for (var mperm : MPerm.getAll()) + { + if (faction.isPermitted(permable.getId(), mperm.getId())) perms.add(mperm); + } + + if (perms.isEmpty()) + { + msg("In %s %s specifically has no permissions.", faction.describeTo(msender), permable.getDisplayName(sender)); + } + else + { + var permNames = perms.stream().map(perm -> Txt.parse("") + perm.getName()).collect(Collectors.toList()); + String names = Txt.implodeCommaAnd(permNames, Txt.parse("")); + + // Create messages + var permissionSingularPlural = permNames.size() == 1 ? "permission" : "permissions"; + msg("In %s %s specifically has the %s: %s.", faction.describeTo(msender), permable.getDisplayName(sender), permissionSingularPlural, names); + } + if (permable instanceof MPlayer) + { + MPlayer mplayer = (MPlayer) permable; + msg("They may have other permissions through their faction membership, rank or relation to %s.", faction.describeTo(msender)); + + var msons = new MassiveList(); + + if (mplayer.getFaction() != faction) msons.add(Mson.parse("[faction]").command(this, mplayer.getFaction().getName(), faction.getName())); + msons.add(Mson.parse("[rank]").command(this, mplayer.getFaction().getName() + "-" + mplayer.getRank().getName(), faction.getName())); + if (mplayer.getFaction() != faction) msons.add(Mson.parse("[relation]").command(this, faction.getRelationTo(mplayer).toString(), faction.getName())); + Mson msons2 = Mson.implode(msons, Mson.SPACE); + message(mson(mson("Commands: ").color(ChatColor.YELLOW), msons2)); + } + if (permable instanceof Faction) + { + Faction faction1 = (Faction) permable; + msg("They may have other permissions through their relation to %s.", faction.describeTo(msender)); + Mson msonRelation = Mson.parse("[relation]").command(this, faction.getRelationTo(faction1).toString(), faction.getName()); + Mson msons = Mson.implode(MUtil.list(msonRelation), Mson.SPACE); + message(mson(mson("Commands: ").color(ChatColor.YELLOW), msons)); + } + if (permable instanceof Rank && !faction.hasRank((Rank) permable)) + { + Rank rank = (Rank) permable; + msg("They may have other permissions thorugh their faction membership or relation to %s.", faction.describeTo(msender)); + Mson msonFaction = Mson.parse("[faction]").command(this, rank.getFaction().getName(), faction.getName()); + Mson msonRelation = Mson.parse("[relation]").command(this, faction.getRelationTo(rank.getFaction()).toString(), faction.getName()); + Mson msons = Mson.implode(MUtil.list(msonFaction, msonRelation), Mson.SPACE); + message(mson(mson("Commands: ").color(ChatColor.YELLOW), msons)); + } + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankEditCreate.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankEditCreate.java index 9403095a..5123a246 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsRankEditCreate.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankEditCreate.java @@ -6,6 +6,7 @@ import com.massivecraft.factions.entity.Rank; import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.command.type.primitive.TypeInteger; import com.massivecraft.massivecore.command.type.primitive.TypeString; +import com.massivecraft.massivecore.command.type.primitive.TypeStringParsed; public class CmdFactionsRankEditCreate extends FactionsCommand { @@ -18,7 +19,7 @@ public class CmdFactionsRankEditCreate extends FactionsCommand // Parameters this.addParameter(TypeString.get(), "name"); this.addParameter(TypeInteger.get(), "priority"); - this.addParameter("", TypeString.get(), "prefix", "none"); + this.addParameter("", TypeStringParsed.get(), "prefix", "none"); this.addParameter(TypeFaction.get(), "faction", "you"); } diff --git a/src/com/massivecraft/factions/cmd/type/TypeMPermable.java b/src/com/massivecraft/factions/cmd/type/TypeMPermable.java index 16b08c3d..f0b498c8 100644 --- a/src/com/massivecraft/factions/cmd/type/TypeMPermable.java +++ b/src/com/massivecraft/factions/cmd/type/TypeMPermable.java @@ -1,13 +1,20 @@ package com.massivecraft.factions.cmd.type; +import com.massivecraft.factions.Rel; import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.Rank; -import com.massivecraft.massivecore.command.type.TypeAbstractChoice; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.command.type.TypeAbstract; +import org.bukkit.command.CommandSender; -import java.util.Collections; +import java.util.Collection; +import java.util.stream.Collectors; -public class TypeMPermable extends TypeAbstractChoice +public class TypeMPermable extends TypeAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -20,7 +27,6 @@ public class TypeMPermable extends TypeAbstractChoice super(Rank.class); this.faction = null; - this.setAll(Collections.emptyList()); } public static TypeMPermable get(Faction faction) { return new TypeMPermable(faction); } @@ -30,10 +36,6 @@ public class TypeMPermable extends TypeAbstractChoice if (faction == null) throw new NullPointerException("faction"); this.faction = faction; - - var permables = MPerm.getPermables(faction); - - this.setAll(permables); } // -------------------------------------------- // @@ -43,4 +45,100 @@ public class TypeMPermable extends TypeAbstractChoice private final Faction faction; public Faction getFaction() { return this.faction; } + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public MPerm.MPermable read(String arg, CommandSender sender) throws MassiveException + { + TypeRank typeRank = new TypeRank(this.getFaction()); + try + { + Rank rank = typeRank.read(arg, sender); + return rank; + } + catch (MassiveException ex) + { + // Do nothing + } + + try + { + Rel rel = TypeRelation.get().read(arg, sender); + return rel; + } + catch (MassiveException ex) + { + // Do nothing + } + + try + { + MPlayer mplayer = TypeMPlayer.get().read(arg, sender); + return mplayer; + } + catch (MassiveException ex) + { + // Do nothing + } + + try + { + Faction faction = TypeFaction.get().read(arg, sender); + return faction; + } + catch (MassiveException ex) + { + // Do nothing + } + + if (arg.contains("-")) + { + int idx = arg.indexOf('-'); + String factionName = arg.substring(0, idx); + String rankName = arg.substring(idx+1); + + Faction faction = TypeFaction.get().read(factionName, sender); + Rank rank = TypeRank.get(faction).read(rankName, sender); + return rank; + } + + throw new MassiveException().addMsg("No rank, relation, player or faction matches: %s.", arg); + } + + public Collection getTabList(CommandSender sender, String arg) + { + var ret = new MassiveList(); + var faction = this.getFaction(); + if (faction == null) faction = MPlayer.get(sender).getFaction(); + ret.addAll(faction.getRanks().getAll().stream().map(Rank::getName).collect(Collectors.toList())); + ret.addAll(TypeRelation.get().getTabList(sender, arg)); + ret.addAll(TypeFaction.get().getTabList(sender, arg)); + + // Faction specific ranks + if (arg.length() >= 3) + { + for (var f : FactionColl.get().getAll()) + { + String name = f.getName(); + if (arg.length() <= name.length() && !name.toLowerCase().startsWith(arg.toLowerCase())) continue; + if (arg.length() > name.length() && !arg.toLowerCase().startsWith(name.toLowerCase())) continue; + + ret.addAll(f.getRanks().getAll().stream().map(r -> name + "-" + r.getName()).collect(Collectors.toList())); + } + } + + return ret; + } + + + @Override + public boolean isValid(String arg, CommandSender sender) + { + // In the generic case accept all + if (this.getFaction() == null) return true; + else return super.isValid(arg, sender); + } + } diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 0b97a8b0..5f81374f 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -158,7 +158,7 @@ public class Faction extends Entity implements FactionsParticipator, MP // The perm overrides are modifications to the default values. // Null means default. - private Map> perms = new MassiveMap<>(); + private Map> perms = this.createNewPermMap(); // -------------------------------------------- // // FIELD: id @@ -751,7 +751,7 @@ public class Faction extends Entity implements FactionsParticipator, MP } // -------------------------------------------- // - // FIELD: permOverrides + // FIELD: perms // -------------------------------------------- // public Map> getPerms() @@ -759,6 +759,32 @@ public class Faction extends Entity implements FactionsParticipator, MP return this.perms; } + public Map> createNewPermMap() + { + Map> ret = new MassiveMap<>(); + + var leaderId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("leader")).map(Rank::getId).findFirst(); + var officerId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("officer")).map(Rank::getId).findFirst(); + var memberId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("member")).map(Rank::getId).findFirst(); + var recruitId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("recruit")).map(Rank::getId).findAny(); + + for (var mperm : MPerm.getAll()) + { + var id = mperm.getId(); + var value = new MassiveSet<>(mperm.getStandard()); + + if (value.remove("LEADER") && leaderId.isPresent()) value.add(leaderId.get()); + if (value.remove("OFFICER") && officerId.isPresent()) value.add(officerId.get()); + if (value.remove("MEMBER") && memberId.isPresent()) value.add(memberId.get()); + if (value.remove("RECRUIT") && recruitId.isPresent()) value.add(recruitId.get()); + + ret.put(mperm.getId(), value); + } + + return ret; + } + + // IS PERMITTED public boolean isPlayerPermitted(MPlayer mplayer, String permId) @@ -789,19 +815,30 @@ public class Faction extends Entity implements FactionsParticipator, MP return isFactionPermitted(faction, mperm.getId()); } - @Deprecated - public boolean isPermablePermitted(MPerm.MPermable permable, String permId) + public Set getPermitted(String permId) { - return isPermitted(permable.getId(), permId); + if (permId == null) throw new NullPointerException("permId"); + var permables = this.perms.get(permId); + + if (permables == null) + { + // No perms was found, but likely this is just a new MPerm. + // So if this does not exist in the database, throw an error. + if (!doesPermExist(permId)) throw new NullPointerException(permId + " caused null"); + + permables = new MassiveSet<>(); + this.perms.put(permId, permables); + } + + return permables; } - @Deprecated - public boolean isPermablePermitted(MPerm.MPermable permable, MPerm mperm) + public Set getPermitted(MPerm mperm) { - return isPermablePermitted(permable, mperm.getId()); + return getPermitted(mperm.getId()); } - private boolean isPermitted(String permableId, String permId) + public boolean isPermitted(String permableId, String permId) { if (permableId == null) throw new NullPointerException("permableId"); if (permId == null) throw new NullPointerException("permId"); @@ -817,7 +854,7 @@ public class Faction extends Entity implements FactionsParticipator, MP return false; } - return permables.contains(permableId); + return getPermitted(permId).contains(permableId); } // SET PERMITTED @@ -873,7 +910,6 @@ public class Faction extends Entity implements FactionsParticipator, MP return MPermColl.get().getFixed(permId) != null; } - // -------------------------------------------- // // OVERRIDE: RelationParticipator // -------------------------------------------- // @@ -907,7 +943,20 @@ public class Faction extends Entity implements FactionsParticipator, MP { return RelationUtil.getColorOfThatToMe(this, observer); } - + + // -------------------------------------------- // + // OVERRIDE: permable + // -------------------------------------------- // + + @Override + public String getDisplayName(Object senderObject) + { + MPlayer mplayer = MPlayer.get(senderObject); + if (mplayer == null) return this.getName(); + + return this.describeTo(mplayer); + } + // -------------------------------------------- // // POWER // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index e9b0b740..2f010635 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -280,14 +280,14 @@ public class MConf extends Entity // PERMS // -------------------------------------------- // - public List defaultPermsEnemy = MUtil.list(MPerm.ID_DEPOSIT); + /*public List defaultPermsEnemy = MUtil.list(MPerm.ID_DEPOSIT); public List defaultPermsNeutral = MUtil.list(MPerm.ID_DEPOSIT); public List defaultPermsTruce = MUtil.list(MPerm.ID_DEPOSIT); public List defaultPermsAlly = MUtil.list(MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_HOME, MPerm.ID_CLAIMNEAR); public List defaultPermsRecruit = MUtil.list(MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_HOME, MPerm.ID_CLAIMNEAR); public List defaultPermsMember = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_HOME, MPerm.ID_CLAIMNEAR); public List defaultPermsOfficer = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_DESC, MPerm.ID_MOTD, MPerm.ID_INVITE, MPerm.ID_KICK, MPerm.ID_RANK, MPerm.ID_TITLE, MPerm.ID_HOME, MPerm.ID_SETHOME, MPerm.ID_TERRITORY, MPerm.ID_ACCESS, MPerm.ID_CLAIMNEAR, MPerm.ID_REL); - public List defaultPermsLeader = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_NAME, MPerm.ID_DESC, MPerm.ID_MOTD, MPerm.ID_INVITE, MPerm.ID_KICK, MPerm.ID_RANK, MPerm.ID_TITLE, MPerm.ID_HOME, MPerm.ID_SETHOME, MPerm.ID_WITHDRAW, MPerm.ID_TERRITORY, MPerm.ID_ACCESS, MPerm.ID_CLAIMNEAR, MPerm.ID_REL, MPerm.ID_DISBAND, MPerm.ID_FLAGS, MPerm.ID_FLAGS); + public List defaultPermsLeader = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_NAME, MPerm.ID_DESC, MPerm.ID_MOTD, MPerm.ID_INVITE, MPerm.ID_KICK, MPerm.ID_RANK, MPerm.ID_TITLE, MPerm.ID_HOME, MPerm.ID_SETHOME, MPerm.ID_WITHDRAW, MPerm.ID_TERRITORY, MPerm.ID_ACCESS, MPerm.ID_CLAIMNEAR, MPerm.ID_REL, MPerm.ID_DISBAND, MPerm.ID_FLAGS, MPerm.ID_FLAGS);*/ // -------------------------------------------- // // TERRITORY INFO diff --git a/src/com/massivecraft/factions/entity/MPerm.java b/src/com/massivecraft/factions/entity/MPerm.java index 2e9e063e..222492ef 100644 --- a/src/com/massivecraft/factions/entity/MPerm.java +++ b/src/com/massivecraft/factions/entity/MPerm.java @@ -18,7 +18,6 @@ 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.ChatColor; import org.bukkit.entity.Player; import java.util.ArrayList; @@ -417,7 +416,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N return list; } - public static String getStateHeaders(Faction faction) + /*public static String getStateHeaders(Faction faction) { if (faction == null) throw new NullPointerException("faction"); @@ -469,7 +468,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N if (withDesc) ret += " " + this.getDesc(); return ret; - } + }*/ public interface MPermable extends Named, Identified { @@ -488,22 +487,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N return this.getName().substring(0, 3).toUpperCase(); } - default ChatColor getColor() - { - if (this.isRelation()) - { - throw new RuntimeException(); - } - else if (this.isRank()) - { - return MConf.get().colorMember; - } - else - { - throw new RuntimeException(); - } - } - + String getDisplayName(Object senderObject); } } diff --git a/src/com/massivecraft/factions/entity/Rank.java b/src/com/massivecraft/factions/entity/Rank.java index fc91cfcc..843fef54 100644 --- a/src/com/massivecraft/factions/entity/Rank.java +++ b/src/com/massivecraft/factions/entity/Rank.java @@ -1,6 +1,8 @@ package com.massivecraft.factions.entity; import com.massivecraft.massivecore.store.EntityInternal; +import com.massivecraft.massivecore.store.EntityInternalMap; +import com.massivecraft.massivecore.util.Txt; import org.bukkit.ChatColor; public class Rank extends EntityInternal implements MPerm.MPermable @@ -17,6 +19,20 @@ public class Rank extends EntityInternal implements MPerm.MPermable return this; } + @Override + public void preDetach(String id) + { + for (var f : FactionColl.get().getAll()) + { + for (var it = f.getPerms().entrySet().iterator(); it.hasNext();) + { + var entry = it.next(); + var value = entry.getValue(); + value.remove(id); + } + } + } + // -------------------------------------------- // // FIELDS // -------------------------------------------- // @@ -33,6 +49,13 @@ public class Rank extends EntityInternal implements MPerm.MPermable public String getPrefix() { return this.prefix; } public void setPrefix(String prefix) { this.prefix = prefix; this.changed(); } + public Faction getFaction() + { + var internalMap = (EntityInternalMap) this.getContainer(); + var faction = (Faction) internalMap.getEntity(); + return faction; + } + // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // @@ -54,6 +77,14 @@ public class Rank extends EntityInternal implements MPerm.MPermable // VISUAL // -------------------------------------------- // + @Override + public String getDisplayName(Object senderObject) + { + String ret = this.getVisual(); + ret += Txt.parse(" of ") + this.getFaction().getDisplayName(senderObject); + return ret; + } + public String getVisual() { String ret = "";