From 480fcdc8b968a17402c41d71890ef62c432bbdd9 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 6 Oct 2013 14:28:29 +0200 Subject: [PATCH] Add a permission arg reader and the option to turn of the cmd arg tokenizer. --- .../mcore/cmd/BukkitGlueCommand.java | 12 +++++- src/com/massivecraft/mcore/cmd/MCommand.java | 7 ++++ .../mcore/cmd/arg/ARPermission.java | 40 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/com/massivecraft/mcore/cmd/arg/ARPermission.java diff --git a/src/com/massivecraft/mcore/cmd/BukkitGlueCommand.java b/src/com/massivecraft/mcore/cmd/BukkitGlueCommand.java index ce75d4c6..5baec6da 100644 --- a/src/com/massivecraft/mcore/cmd/BukkitGlueCommand.java +++ b/src/com/massivecraft/mcore/cmd/BukkitGlueCommand.java @@ -1,6 +1,7 @@ package com.massivecraft.mcore.cmd; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -28,7 +29,16 @@ public class BukkitGlueCommand extends Command public boolean execute(CommandSender sender, String commandLabel, String[] args) { if ( ! plugin.isEnabled()) return false; - this.mcommand.execute(sender, Txt.tokenizeArguments(Txt.implode(args, " "))); + List argList; + if (this.mcommand.isUsingTokenizer()) + { + argList = Txt.tokenizeArguments(Txt.implode(args, " ")); + } + else + { + argList = new ArrayList(Arrays.asList(args)); + } + this.mcommand.execute(sender, argList); return true; } diff --git a/src/com/massivecraft/mcore/cmd/MCommand.java b/src/com/massivecraft/mcore/cmd/MCommand.java index 6a26633e..1202a72e 100644 --- a/src/com/massivecraft/mcore/cmd/MCommand.java +++ b/src/com/massivecraft/mcore/cmd/MCommand.java @@ -70,6 +70,12 @@ public abstract class MCommand public boolean getErrorOnToManyArgs() { return this.errorOnToManyArgs; } public void setErrorOnToManyArgs(boolean val) { this.errorOnToManyArgs = val; } + // FIELD: usingTokenizer + // Should the arguments be parsed considering quotes and backslashes and such? + protected boolean usingTokenizer; + public boolean isUsingTokenizer() { return this.usingTokenizer; } + public void setUsingTokenizer(boolean usingTokenizer) { this.usingTokenizer = usingTokenizer; } + // FIELD: requirements // All these requirements must be met for the command to be executable; protected List requirements; @@ -214,6 +220,7 @@ public abstract class MCommand this.requirements = new ArrayList(); this.errorOnToManyArgs = true; + this.usingTokenizer = true; this.desc = null; diff --git a/src/com/massivecraft/mcore/cmd/arg/ARPermission.java b/src/com/massivecraft/mcore/cmd/arg/ARPermission.java new file mode 100644 index 00000000..83c85392 --- /dev/null +++ b/src/com/massivecraft/mcore/cmd/arg/ARPermission.java @@ -0,0 +1,40 @@ +package com.massivecraft.mcore.cmd.arg; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.permissions.Permission; + +public class ARPermission extends ArgReaderAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static ARPermission i = new ARPermission(); + public static ARPermission get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public ArgResult read(String arg, CommandSender sender) + { + ArgResult ret = new ArgResult(); + + for (Permission permission : Bukkit.getPluginManager().getPermissions()) + { + if (!permission.getName().equals(arg)) continue; + ret.setResult(permission); + break; + } + + if (!ret.hasResult()) + { + ret.setErrors("No permission with the name \""+arg+"\" was found."); + } + + return ret; + } + +}