Support for Primitive parameter types

This commit is contained in:
Aikar
2018-01-05 20:07:46 -05:00
parent f591117733
commit 45dcc43d14
3 changed files with 55 additions and 8 deletions
@@ -42,6 +42,20 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
CommandContexts(CommandManager manager) {
this.manager = manager;
registerContext(Short.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).shortValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(short.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).shortValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(Integer.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).intValue();
@@ -49,13 +63,26 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(int.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).intValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(Long.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).longValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(long.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).longValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(Float.class, (c) -> {
try {
@@ -64,6 +91,13 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(float.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).floatValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(Double.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).doubleValue();
@@ -71,6 +105,13 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(double.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).doubleValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(Number.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes"));
@@ -78,12 +119,14 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(Boolean.class, (c) -> {
String test = c.popFirstArg();
if (test == null) {
return null;
registerContext(Boolean.class, (c) -> ACFUtil.isTruthy(c.popFirstArg()));
registerContext(boolean.class, (c) -> ACFUtil.isTruthy(c.popFirstArg()));
registerContext(char.class, c -> {
String s = c.popFirstArg();
if (s.length() > 1) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_MAX_LENGTH, "{max}", String.valueOf(1));
}
return ACFUtil.isTruthy(test);
return s.charAt(0);
});
registerContext(String.class, (c) -> {
final Values values = c.getParam().getAnnotation(Values.class);
@@ -216,7 +216,11 @@ public class RegisteredCommand <CEC extends CommandExecutionContext<? extends Co
if (allowOptional && def != null) {
args.add(scope.manager.getCommandReplacements().replace(def.value()));
} else if (allowOptional && opt != null) {
passedArgs.put(parameterName, isOptionalResolver(resolver) ? resolver.getContext(context) : null);
Object value = isOptionalResolver(resolver) ? resolver.getContext(context) : null;
if (value == null && parameter.getClass().isPrimitive()) {
throw new IllegalStateException("Parameter " + parameter.getName() + " is primitive and does not support Optional.");
}
passedArgs.put(parameterName, value);
//noinspection UnnecessaryContinue
continue;
} else if (!isOptionalResolver) {
+1 -1
View File
@@ -30,7 +30,7 @@ acf-core.info_message = {message}
acf-core.please_specify_one_of = Error: Please specify one of (<c2>{valid}</c2>).
acf-core.must_be_a_number = Error: Must be a number.
acf-core.must_be_min_length = Error: Must be at least {min} characters long.
acf-core.must_be_max_length = Error: Must be less than {max} characters long.
acf-core.must_be_max_length = Error: Must be at most {max} characters long.
acf-core.not_allowed_on_console = Error: Console may not execute this command.
acf-core.could_not_find_player = Error: Could not find a player by the name: <c2>{search}</c2>
acf-core.help_format = <c1>{command}</c1> <c2>{parameters}</c2> <c3>{separator} {description}</c3>