Added flags and fixed cmd->java parameter ordering
This commit is contained in:
@@ -10,6 +10,7 @@ import com.nemez.cmdmgr.CommandManager;
|
||||
public class CmdMgrTest extends JavaPlugin {
|
||||
|
||||
public void onEnable() {
|
||||
CommandManager.debugHelpMenu = true;
|
||||
CommandManager.registerCommand(new File("plugins/test.cmd"), this, this);
|
||||
}
|
||||
|
||||
@@ -32,9 +33,10 @@ public class CmdMgrTest extends JavaPlugin {
|
||||
}
|
||||
|
||||
@Command(hook="home_del")
|
||||
public void executeDelHome(CommandSender sender, String name) {
|
||||
public void executeDelHome(CommandSender sender, String name, boolean aFlag) {
|
||||
sender.sendMessage("You executed:");
|
||||
sender.sendMessage("/home del " + name);
|
||||
sender.sendMessage("-a - " + aFlag);
|
||||
}
|
||||
|
||||
@Command(hook="home_list")
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.nemez.cmdmgr.component.FloatComponent;
|
||||
import com.nemez.cmdmgr.component.ICommandComponent;
|
||||
import com.nemez.cmdmgr.component.IntegerComponent;
|
||||
import com.nemez.cmdmgr.component.LongComponent;
|
||||
import com.nemez.cmdmgr.component.OptionalComponent;
|
||||
import com.nemez.cmdmgr.component.ShortComponent;
|
||||
import com.nemez.cmdmgr.component.StringComponent;
|
||||
import com.nemez.cmdmgr.util.BranchStack;
|
||||
@@ -378,6 +379,10 @@ public class CommandManager {
|
||||
case "bool":
|
||||
case "boolean":
|
||||
return new BooleanComponent();
|
||||
case "optional":
|
||||
case "opt":
|
||||
case "flag":
|
||||
return new OptionalComponent();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.nemez.cmdmgr.component;
|
||||
public abstract class ArgumentComponent implements ICommandComponent {
|
||||
|
||||
public String argName;
|
||||
public int position;
|
||||
|
||||
@Override
|
||||
public String argName() {
|
||||
|
||||
19
com/nemez/cmdmgr/component/OptionalComponent.java
Normal file
19
com/nemez/cmdmgr/component/OptionalComponent.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.nemez.cmdmgr.component;
|
||||
|
||||
public class OptionalComponent extends ArgumentComponent {
|
||||
|
||||
@Override
|
||||
public Object get(String input) {
|
||||
return input.equals(argName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean valid(String input) {
|
||||
return input.equals(argName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getComponentInfo() {
|
||||
return "<" + argName + ":flag>";
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import com.nemez.cmdmgr.component.FloatComponent;
|
||||
import com.nemez.cmdmgr.component.ICommandComponent;
|
||||
import com.nemez.cmdmgr.component.IntegerComponent;
|
||||
import com.nemez.cmdmgr.component.LongComponent;
|
||||
import com.nemez.cmdmgr.component.OptionalComponent;
|
||||
import com.nemez.cmdmgr.component.ShortComponent;
|
||||
import com.nemez.cmdmgr.component.StringComponent;
|
||||
|
||||
@@ -44,19 +45,34 @@ public class Executable extends org.bukkit.command.Command {
|
||||
for (HelpPageCommand cmd : page) {
|
||||
if (cmd != null) {
|
||||
processLine(cmd.usage.split("\\ "), cmd.permission, cmd.method, methods, methodContainer, plugin, cmd.type);
|
||||
if (CommandManager.debugHelpMenu) {
|
||||
continue;
|
||||
}
|
||||
String newUsage = "";
|
||||
String buffer = "";
|
||||
String typeBuffer = "";
|
||||
boolean ignore = false;
|
||||
boolean toBuffer = false;
|
||||
for (char c : cmd.usage.toCharArray()) {
|
||||
if (c == ':' || c == '>') {
|
||||
ignore = !ignore;
|
||||
if (c == '>') {
|
||||
if (c == '<') {
|
||||
toBuffer = true;
|
||||
}else if (c == ':') {
|
||||
toBuffer = false;
|
||||
ignore = true;
|
||||
}else if (c == '>') {
|
||||
ignore = false;
|
||||
if (typeBuffer.equals("flag")) {
|
||||
newUsage += '[' + buffer + (CommandManager.debugHelpMenu ? ':' + typeBuffer : "") + ']';
|
||||
}else{
|
||||
newUsage += '<' + buffer + (CommandManager.debugHelpMenu ? ':' + typeBuffer : "") + '>';
|
||||
}
|
||||
buffer = "";
|
||||
typeBuffer = "";
|
||||
}else{
|
||||
if (toBuffer) {
|
||||
buffer += c;
|
||||
}else if (ignore) {
|
||||
typeBuffer += c;
|
||||
}else{
|
||||
newUsage += c;
|
||||
}
|
||||
}else if (!ignore) {
|
||||
newUsage += c;
|
||||
}
|
||||
}
|
||||
cmd.usage = newUsage;
|
||||
@@ -70,13 +86,8 @@ public class Executable extends org.bukkit.command.Command {
|
||||
cmdMap.setAccessible(true);
|
||||
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
|
||||
map.register(name, this);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to register command '" + name + "'!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -93,7 +104,7 @@ public class Executable extends org.bukkit.command.Command {
|
||||
IntegerComponent pageID = new IntegerComponent();
|
||||
pageID.argName = "page";
|
||||
command.add(pageID);
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, permission, null, methodContainer, Type.BOTH);
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, null, permission, null, methodContainer, Type.BOTH);
|
||||
commands.add(def);
|
||||
return;
|
||||
}
|
||||
@@ -101,6 +112,7 @@ public class Executable extends org.bukkit.command.Command {
|
||||
method = method.trim() + " ";
|
||||
String[] methodArray = method.split(" ");
|
||||
Method target = null;
|
||||
ArrayList<Integer> links = new ArrayList<Integer>();
|
||||
|
||||
for (String s : line) {
|
||||
if (s.contains("/")) {
|
||||
@@ -157,6 +169,13 @@ public class Executable extends org.bukkit.command.Command {
|
||||
comp8.argName = type[0].substring(1);
|
||||
paramName = comp8.argName;
|
||||
command.add(comp8);
|
||||
break;
|
||||
case "flag":
|
||||
OptionalComponent comp9 = new OptionalComponent();
|
||||
comp9.argName = type[0].substring(1);
|
||||
paramName = comp9.argName;
|
||||
command.add(comp9);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
@@ -165,6 +184,8 @@ public class Executable extends org.bukkit.command.Command {
|
||||
if (methodArray[i] != null && !methodArray[i].trim().equals("")) {
|
||||
if (methodArray[i].trim().equals(paramName)) {
|
||||
methodParams.put(index, command.get(command.size() - 1));
|
||||
links.add(index);
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
@@ -179,13 +200,13 @@ public class Executable extends org.bukkit.command.Command {
|
||||
if (annotations == null || annotations.length != 1) {
|
||||
plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + ")");
|
||||
CommandManager.errors = true;
|
||||
System.err.println("Method not found! (" + methodArray[0] + ")");
|
||||
return;
|
||||
}else{
|
||||
if (annotations[0].hook().equals(methodArray[0])) {
|
||||
Class<?>[] params = m.getParameterTypes();
|
||||
if (params.length -1 != methodParams.size()) {
|
||||
plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + "): Arguments don't match");
|
||||
CommandManager.errors = true;
|
||||
return;
|
||||
}else{
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
@@ -213,6 +234,8 @@ public class Executable extends org.bukkit.command.Command {
|
||||
|
||||
}else if (comp instanceof BooleanComponent && params[i] == boolean.class) {
|
||||
|
||||
}else if (comp instanceof OptionalComponent && params[i] == boolean.class) {
|
||||
|
||||
}else{
|
||||
plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + "): Invalid method arguments");
|
||||
CommandManager.errors = true;
|
||||
@@ -234,7 +257,7 @@ public class Executable extends org.bukkit.command.Command {
|
||||
if (etype == null) {
|
||||
etype = Type.BOTH;
|
||||
}
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, permission, target, methodContainer, etype);
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, links, permission, target, methodContainer, etype);
|
||||
commands.add(def);
|
||||
}
|
||||
|
||||
@@ -242,14 +265,25 @@ public class Executable extends org.bukkit.command.Command {
|
||||
public boolean execute(CommandSender sender, String name, String[] args) {
|
||||
ArrayList<ExecutableDefinition> defs = new ArrayList<ExecutableDefinition>();
|
||||
defs.addAll(commands);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
for (int j = 0; j < defs.size(); j++) {
|
||||
if (!defs.get(j).valid(i, args[i])) {
|
||||
defLoop: for (int j = 0; j < defs.size(); j++) {
|
||||
int i = 0;
|
||||
for (int k = 0; i < args.length; i++, k++) {
|
||||
if (!defs.get(j).valid(k, args[i])) {
|
||||
if (!defs.get(j).isOptional(k)) {
|
||||
defs.remove(j);
|
||||
j--;
|
||||
continue defLoop;
|
||||
}else{
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i != args.length) {
|
||||
defs.remove(j);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
if (args.length == 0 || defs.size() == 0) {
|
||||
printPage(sender, 1);
|
||||
}else{
|
||||
@@ -283,17 +317,22 @@ public class Executable extends org.bukkit.command.Command {
|
||||
sender.sendMessage(CommandManager.notAllowedFormatting + "Nobody can run this command.");
|
||||
return true;
|
||||
}
|
||||
if (def.getLength() != args.length) {
|
||||
printPage(sender, 1);
|
||||
return true;
|
||||
}
|
||||
ArrayList<Object> arguments = new ArrayList<Object>();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (def.isArgument(i)) {
|
||||
arguments.add(def.get(i, args[i]));
|
||||
for (int i = 0, j = 0; i < args.length; i++, j++) {
|
||||
if (def.isArgument(j)) {
|
||||
if (def.valid(j, args[i])) {
|
||||
arguments.add(def.get(j, args[i]));
|
||||
}else if (def.isOptional(j)) {
|
||||
arguments.add(false);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
if (!def.invoke(arguments, sender, plugin)) {
|
||||
}
|
||||
Object[] linkedArgs = new Object[arguments.size() + 1];
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
linkedArgs[def.getLink(i) + 1] = arguments.get(i);
|
||||
}
|
||||
if (!def.invoke(linkedArgs, sender, plugin)) {
|
||||
printPage(sender, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@ import java.util.logging.Level;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.nemez.cmdmgr.CommandManager;
|
||||
import com.nemez.cmdmgr.component.ArgumentComponent;
|
||||
import com.nemez.cmdmgr.component.ICommandComponent;
|
||||
import com.nemez.cmdmgr.component.OptionalComponent;
|
||||
|
||||
public class ExecutableDefinition {
|
||||
|
||||
@@ -17,13 +19,15 @@ public class ExecutableDefinition {
|
||||
private Method target;
|
||||
private Object methodContainer;
|
||||
private Type type;
|
||||
private ArrayList<Integer> paramLinks;
|
||||
|
||||
public ExecutableDefinition(ArrayList<ICommandComponent> cmd, String perm, Method method, Object methodContainer, Type type) {
|
||||
public ExecutableDefinition(ArrayList<ICommandComponent> cmd, ArrayList<Integer> paramLinks, String perm, Method method, Object methodContainer, Type type) {
|
||||
this.components = cmd;
|
||||
this.permission = perm;
|
||||
this.target = method;
|
||||
this.methodContainer = methodContainer;
|
||||
this.type = type;
|
||||
this.paramLinks = paramLinks;
|
||||
}
|
||||
|
||||
public boolean valid(int index, String arg) {
|
||||
@@ -47,6 +51,13 @@ public class ExecutableDefinition {
|
||||
return components.get(index) instanceof ArgumentComponent;
|
||||
}
|
||||
|
||||
public boolean isOptional(int index) {
|
||||
if (index < 0 || index >= components.size()) {
|
||||
return false;
|
||||
}
|
||||
return components.get(index) instanceof OptionalComponent;
|
||||
}
|
||||
|
||||
public boolean isHelp() {
|
||||
return target == null && components.get(0).valid("help") && components.get(1).getComponentInfo().equals("<page:i32>");
|
||||
}
|
||||
@@ -63,23 +74,27 @@ public class ExecutableDefinition {
|
||||
return components.size();
|
||||
}
|
||||
|
||||
public boolean invoke(ArrayList<Object> args, CommandSender sender, JavaPlugin plugin) {
|
||||
public int getLink(int i) {
|
||||
if (i < 0 || i > paramLinks.size()) {
|
||||
return i;
|
||||
}
|
||||
return paramLinks.get(i);
|
||||
}
|
||||
|
||||
public boolean invoke(Object[] args, CommandSender sender, JavaPlugin plugin) {
|
||||
if (target == null) {
|
||||
return false;
|
||||
}
|
||||
Object[] arguments = new Object[args.size() + 1];
|
||||
for (int i = 1; i < arguments.length; i++) {
|
||||
arguments[i] = args.get(i - 1);
|
||||
}
|
||||
arguments[0] = sender;
|
||||
args[0] = sender;
|
||||
try {
|
||||
if (target.getReturnType() == void.class) {
|
||||
target.invoke(methodContainer, arguments);
|
||||
target.invoke(methodContainer, args);
|
||||
return true;
|
||||
}else if (target.getReturnType() == boolean.class) {
|
||||
return (boolean) target.invoke(methodContainer, arguments);
|
||||
return (boolean) target.invoke(methodContainer, args);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
sender.sendMessage(CommandManager.helpInvalidPageFormatting + "An internal error occured, please contact the server administrator and/or report a bug.");
|
||||
plugin.getLogger().log(Level.WARNING, "Runtime Error: invalid method");
|
||||
e.printStackTrace();
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user