mirror of
https://github.com/aikar/commands.git
synced 2026-06-30 10:18:29 +00:00
Support for Primitive parameter types
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user