Say hi to the requirements.

This commit is contained in:
Olof Larsson 2011-12-12 19:22:47 +01:00
parent d823098f44
commit 9c2838efc5
6 changed files with 114 additions and 31 deletions

View File

@ -1,6 +1,6 @@
name: mcore1
version: 1
main: com.massivecraft.core1.MCore
main: com.massivecraft.mcore1.MCore
authors: [Olof Larsson]
softdepend: [Vault, Spout]
commands:

View File

@ -9,6 +9,7 @@ import org.bukkit.entity.Player;
import com.massivecraft.mcore1.Lang;
import com.massivecraft.mcore1.MCore;
import com.massivecraft.mcore1.cmd.arg.IArgHandler;
import com.massivecraft.mcore1.cmd.req.IReq;
import com.massivecraft.mcore1.persist.IClassManager;
import com.massivecraft.mcore1.persist.Persist;
import com.massivecraft.mcore1.plugin.MPlugin;
@ -55,10 +56,17 @@ public abstract class MCommand
// FIELD: errorOnToManyArgs
// Should an error be thrown if "to many" args are sent.
protected boolean errorOnToManyArgs = true;
protected boolean errorOnToManyArgs;
public boolean getErrorOnToManyArgs() { return this.errorOnToManyArgs; }
public void setErrorOnToManyArgs(boolean val) { this.errorOnToManyArgs = val; }
// FIELD: requirements
// All these requirements must be met for the command to be executable;
protected List<IReq> requirements;
public List<IReq> getRequirements() { return this.requirements; }
public void setRequirements(List<IReq> val) { this.requirements = val; }
public void addRequirements(IReq... requirements) { this.requirements.addAll(Arrays.asList(requirements)); }
// FIELD: desc
// This field may be left blank and will in such case be loaded from the permissions node instead.
// Thus make sure the permissions node description is an action description like "eat hamburgers" or "do admin stuff".
@ -68,7 +76,7 @@ public abstract class MCommand
{
if (this.desc == null)
{
String pdesc = getPlugin().perm.getPermissionDescription(this.permission);
String pdesc = getPlugin().perm.getPermissionDescription(this.descPermission);
if (pdesc != null)
{
return pdesc;
@ -78,6 +86,17 @@ public abstract class MCommand
return this.desc;
}
// FIELD: descPermission
// This permission node IS NOT TESTED AT ALL. It is rather used in the method above.
protected String descPermission;
public String getDescPermission() { return this.descPermission; }
public void setDescPermission(String val) { this.descPermission = val; }
// FIELD: visibilityMode
protected VisibilityMode visibilityMode;
public VisibilityMode getVisibilityMode() { return this.visibilityMode; }
public void setVisibilityMode(VisibilityMode val) { this.visibilityMode = val; }
// -------------------------------------------- //
// EXECUTION INFO
// -------------------------------------------- //
@ -123,18 +142,9 @@ public abstract class MCommand
return null;
}
// -------------------------------------------- //
// TODO: PURE DERP
// -------------------------------------------- //
// Some information on permissions
// this is part of validating if the sender is ok...
public boolean senderMustBePlayer;
public String permission;
public MCommand()
{
this.permission = null;
this.descPermission = null;
this.subCommands = new ArrayList<MCommand>();
this.aliases = new ArrayList<String>();
@ -142,7 +152,13 @@ public abstract class MCommand
this.requiredArgs = new ArrayList<String>();
this.optionalArgs = new LinkedHashMap<String, String>();
this.requirements = new ArrayList<IReq>();
this.errorOnToManyArgs = true;
this.desc = null;
this.visibilityMode = VisibilityMode.VISIBLE;
}
// The commandChain is a list of the parent command chain used to get to this command.
@ -193,17 +209,12 @@ public abstract class MCommand
*/
public boolean validCall(CommandSender sender, List<String> args)
{
if ( ! validSenderType(sender, true))
if ( ! this.requirementsAreMet(sender, true))
{
return false;
}
if ( ! validSenderPermissions(sender, true))
{
return false;
}
if ( ! validArgs(args, sender))
if ( ! this.validArgs(args, sender))
{
return false;
}
@ -216,23 +227,20 @@ public abstract class MCommand
return true;
}
public boolean validSenderType(CommandSender sender, boolean informSenderIfNot)
public boolean requirementsAreMet(CommandSender sender, boolean informSenderIfNot)
{
if (this.senderMustBePlayer && ! (sender instanceof Player))
for (IReq req : this.getRequirements())
{
if ( ! req.test(sender, this))
{
if (informSenderIfNot)
{
msg(Lang.commandSenderMustBePlayer);
this.msg(req.createErrorMessage(sender, this));
}
return false;
}
return true;
}
public boolean validSenderPermissions(CommandSender sender, boolean informSenderIfNot)
{
if (this.permission == null) return true;
return getPlugin().perm.has(sender, this.permission, informSenderIfNot);
return true;
}
public boolean validArgs(List<String> args, CommandSender sender)

View File

@ -0,0 +1,9 @@
package com.massivecraft.mcore1.cmd;
public enum VisibilityMode
{
VISIBLE, // Visible commands are visible to anyone. Even those who don't have permission to use it or is of invalid sender type.
SECRET, // Secret commands are visible only to those who can use the command. These commands are usually some kind of admin commands.
INVISIBLE, // Invisible commands are invisible to everyone, even those who can use the command.
;
}

View File

@ -0,0 +1,14 @@
package com.massivecraft.mcore1.cmd.req;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.cmd.MCommand;
public interface IReq
{
// This just tests wether the requirement is met or not.
public boolean test(CommandSender sender, MCommand command);
// This just composes the error message and does NOT test the requirement at all.
public String createErrorMessage(CommandSender sender, MCommand command);
}

View File

@ -0,0 +1,30 @@
package com.massivecraft.mcore1.cmd.req;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.cmd.MCommand;
public class ReqHasPerm implements IReq
{
private String perm;
public String getPerm() { return this.perm; }
public void setPerm(String val) { this.perm = val; }
public ReqHasPerm(String perm)
{
this.perm = perm;
}
@Override
public boolean test(CommandSender sender, MCommand command)
{
return sender.hasPermission(this.perm);
}
@Override
public String createErrorMessage(CommandSender sender, MCommand command)
{
return command.getPlugin().perm.getForbiddenMessage(this.perm);
}
}

View File

@ -0,0 +1,22 @@
package com.massivecraft.mcore1.cmd.req;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.massivecraft.mcore1.Lang;
import com.massivecraft.mcore1.cmd.MCommand;
public class ReqIsPlayer implements IReq
{
@Override
public boolean test(CommandSender sender, MCommand command)
{
return sender instanceof Player;
}
@Override
public String createErrorMessage(CommandSender sender, MCommand command)
{
return Lang.commandSenderMustBePlayer;
}
}