From 37e435ce34ed0845df19f8283a1ef94068eb4ac4 Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 10 Mar 2018 16:07:28 -0500 Subject: [PATCH] add min/max validation flags. This should of really been conditions, but lazy to set that up proper for now. --- .../co/aikar/commands/CommandContexts.java | 19 ++++++++++++++++--- .../java/co/aikar/commands/MessageKeys.java | 1 + languages/core/acf-core_en.properties | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/co/aikar/commands/CommandContexts.java b/core/src/main/java/co/aikar/commands/CommandContexts.java index 7a0407ce..0128631b 100644 --- a/core/src/main/java/co/aikar/commands/CommandContexts.java +++ b/core/src/main/java/co/aikar/commands/CommandContexts.java @@ -124,14 +124,18 @@ public class CommandContexts { 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 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)); + } } diff --git a/core/src/main/java/co/aikar/commands/MessageKeys.java b/core/src/main/java/co/aikar/commands/MessageKeys.java index fd7be9ba..74e05fc8 100644 --- a/core/src/main/java/co/aikar/commands/MessageKeys.java +++ b/core/src/main/java/co/aikar/commands/MessageKeys.java @@ -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, diff --git a/languages/core/acf-core_en.properties b/languages/core/acf-core_en.properties index fb3a9a9e..496331ff 100644 --- a/languages/core/acf-core_en.properties +++ b/languages/core/acf-core_en.properties @@ -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: {search} acf-core.help_format = {command} {parameters} {separator} {description}