Factions/src/com/massivecraft/factions/cmd/CmdFactionsPermSet.java

102 lines
3.2 KiB
Java
Raw Normal View History

package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Rel;
2017-04-06 22:32:52 +02:00
import com.massivecraft.factions.Selector;
import com.massivecraft.factions.cmd.req.RequirementHasMPerm;
import com.massivecraft.factions.cmd.type.TypeMPerm;
2017-04-06 22:32:52 +02:00
import com.massivecraft.factions.cmd.type.TypeSelector;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.event.EventFactionsPermChange;
2017-04-06 22:32:52 +02:00
import com.massivecraft.massivecore.Button;
2015-02-12 12:00:55 +01:00
import com.massivecraft.massivecore.MassiveException;
2017-04-06 22:32:52 +02:00
import com.massivecraft.massivecore.mson.Mson;
import org.bukkit.ChatColor;
2017-04-06 22:32:52 +02:00
public abstract class CmdFactionsPermSet extends FactionsCommand
{
2017-04-06 22:32:52 +02:00
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private boolean add;
public boolean isAdd() { return this.add; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
2017-04-06 22:32:52 +02:00
public CmdFactionsPermSet(boolean add)
{
2017-04-06 22:32:52 +02:00
// Add/Remove
this.add = add;
// Parameters
2017-04-06 22:32:52 +02:00
this.addParameter(TypeMPerm.get(), "permission");
this.addParameter(TypeSelector.get(), "selector");
// Requirements
this.addRequirements(RequirementHasMPerm.get(MPerm.getPermPerms()));
}
2017-04-06 22:32:52 +02:00
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
2015-02-12 12:00:55 +01:00
public void perform() throws MassiveException
{
2017-04-06 22:32:52 +02:00
// Parameter
2015-05-06 17:04:35 +02:00
MPerm perm = this.readArg();
2017-04-06 22:32:52 +02:00
Selector selector = this.readArg();
Faction faction = msender.getUsedFaction();
boolean adding = this.isAdd();
// Is this perm editable?
2017-04-06 22:32:52 +02:00
if (!msender.isOverriding() && !perm.isEditable())
{
msg("<b>The perm <h>%s <b>is not editable.", perm.getName());
return;
}
2017-04-06 22:32:52 +02:00
// Visuals
String visualSelector = TypeSelector.get().getVisual(selector);
String visualFaction = faction.describeTo(msender);
String visualPerm = perm.getDesc(true, false);
// No change
2017-04-06 22:32:52 +02:00
if (faction.isPermitted(perm, selector) == adding)
{
2017-04-06 22:32:52 +02:00
String already = "already";
if (!adding) already += " not";
msg("%s <i>is %s permitted for faction %s<i> and perm %s<i>.", visualSelector, already, visualFaction, visualPerm);
return;
}
2017-04-06 22:32:52 +02:00
// Event
EventFactionsPermChange event = new EventFactionsPermChange(sender, faction, perm, selector, adding);
event.run();
if (event.isCancelled()) return;
adding = event.getNewValue();
// The following is to make sure the leader always has the right to change perms if that is our goal.
2017-04-06 22:32:52 +02:00
if (perm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains(Rel.LEADER) && !adding)
{
2017-04-06 22:32:52 +02:00
throw new MassiveException().setMsg("<b>You are not allowed to remove the leader from the perm <h>perms<b>.");
}
2017-04-06 22:32:52 +02:00
// Apply
faction.setPermitted(perm, selector, adding);
2017-04-06 22:32:52 +02:00
// Inform
String addRemove = adding ? "added" : "removed";
Mson button = new Button()
.setName("Show")
.setCommand(CmdFactions.get().cmdFactionsPerm.cmdFactionsPermShow)
.setArgs(perm.getId())
.render();
faction.sendMessage(mson(visualSelector, " was ", addRemove, " to the perm ", visualPerm, ". ", button).color(ChatColor.YELLOW));
}
}