From 96989ee653118a28993bca4aae89b26d53de1d30 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Sun, 30 Dec 2018 10:48:23 +0100 Subject: [PATCH] F perm viewall This command allows someone to see all the perms held by a rank/rel/player/faction --- plugin.yml | 3 + src/com/massivecraft/factions/Perm.java | 1 + .../factions/cmd/CmdFactionsPerm.java | 1 + .../factions/cmd/CmdFactionsPermSet.java | 2 +- .../factions/cmd/CmdFactionsPermViewall.java | 103 ++++++++++++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsPermViewall.java diff --git a/plugin.yml b/plugin.yml index 9380638a..88c54200 100644 --- a/plugin.yml +++ b/plugin.yml @@ -64,6 +64,7 @@ permissions: factions.perm.set: {description: set perms, default: false} factions.perm.show: {description: show who has perm, default: false} factions.perm.view: {description: view perms given to, default: false} + factions.perm.viewall: {description: view all perms held by, default: false} factions.player: {description: show player information} factions.powerboost: {description: manage powerboost, default: false} factions.powerboost.faction: {description: show faction powerboost, default: false} @@ -163,6 +164,7 @@ permissions: factions.perm.set: true factions.perm.show: true factions.perm.view: true + factions.perm.viewall: true factions.player: true factions.powerboost: true factions.powerboost.faction: true @@ -285,6 +287,7 @@ permissions: factions.perm.set: true factions.perm.show: true factions.perm.view: true + factions.perm.viewall: true factions.player: true factions.promote: true factions.powerboost: true diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index 7a5f6abb..f0cb183a 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -59,6 +59,7 @@ public enum Perm implements Identified PERM_SET, PERM_SHOW, PERM_VIEW, + PERM_VIEWALL, PLAYER, POWERBOOST, POWERBOOST_PLAYER, diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java b/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java index f25ef7a8..5650810b 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPerm.java @@ -9,6 +9,7 @@ public class CmdFactionsPerm extends FactionsCommand CmdFactionsPermList cmdFactionsPermList = new CmdFactionsPermList(); CmdFactionsPermShow cmdFactionsPermShow = new CmdFactionsPermShow(); CmdFactionsPermView cmdFactionsPermView = new CmdFactionsPermView(); + CmdFactionsPermViewall cmdFactionsPermViewall = new CmdFactionsPermViewall(); CmdFactionsPermSet cmdFactionsPermSet = new CmdFactionsPermSet(); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java index a33ec558..410d30c0 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java @@ -20,7 +20,7 @@ public class CmdFactionsPermSet extends FactionsCommand { // Parameters this.addParameter(TypeMPerm.get(), "perm"); - this.addParameter(TypeMPermable.get(), "relation"); + this.addParameter(TypeMPermable.get(), "rank/rel/player/faction"); this.addParameter(TypeBooleanYes.get(), "yes/no"); this.addParameter(TypeFaction.get(), "faction", "you"); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermViewall.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermViewall.java new file mode 100644 index 00000000..53929a4d --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermViewall.java @@ -0,0 +1,103 @@ +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.MPerm.MPermable; +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.util.Txt; + +import java.util.stream.Collectors; + +public class CmdFactionsPermViewall extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsPermViewall() + { + // 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); + + // Self check + if (permable == faction) + { + throw new MassiveException().addMsg("A faction can't have perms for itself."); + } + + // Create list of all applicable permables + var permables = new MassiveList(); + permables.add(permable); + + if (permable instanceof MPlayer) + { + MPlayer mplayer = (MPlayer) permable; + permables.add(mplayer.getFaction()); + permables.add(mplayer.getRank()); + permables.add(faction.getRelationTo(mplayer)); + } + if (permable instanceof Faction) + { + Faction faction1 = (Faction) permable; + permables.add(faction.getRelationTo(faction1)); + } + if (permable instanceof Rank && !faction.hasRank((Rank) permable)) + { + Rank rank = (Rank) permable; + Faction faction1 = rank.getFaction(); + permables.add(faction1); + permables.add(faction.getRelationTo(faction1)); + } + + // Find the perms they have + var perms = new MassiveList(); + + perm: + for (var mperm : MPerm.getAll()) + { + String mpermId = mperm.getId(); + permable: + for (var mpa : permables) + { + if (!faction.isPermitted(mpa.getId(), mperm.getId())) continue permable; + perms.add(mperm); + continue perm; + } + + } + + if (perms.isEmpty()) + { + msg("In %s %s 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 has the %s: %s either specifically granted to them or through rank, relation or faction membership.", faction.describeTo(msender), permable.getDisplayName(sender), permissionSingularPlural, names); + } + } + +}