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.List; 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); TypeMPermable 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 List permables = new MassiveList<>(); permables.add(permable); if (permable instanceof MPlayer mplayer) { permables.add(mplayer.getFaction()); permables.add(mplayer.getRank()); permables.add(faction.getRelationTo(mplayer)); } if (permable instanceof Faction faction1) { permables.add(faction.getRelationTo(faction1)); } if (permable instanceof Rank rank && !faction.hasRank((Rank) permable)) { Faction faction1 = rank.getFaction(); permables.add(faction1); permables.add(faction.getRelationTo(faction1)); } // Find the perms they have List perms = new MassiveList<>(); perm: for (MPerm mperm : MPerm.getAll()) { String mpermId = mperm.getId(); permable: for (MPermable 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 { List permNames = perms.stream().map(perm -> Txt.parse("") + perm.getName()).collect(Collectors.toList()); String names = Txt.implodeCommaAnd(permNames, Txt.parse("")); // Create messages String 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); } } }