From d64fd9517351952d8d6dfe595b20fa7ba485faa6 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Mon, 29 Aug 2011 14:44:14 +0200 Subject: [PATCH] Added support for value flags to the command system --- .../minecraft/util/commands/Command.java | 9 +++++ .../util/commands/CommandsManager.java | 39 ++++++++++--------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/sk89q/minecraft/util/commands/Command.java b/src/main/java/com/sk89q/minecraft/util/commands/Command.java index 381446135..0e164fcc1 100644 --- a/src/main/java/com/sk89q/minecraft/util/commands/Command.java +++ b/src/main/java/com/sk89q/minecraft/util/commands/Command.java @@ -65,4 +65,13 @@ public @interface Command { * each character being a flag. Use A-Z and a-z as possible flags. */ String flags() default ""; + + /** + * Value flags are special flags, that take a value after the flag. + * The semantics are the same as with the flags parameter. + * They aren't automatically documented and thus need to be added + * to the "usage" parameter separately. + */ + String valueFlags() default ""; + } diff --git a/src/main/java/com/sk89q/minecraft/util/commands/CommandsManager.java b/src/main/java/com/sk89q/minecraft/util/commands/CommandsManager.java index 79b7b6b51..b4153d47f 100644 --- a/src/main/java/com/sk89q/minecraft/util/commands/CommandsManager.java +++ b/src/main/java/com/sk89q/minecraft/util/commands/CommandsManager.java @@ -374,33 +374,34 @@ public abstract class CommandsManager { executeMethod(parent, aCmd.aliasTo(), player, methodArgs, level); } else { Command cmd = method.getAnnotation(Command.class); - + String[] newArgs = new String[args.length - level]; System.arraycopy(args, level, newArgs, 0, args.length - level); - - CommandContext context = new CommandContext(newArgs); - - if (context.argsLength() < cmd.min()) { - throw new CommandUsageException("Too few arguments.", - getUsage(args, level, cmd)); + + final String valueFlags = cmd.valueFlags(); + final Set isValueFlag = new HashSet(); + + for (int i = 0; i < valueFlags.length(); ++i) { + isValueFlag.add(valueFlags.charAt(i)); } - - if (cmd.max() != -1 && context.argsLength() > cmd.max()) { - throw new CommandUsageException("Too many arguments.", - getUsage(args, level, cmd)); - } - + + CommandContext context = new CommandContext(newArgs, isValueFlag); + + if (context.argsLength() < cmd.min()) + throw new CommandUsageException("Too few arguments.", getUsage(args, level, cmd)); + + if (cmd.max() != -1 && context.argsLength() > cmd.max()) + throw new CommandUsageException("Too many arguments.", getUsage(args, level, cmd)); + for (char flag : context.getFlags()) { - if (cmd.flags().indexOf(String.valueOf(flag)) == -1) { - throw new CommandUsageException("Unknown flag: " + flag, - getUsage(args, level, cmd)); - } + if (cmd.flags().indexOf(String.valueOf(flag)) == -1) + throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, level, cmd)); } - + methodArgs[0] = context; Object instance = instances.get(method); - + invokeMethod(parent, args, player, method, instance, methodArgs, argsCount); } }