Reworked flag & perm cmd

Flag & Perm commands are now parent commands, and not so awkward.
This commit is contained in:
Magnus Ulf
2015-01-09 22:36:12 +01:00
committed by Olof Larsson
parent d6d5cb325d
commit afc45fea13
12 changed files with 691 additions and 325 deletions

View File

@@ -1,104 +1,34 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.arg.ARMFlag;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.event.EventFactionsFlagChange;
import com.massivecraft.massivecore.cmd.arg.ARBoolean;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsFlag extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsFlag()
{
// Aliases
this.addAliases("flag");
// Args
this.addOptionalArg("faction", "you");
this.addOptionalArg("flag", "all");
this.addOptionalArg("yes/no", "read");
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.FLAG.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Arg: Faction
Faction faction = this.arg(0, ARFaction.get(), msenderFaction);
if (faction == null) return;
// Case: Show All
if ( ! this.argIsSet(1))
{
msg(Txt.titleize("Flags for " + faction.describeTo(msender, true)));
for (MFlag mflag : MFlag.getAll())
{
if (!mflag.isVisible() && !msender.isUsingAdminMode()) continue;
msg(mflag.getStateDesc(faction.getFlag(mflag), true, true, true, true, false));
}
return;
}
// Arg: MFlag
MFlag mflag = this.arg(1, ARMFlag.get());
if (mflag == null) return;
// Case: Show One
if ( ! this.argIsSet(2))
{
msg(Txt.titleize("Flag for " + faction.describeTo(msender, true)));
msg(mflag.getStateDesc(faction.getFlag(mflag), true, true, true, true, false));
return;
}
// Do the sender have the right to change flags for this faction?
if ( ! MPerm.getPermFlags().has(msender, faction, true)) return;
// Is this flag editable?
if (!msender.isUsingAdminMode() && !mflag.isEditable())
{
msg("<b>The flag <h>%s <b>is not editable.", mflag.getName());
return;
}
// Arg: Target Value
Boolean targetValue = this.arg(2, ARBoolean.get());
if (targetValue == null) return;
// Event
EventFactionsFlagChange event = new EventFactionsFlagChange(sender, faction, mflag, targetValue);
event.run();
if (event.isCancelled()) return;
targetValue = event.isNewValue();
// Apply
faction.setFlag(mflag, targetValue);
// Inform
String stateInfo = mflag.getStateDesc(faction.getFlag(mflag), true, false, true, true, true);
if (msender.getFaction() != faction)
{
// Send message to sender
msg("<h>%s <i>set a flag for <h>%s", msender.describeTo(msender, true), faction.describeTo(msender, true));
msg(stateInfo);
}
faction.msg("<h>%s <i>set a flag for <h>%s", msender.describeTo(faction, true), faction.describeTo(faction, true));
faction.msg(stateInfo);
}
}
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
public class CmdFactionsFlag extends FactionsCommand
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
CmdFactionsFlagList cmdFactionsFlagList = new CmdFactionsFlagList();
CmdFactionsFlagShow cmdFactionsFlagShow = new CmdFactionsFlagShow();
CmdFactionsFlagSet cmdFactionsFlagSet = new CmdFactionsFlagSet();
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsFlag()
{
// Aliases
this.addAliases("flag");
// Subcommands
this.addSubCommand(this.cmdFactionsFlagList);
this.addSubCommand(this.cmdFactionsFlagShow);
this.addSubCommand(this.cmdFactionsFlagSet);
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.FLAG.node));
}
}

View File

@@ -0,0 +1,54 @@
package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import java.util.List;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.massivecore.cmd.arg.ARInteger;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsFlagList extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsFlagList()
{
// Aliases
this.addAliases("l", "list");
// Args
this.addOptionalArg("page", "1");
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.FLAG_LIST.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Args
Integer pageHumanBased = this.arg(0, ARInteger.get(), 1);
if (pageHumanBased == null) return;
//Create messages
List<String> messages = new ArrayList<String>();
for (MFlag flag : MFlag.getAll())
{
if ( ! flag.isVisible() && ! msender.isUsingAdminMode()) continue;
messages.add(flag.getStateDesc(false, false, true, true, true, false));
}
//Send messages
sendMessage(Txt.getPage(messages, pageHumanBased, "Available Faction Flags", sender));
}
}

View File

@@ -0,0 +1,86 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.cmd.arg.ARMFlag;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.event.EventFactionsFlagChange;
import com.massivecraft.massivecore.cmd.arg.ARBoolean;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
public class CmdFactionsFlagSet extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsFlagSet()
{
// Aliases
this.addAliases("set");
// Args
this.addRequiredArg("flag");
this.addRequiredArg("yes/no");
this.addOptionalArg("faction", "you");
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.FLAG_SET.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Args
MFlag flag = this.arg(0, ARMFlag.get());
if (flag == null) return;
Boolean value = this.arg(1, ARBoolean.get());
if (value == null) return;
Faction faction = this.arg(2, ARFaction.get(), msenderFaction);
if (faction == null) return;
// Do the sender have the right to change flags for this faction?
if ( ! MPerm.getPermFlags().has(msender, faction, true)) return;
// Is this flag editable?
if (!msender.isUsingAdminMode() && ! flag.isEditable())
{
msg("<b>The flag <h>%s <b>is not editable.", flag.getName());
return;
}
// Event
EventFactionsFlagChange event = new EventFactionsFlagChange(sender, faction, flag, value);
event.run();
if (event.isCancelled()) return;
value = event.isNewValue();
// No change
if (faction.getFlag(flag) == value)
{
msg("%s <i>already has %s <i>set to %s<i>.", faction.describeTo(msender), flag.getStateDesc(value, false, true, true, false, true), flag.getStateDesc(value, true, true, false, false, false));
return;
}
// Apply
faction.setFlag(flag, value);
// Inform
String stateInfo = flag.getStateDesc(faction.getFlag(flag), true, false, true, true, true);
if (msender.getFaction() != faction)
{
// Send message to sender
msg("<h>%s <i>set a flag for <h>%s<i>.", msender.describeTo(msender, true), faction.describeTo(msender, true));
sendMessage(stateInfo);
}
faction.msg("<h>%s <i>set a flag for <h>%s<i>.", msender.describeTo(faction, true), faction.describeTo(faction, true));
faction.sendMessage(stateInfo);
}
}

View File

@@ -0,0 +1,77 @@
package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import java.util.List;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.cmd.arg.ARMFlag;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.massivecore.cmd.arg.ARList;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsFlagShow extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsFlagShow()
{
// Aliases
this.addAliases("s", "show");
// Args
this.addOptionalArg("faction", "you");
this.addOptionalArg("flags", "all");
this.setErrorOnToManyArgs(false);
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.FLAG_SHOW.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Arg: Faction
Faction faction = this.arg(0, ARFaction.get(), msenderFaction);
if (faction == null) return;
List<MFlag> flags = new ArrayList<MFlag>();
// Case: Show All
if ( ! this.argIsSet(1) || "all".equalsIgnoreCase(this.arg(1)))
{
for (MFlag mflag : MFlag.getAll())
{
if (!mflag.isVisible() && ! msender.isUsingAdminMode()) continue;
flags.add(mflag);
}
}
else
{
// Arg: MFlag. Maybe we should use ARSet but that is currently buggy.
List<MFlag> mflags = this.arg(this.argConcatFrom(1), ARList.get(ARMFlag.get()));
if (mflags == null) return;
flags.addAll(mflags);
}
// Create messages
List<String> messages = new ArrayList<String>();
messages.add(Txt.titleize("Flag for " + faction.describeTo(msender, true)));
for (MFlag mflag : flags)
{
messages.add(mflag.getStateDesc(faction.getFlag(mflag), true, true, true, true, true));
}
// Send messages
sendMessage(messages);
}
}

View File

@@ -1,111 +1,34 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.arg.ARMPerm;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.cmd.arg.ARRel;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.massivecore.cmd.arg.ARBoolean;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsPerm extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPerm()
{
// Aliases
this.addAliases("perm");
// Args
this.addOptionalArg("faction", "you");
this.addOptionalArg("perm", "all");
this.addOptionalArg("relation", "read");
this.addOptionalArg("yes/no", "read");
this.setErrorOnToManyArgs(false);
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.PERM.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Arg: Faction
Faction faction = this.arg(0, ARFaction.get(), msenderFaction);
if (faction == null) return;
// Case: Show All
if ( ! this.argIsSet(1))
{
msg(Txt.titleize("Perms for " + faction.describeTo(msender, true)));
msg(MPerm.getStateHeaders());
for (MPerm perm : MPerm.getAll())
{
msg(perm.getStateInfo(faction.getPermitted(perm), true));
}
return;
}
// Arg: MPerm
MPerm mperm = this.arg(1, ARMPerm.get());
if (mperm == null) return;
// Case: Show One
if ( ! this.argIsSet(2))
{
msg(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
msg(MPerm.getStateHeaders());
msg(mperm.getStateInfo(faction.getPermitted(mperm), true));
return;
}
// Do the sender have the right to change perms for this faction?
if ( ! MPerm.getPermPerms().has(msender, faction, true)) return;
// Is this perm editable?
if (!msender.isUsingAdminMode() && !mperm.isEditable())
{
msg("<b>The perm <h>%s <b>is not editable.", mperm.getName());
return;
}
// Arg: Rel
Rel rel = this.arg(2, ARRel.get());
if (rel == null) return;
if ( ! this.argIsSet(3))
{
msg("<b>Should <h>%s <b>have the <h>%s <b>permission or not?\nYou must <h>add \"yes\" or \"no\" <b>at the end.", Txt.getNicedEnum(rel), Txt.upperCaseFirst(mperm.getName()));
return;
}
// Arg: Target Value
Boolean targetValue = this.arg(3, ARBoolean.get(), null);
if (targetValue == null) return;
// Apply
faction.setRelationPermitted(mperm, rel, targetValue);
// The following is to make sure the leader always has the right to change perms if that is our goal.
if (mperm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains(Rel.LEADER))
{
faction.setRelationPermitted(MPerm.getPermPerms(), Rel.LEADER, true);
}
// Inform
msg(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
msg(MPerm.getStateHeaders());
msg(mperm.getStateInfo(faction.getPermitted(mperm), true));
}
}
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
public class CmdFactionsPerm extends FactionsCommand
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
CmdFactionsPermList cmdFactionsPermList = new CmdFactionsPermList();
CmdFactionsPermShow cmdFactionsPermShow = new CmdFactionsPermShow();
CmdFactionsPermSet cmdFactionsPermSet = new CmdFactionsPermSet();
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPerm()
{
// Aliases
this.addAliases("perm");
// Subcommands
this.addSubCommand(this.cmdFactionsPermList);
this.addSubCommand(this.cmdFactionsPermShow);
this.addSubCommand(this.cmdFactionsPermSet);
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.PERM.node));
}
}

View File

@@ -0,0 +1,54 @@
package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import java.util.List;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.massivecore.cmd.arg.ARInteger;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsPermList extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPermList()
{
// Aliases
this.addAliases("l", "list");
// Args
this.addOptionalArg("page", "1");
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.PERM_LIST.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Args
Integer pageHumanBased = this.arg(0, ARInteger.get(), 1);
if (pageHumanBased == null) return;
// Create messages
List<String> messages = new ArrayList<String>();
for (MPerm perm : MPerm.getAll())
{
if ( ! perm.isVisible() && ! msender.isUsingAdminMode()) continue;
messages.add(perm.getDesc(true, true));
}
// Send messages
sendMessage(Txt.getPage(messages, pageHumanBased, "Available Faction Perms", sender));
}
}

View File

@@ -0,0 +1,102 @@
package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import java.util.List;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.cmd.arg.ARMPerm;
import com.massivecraft.factions.cmd.arg.ARRel;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.cmd.arg.ARBoolean;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsPermSet extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPermSet()
{
// Aliases
this.addAliases("set");
// Args
this.addRequiredArg("perm");
this.addRequiredArg("relation");
this.addRequiredArg("yes/no");
this.addOptionalArg("faction", "you");
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.PERM_SET.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Args
MPerm perm = this.arg(0, ARMPerm.get());
if (perm == null) return;
Rel rel = this.arg(1, ARRel.get());
if (rel == null) return;
Boolean value = this.arg(2, ARBoolean.get());
if (value == null) return;
Faction faction = this.arg(3, ARFaction.get(), msenderFaction);
if (faction == null) return;
// Do the sender have the right to change perms for this faction?
if ( ! MPerm.getPermPerms().has(msender, faction, true)) return;
// Is this perm editable?
if ( ! msender.isUsingAdminMode() && ! perm.isEditable())
{
msg("<b>The perm <h>%s <b>is not editable.", perm.getName());
return;
}
// No change
if (faction.getPermitted(perm).contains(rel) == value)
{
msg("%s <i>already has %s <i>set to %s <i>for %s<i>.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "<g>YES" : "<b>NOO"), rel.getColor() + rel.getDescPlayerMany());
return;
}
// Apply
faction.setRelationPermitted(perm, rel, value);
// The following is to make sure the leader always has the right to change perms if that is our goal.
if (perm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains(Rel.LEADER))
{
faction.setRelationPermitted(MPerm.getPermPerms(), Rel.LEADER, true);
}
// Create messages
List<String> messages = new ArrayList<String>();
// Inform sender
messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
messages.add(MPerm.getStateHeaders());
messages.add(Txt.parse(perm.getStateInfo(faction.getPermitted(perm), true)));
sendMessage(messages);
// Inform faction (their message is slighly different)
List<MPlayer> recipients = faction.getMPlayers();
recipients.remove(msender);
for (MPlayer recipient : recipients)
{
messages.add(0, Txt.parse("<h>%s <i>set a perm for <h>%s<i>.", msender.describeTo(recipient, true), faction.describeTo(recipient, true)));
recipient.sendMessage(messages);
}
}
}

View File

@@ -0,0 +1,80 @@
package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import java.util.List;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.cmd.arg.ARMPerm;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.massivecore.cmd.arg.ARList;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsPermShow extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPermShow()
{
// Aliases
this.addAliases("s", "show");
// Args
this.addOptionalArg("faction", "you");
this.addOptionalArg("perms", "all");
this.setErrorOnToManyArgs(false);
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.PERM_SHOW.node));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Arg: Faction
Faction faction = this.arg(0, ARFaction.get(), msenderFaction);
if (faction == null) return;
List<MPerm> perms = new ArrayList<MPerm>();
// Case: Show All
if ( ! this.argIsSet(1) || "all".equalsIgnoreCase(this.arg(1)))
{
for (MPerm mperm : MPerm.getAll())
{
if ( ! mperm.isVisible() && ! msender.isUsingAdminMode()) continue;
perms.add(mperm);
}
}
// Case: Show Some
else
{
// Arg perm. Maybe we should use ARSet but that is currently buggy.
List<MPerm> mperms = this.arg(this.argConcatFrom(1), ARList.get(ARMPerm.get()));
if (mperms == null) return;
perms.addAll(mperms);
}
// Create messages
List<String> messages = new ArrayList<String>();
messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
messages.add(MPerm.getStateHeaders());
for (MPerm mperm : perms)
{
messages.add(Txt.parse(mperm.getStateInfo(faction.getPermitted(mperm), true)));
}
// Send messages
sendMessage(messages);
}
}