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

69 lines
2.2 KiB
Java
Raw Normal View History

2018-12-21 02:41:42 +01:00
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.entity.Faction;
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;
2018-12-22 01:55:46 +01:00
import com.massivecraft.massivecore.command.type.primitive.TypeStringParsed;
2018-12-21 02:41:42 +01:00
import java.util.Collection;
2018-12-21 02:41:42 +01:00
public class CmdFactionsRankEditCreate extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsRankEditCreate()
{
// Parameters
this.addParameter(TypeString.get(), "name");
this.addParameter(TypeInteger.get(), "priority");
2018-12-22 01:55:46 +01:00
this.addParameter("", TypeStringParsed.get(), "prefix", "none");
2018-12-21 02:41:42 +01:00
this.addParameter(TypeFaction.get(), "faction", "you");
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform() throws MassiveException
{
// Args
String name = this.readArg();
Integer priority = this.readArg();
String prefix = this.readArg();
Faction faction = this.readArg(msenderFaction);
CmdFactionsRankEdit.ensureAllowed(msender, faction, "create");
2018-12-21 02:41:42 +01:00
Collection<Rank> ranks = faction.getRanks().getAll();
2018-12-21 02:41:42 +01:00
if (ranks.stream().map(Rank::getName).anyMatch(s -> s.equalsIgnoreCase(name)))
{
throw new MassiveException().addMsg("<b>There is already a rank called <h>%s<b>.", name);
}
if (ranks.stream().map(Rank::getPriority).anyMatch(i -> i.equals(priority)))
{
throw new MassiveException().addMsg("<b>There is already a with priority <h>%s<b>.", priority);
}
if (priority > faction.getLeaderRank().getPriority())
{
throw new MassiveException().addMsg("<b>You can't create a rank of higher priority than the leader rank.");
}
Rank rank = new Rank(name, priority, prefix);
faction.getRanks().attach(rank);
// Inform
msg("<i>You created the rank <reset>%s<i>.", rank.getVisual());
msg("<i>You might want to change its permissions:");
CmdFactions.get().cmdFactionsPerm.getTemplate(false, true, sender).messageOne(msender);
}
}