Factions3/src/com/massivecraft/factions/cmd/CmdFactionsPermViewall.java
Magnus Ulf 96989ee653 F perm viewall
This command allows someone to see all the perms held by a rank/rel/player/faction
2018-12-30 10:48:23 +01:00

104 lines
3.1 KiB
Java

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("<b>A faction can't have perms for itself.");
}
// Create list of all applicable permables
var permables = new MassiveList<MPermable>();
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<MPerm>();
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("<i>In <reset>%s <reset>%s <i>has <b>no permissions<i>.", faction.describeTo(msender), permable.getDisplayName(sender));
}
else
{
var permNames = perms.stream().map(perm -> Txt.parse("<h>") + perm.getName()).collect(Collectors.toList());
String names = Txt.implodeCommaAnd(permNames, Txt.parse("<i>"));
// Create messages
var permissionSingularPlural = permNames.size() == 1 ? "permission" : "permissions";
msg("<i>In <reset>%s <reset>%s <i>has the %s: <reset>%s<i> either specifically granted to them or through rank, relation or faction membership.", faction.describeTo(msender), permable.getDisplayName(sender), permissionSingularPlural, names);
}
}
}