Factions/src/main/java/com/massivecraft/factions/cmd/CmdFactionsCreate.java

97 lines
2.8 KiB
Java
Raw Normal View History

2011-10-09 21:57:43 +02:00
package com.massivecraft.factions.cmd;
2011-03-19 13:00:03 +01:00
import java.util.ArrayList;
2013-04-09 13:00:09 +02:00
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.req.ReqHasntFaction;
import com.massivecraft.factions.entity.MPlayer;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPlayerColl;
2014-06-04 16:47:01 +02:00
import com.massivecraft.factions.event.EventFactionsCreate;
import com.massivecraft.factions.event.EventFactionsMembershipChange;
import com.massivecraft.factions.event.EventFactionsMembershipChange.MembershipChangeReason;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.store.MStore;
2011-03-19 13:00:03 +01:00
2014-09-18 13:41:20 +02:00
public class CmdFactionsCreate extends FactionsCommand
{
2013-11-11 09:31:04 +01:00
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
2013-04-10 13:12:22 +02:00
public CmdFactionsCreate()
{
2013-11-11 09:31:04 +01:00
// Aliases
2013-04-16 10:11:59 +02:00
this.addAliases("create");
2013-11-11 09:31:04 +01:00
// Args
this.addRequiredArg("name");
2013-11-11 09:31:04 +01:00
// Requirements
this.addRequirements(ReqHasntFaction.get());
2013-04-16 10:11:59 +02:00
this.addRequirements(ReqHasPerm.get(Perm.CREATE.node));
2011-03-23 17:39:56 +01:00
}
2013-11-11 09:31:04 +01:00
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
2011-03-23 17:39:56 +01:00
@Override
public void perform()
2013-11-11 09:31:04 +01:00
{
// Args
String newName = this.arg(0);
2011-03-19 13:00:03 +01:00
// Verify
if (FactionColl.get().isNameTaken(newName))
{
msg("<b>That name is already in use.");
2011-03-19 13:00:03 +01:00
return;
}
ArrayList<String> nameValidationErrors = FactionColl.get().validateName(newName);
if (nameValidationErrors.size() > 0)
{
sendMessage(nameValidationErrors);
2011-03-19 13:00:03 +01:00
return;
}
2012-03-02 02:16:45 +01:00
// Pre-Generate Id
2013-05-03 09:58:43 +02:00
String factionId = MStore.createId();
// Event
EventFactionsCreate createEvent = new EventFactionsCreate(sender, factionId, newName);
createEvent.run();
if (createEvent.isCancelled()) return;
// Apply
Faction faction = FactionColl.get().create(factionId);
faction.setName(newName);
2014-09-18 13:41:20 +02:00
msender.setRole(Rel.LEADER);
msender.setFaction(faction);
2013-04-12 08:56:26 +02:00
2014-09-18 13:41:20 +02:00
EventFactionsMembershipChange joinEvent = new EventFactionsMembershipChange(sender, msender, faction, MembershipChangeReason.CREATE);
joinEvent.run();
// NOTE: join event cannot be cancelled or you'll have an empty faction
2013-04-12 08:56:26 +02:00
// Inform
for (MPlayer follower : MPlayerColl.get().getAllOnline())
{
2014-09-18 13:41:20 +02:00
follower.msg("%s<i> created a new faction %s", msender.describeTo(follower, true), faction.getName(follower));
2011-03-19 13:00:03 +01:00
}
2013-04-16 11:27:03 +02:00
msg("<i>You should now: %s", Factions.get().getOuterCmdFactions().cmdFactionsDescription.getUseageTemplate());
if (MConf.get().logFactionCreate)
{
2014-09-18 13:41:20 +02:00
Factions.get().log(msender.getName()+" created a new faction: "+newName);
}
2011-03-19 13:00:03 +01:00
}
}