mirror of
https://github.com/aikar/commands.git
synced 2026-05-31 06:11:55 +00:00
add min/max validation flags.
This should of really been conditions, but lazy to set that up proper for now.
This commit is contained in:
@@ -124,14 +124,18 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
|
||||
});
|
||||
registerContext(BigDecimal.class, (c) -> {
|
||||
try {
|
||||
return ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes"));
|
||||
BigDecimal number = ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes"));
|
||||
validateMinMax(c, number, null);
|
||||
return number;
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
|
||||
}
|
||||
});
|
||||
registerContext(BigInteger.class, (c) -> {
|
||||
try {
|
||||
return ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes")).toBigIntegerExact();
|
||||
BigDecimal number = ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes"));
|
||||
validateMinMax(c, number, null);
|
||||
return number.toBigIntegerExact();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
|
||||
}
|
||||
@@ -240,10 +244,19 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
|
||||
@NotNull
|
||||
private Number parseAndValidateNumber(R c, Number maxValue) throws InvalidCommandArgument {
|
||||
Number val = ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes"));
|
||||
validateMinMax(c, val, maxValue);
|
||||
return val;
|
||||
}
|
||||
|
||||
private void validateMinMax(R c, Number val, Number maxValue) throws InvalidCommandArgument {
|
||||
Number minValue = c.getFlagValue("min", (Integer) null);
|
||||
maxValue = c.getFlagValue("max", maxValue != null ? maxValue.intValue() : null);
|
||||
if (maxValue != null && val.doubleValue() > maxValue.doubleValue()) {
|
||||
throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_AT_MOST, "{max}", String.valueOf(maxValue));
|
||||
}
|
||||
return val;
|
||||
if (minValue != null && val.doubleValue() < minValue.doubleValue()) {
|
||||
throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_AT_LEAST, "{min}", String.valueOf(minValue));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ public enum MessageKeys implements MessageKeyProvider {
|
||||
MUST_BE_A_NUMBER,
|
||||
MUST_BE_MIN_LENGTH,
|
||||
MUST_BE_MAX_LENGTH,
|
||||
PLEASE_SPECIFY_AT_LEAST,
|
||||
PLEASE_SPECIFY_AT_MOST,
|
||||
NOT_ALLOWED_ON_CONSOLE,
|
||||
COULD_NOT_FIND_PLAYER,
|
||||
|
||||
@@ -32,6 +32,7 @@ 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 at most {max} characters long.
|
||||
acf-core.please_specify_at_most = Error: Please specify a value at most {max}.
|
||||
acf-core.please_specify_at_least = Error: Please specify a value at least {min}.
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user