Added support for value flags to the command system

Dieser Commit ist enthalten in:
TomyLobo 2011-08-29 14:44:14 +02:00
Ursprung b8a310dfdc
Commit d64fd95173
2 geänderte Dateien mit 29 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -65,4 +65,13 @@ public @interface Command {
* each character being a flag. Use A-Z and a-z as possible flags. * each character being a flag. Use A-Z and a-z as possible flags.
*/ */
String flags() default ""; 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 "";
} }

Datei anzeigen

@ -374,33 +374,34 @@ public abstract class CommandsManager<T> {
executeMethod(parent, aCmd.aliasTo(), player, methodArgs, level); executeMethod(parent, aCmd.aliasTo(), player, methodArgs, level);
} else { } else {
Command cmd = method.getAnnotation(Command.class); Command cmd = method.getAnnotation(Command.class);
String[] newArgs = new String[args.length - level]; String[] newArgs = new String[args.length - level];
System.arraycopy(args, level, newArgs, 0, args.length - level); System.arraycopy(args, level, newArgs, 0, args.length - level);
CommandContext context = new CommandContext(newArgs); final String valueFlags = cmd.valueFlags();
final Set<Character> isValueFlag = new HashSet<Character>();
if (context.argsLength() < cmd.min()) {
throw new CommandUsageException("Too few arguments.", for (int i = 0; i < valueFlags.length(); ++i) {
getUsage(args, level, cmd)); isValueFlag.add(valueFlags.charAt(i));
} }
if (cmd.max() != -1 && context.argsLength() > cmd.max()) { CommandContext context = new CommandContext(newArgs, isValueFlag);
throw new CommandUsageException("Too many arguments.",
getUsage(args, level, cmd)); 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()) { for (char flag : context.getFlags()) {
if (cmd.flags().indexOf(String.valueOf(flag)) == -1) { if (cmd.flags().indexOf(String.valueOf(flag)) == -1)
throw new CommandUsageException("Unknown flag: " + flag, throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, level, cmd));
getUsage(args, level, cmd));
}
} }
methodArgs[0] = context; methodArgs[0] = context;
Object instance = instances.get(method); Object instance = instances.get(method);
invokeMethod(parent, args, player, method, instance, methodArgs, argsCount); invokeMethod(parent, args, player, method, instance, methodArgs, argsCount);
} }
} }