mirror of
https://github.com/aikar/commands.git
synced 2026-05-31 06:11:55 +00:00
Fix max value check for Long and add more type flag helpers
This commit is contained in:
@@ -47,77 +47,77 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
|
||||
this.manager = manager;
|
||||
registerContext(Short.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Short.MAX_VALUE).shortValue();
|
||||
return parseAndValidateNumber(c, Short.MIN_VALUE, Short.MAX_VALUE).shortValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(short.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Short.MAX_VALUE).shortValue();
|
||||
return parseAndValidateNumber(c, Short.MIN_VALUE, Short.MAX_VALUE).shortValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(Integer.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Integer.MAX_VALUE).intValue();
|
||||
return parseAndValidateNumber(c, Integer.MIN_VALUE, Integer.MAX_VALUE).intValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(int.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Integer.MAX_VALUE).intValue();
|
||||
return parseAndValidateNumber(c, Integer.MIN_VALUE, Integer.MAX_VALUE).intValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(Long.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Long.MAX_VALUE).longValue();
|
||||
return parseAndValidateNumber(c, Long.MIN_VALUE, Long.MAX_VALUE).longValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(long.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Long.MAX_VALUE).longValue();
|
||||
return parseAndValidateNumber(c, Long.MIN_VALUE, Long.MAX_VALUE).longValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(Float.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Float.MAX_VALUE).floatValue();
|
||||
return parseAndValidateNumber(c, Float.MIN_VALUE, Float.MAX_VALUE).floatValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(float.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Float.MAX_VALUE).floatValue();
|
||||
return parseAndValidateNumber(c, Float.MIN_VALUE, Float.MAX_VALUE).floatValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(Double.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Double.MAX_VALUE).doubleValue();
|
||||
return parseAndValidateNumber(c, Double.MIN_VALUE, Double.MAX_VALUE).doubleValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(double.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Double.MAX_VALUE).doubleValue();
|
||||
return parseAndValidateNumber(c, Double.MIN_VALUE, Double.MAX_VALUE).doubleValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
});
|
||||
registerContext(Number.class, (c) -> {
|
||||
try {
|
||||
return parseAndValidateNumber(c, Double.MAX_VALUE);
|
||||
return parseAndValidateNumber(c, Double.MIN_VALUE, Double.MAX_VALUE);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
}
|
||||
@@ -125,7 +125,7 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
|
||||
registerContext(BigDecimal.class, (c) -> {
|
||||
try {
|
||||
BigDecimal number = ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes"));
|
||||
validateMinMax(c, number, null);
|
||||
validateMinMax(c, number);
|
||||
return number;
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
@@ -134,7 +134,7 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
|
||||
registerContext(BigInteger.class, (c) -> {
|
||||
try {
|
||||
BigDecimal number = ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes"));
|
||||
validateMinMax(c, number, null);
|
||||
validateMinMax(c, number);
|
||||
return number.toBigIntegerExact();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidCommandArgument(MessageKeys.MUST_BE_A_NUMBER, "{num}", c.getFirstArg());
|
||||
@@ -242,16 +242,18 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Number parseAndValidateNumber(R c, Number maxValue) throws InvalidCommandArgument {
|
||||
Number val = ACFUtil.parseNumber(c.getFirstArg(), c.hasFlag("suffixes"));
|
||||
validateMinMax(c, val, maxValue);
|
||||
c.popFirstArg(); // pop later so we can have a chance to display a nicer error message
|
||||
private Number parseAndValidateNumber(R c, Number minValue, Number maxValue) throws InvalidCommandArgument {
|
||||
final Number val = ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes"));
|
||||
validateMinMax(c, val, minValue, 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);
|
||||
private void validateMinMax(R c, Number val) throws InvalidCommandArgument {
|
||||
validateMinMax(c, val, null, null);
|
||||
}
|
||||
private void validateMinMax(R c, Number val, Number minValue, Number maxValue) throws InvalidCommandArgument {
|
||||
minValue = c.getFlagValue("min", minValue);
|
||||
maxValue = c.getFlagValue("max", maxValue);
|
||||
if (maxValue != null && val.doubleValue() > maxValue.doubleValue()) {
|
||||
throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_AT_MOST, "{max}", String.valueOf(maxValue));
|
||||
}
|
||||
|
||||
@@ -113,6 +113,7 @@ public class CommandExecutionContext <CEC extends CommandExecutionContext, I ext
|
||||
public boolean isOptional() {
|
||||
return param.isOptional();
|
||||
}
|
||||
|
||||
public boolean hasFlag(String flag) {
|
||||
return flags.containsKey(flag);
|
||||
}
|
||||
@@ -125,6 +126,50 @@ public class CommandExecutionContext <CEC extends CommandExecutionContext, I ext
|
||||
return ACFUtil.parseInt(this.flags.get(flag), def);
|
||||
}
|
||||
|
||||
public Long getFlagValue(String flag, Long def) {
|
||||
return ACFUtil.parseLong(this.flags.get(flag), def);
|
||||
}
|
||||
|
||||
public Float getFlagValue(String flag, Float def) {
|
||||
return ACFUtil.parseFloat(this.flags.get(flag), def);
|
||||
}
|
||||
|
||||
public Double getFlagValue(String flag, Double def) {
|
||||
return ACFUtil.parseDouble(this.flags.get(flag), def);
|
||||
}
|
||||
|
||||
public Integer getIntFlagValue(String flag, Number def) {
|
||||
return ACFUtil.parseInt(this.flags.get(flag), def != null ? def.intValue() : null);
|
||||
}
|
||||
|
||||
public Long getLongFlagValue(String flag, Number def) {
|
||||
return ACFUtil.parseLong(this.flags.get(flag), def != null ? def.longValue() : null);
|
||||
}
|
||||
|
||||
public Float getFloatFlagValue(String flag, Number def) {
|
||||
return ACFUtil.parseFloat(this.flags.get(flag), def != null ? def.floatValue() : null);
|
||||
}
|
||||
|
||||
public Double getDoubleFlagValue(String flag, Number def) {
|
||||
return ACFUtil.parseDouble(this.flags.get(flag), def != null ? def.doubleValue() : null);
|
||||
}
|
||||
|
||||
public Boolean getBooleanFlagValue(String flag) {
|
||||
return getBooleanFlagValue(flag, false);
|
||||
}
|
||||
|
||||
public Boolean getBooleanFlagValue(String flag, Boolean def) {
|
||||
String val = this.flags.get(flag);
|
||||
if (val == null) {
|
||||
return def;
|
||||
}
|
||||
return ACFUtil.isTruthy(val);
|
||||
}
|
||||
|
||||
public Double getFlagValue(String flag, Number def) {
|
||||
return ACFUtil.parseDouble(this.flags.get(flag), def != null ? def.doubleValue() : null);
|
||||
}
|
||||
|
||||
public <T extends Annotation> T getAnnotation(Class<T> cls) {
|
||||
return param.getParameter().getAnnotation(cls);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user