Validate that input does not exceed max values for numbers

This commit is contained in:
Aikar
2018-01-05 20:38:33 -05:00
parent 45dcc43d14
commit bfc8534176
3 changed files with 23 additions and 11 deletions
@@ -31,6 +31,7 @@ import co.aikar.commands.contexts.IssuerAwareContextResolver;
import co.aikar.commands.contexts.IssuerOnlyContextResolver;
import co.aikar.commands.contexts.OptionalContextResolver;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
@@ -44,77 +45,77 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
this.manager = manager;
registerContext(Short.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).shortValue();
return parseAndValudateNumber(c, Short.MAX_VALUE).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();
return parseAndValudateNumber(c, Short.MAX_VALUE).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();
return parseAndValudateNumber(c, Integer.MAX_VALUE).intValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(int.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).intValue();
return parseAndValudateNumber(c, Integer.MAX_VALUE).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();
return parseAndValudateNumber(c, Long.MAX_VALUE).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();
return parseAndValudateNumber(c, Long.MAX_VALUE).longValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(Float.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).floatValue();
return parseAndValudateNumber(c, Float.MAX_VALUE).floatValue();
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
});
registerContext(float.class, (c) -> {
try {
return ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")).floatValue();
return parseAndValudateNumber(c, Float.MAX_VALUE).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();
return parseAndValudateNumber(c, Double.MAX_VALUE).doubleValue();
} 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();
return parseAndValudateNumber(c, Double.MAX_VALUE).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"));
return parseAndValudateNumber(c, Double.MAX_VALUE);
} catch (NumberFormatException e) {
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER);
}
@@ -220,6 +221,15 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
});
}
@NotNull
private Number parseAndValudateNumber(R c, Number maxValue) throws InvalidCommandArgument {
Number val = ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes"));
if (maxValue != null && val.doubleValue() > maxValue.doubleValue()) {
throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_AT_MOST, "{max}", String.valueOf(maxValue));
}
return val;
}
/**
* @deprecated Please switch to {@link #registerIssuerAwareContext(Class, IssuerAwareContextResolver)}
* as the core wants to use the platform agnostic term of "Issuer" instead of Sender
@@ -42,6 +42,7 @@ public enum MessageKeys implements MessageKeyProvider {
MUST_BE_A_NUMBER,
MUST_BE_MIN_LENGTH,
MUST_BE_MAX_LENGTH,
PLEASE_SPECIFY_AT_MOST,
NOT_ALLOWED_ON_CONSOLE,
COULD_NOT_FIND_PLAYER,
HELP_FORMAT,
+1
View File
@@ -31,6 +31,7 @@ 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 at most {max} characters long.
acf-core.please_specify_at_most = Error: Please specify a value at most {max}.
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>