Added support for value flags to the command system

This commit is contained in:
TomyLobo 2011-08-29 14:44:14 +02:00
parent b8a310dfdc
commit d64fd95173
2 changed files with 29 additions and 19 deletions

View File

@ -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 "";
}

View File

@ -374,33 +374,34 @@ public abstract class CommandsManager<T> {
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<Character> isValueFlag = new HashSet<Character>();
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);
}
}