diff --git a/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java index f68b243..186cadc 100644 --- a/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java @@ -55,6 +55,11 @@ public class IntegerArgumentType implements ArgumentType { if (end > -1) { raw = command.substring(0, end); } + + if (raw.length() < suffix.length()) { + throw ERROR_WRONG_SUFFIX.create(this.suffix); + } + String number = raw.substring(0, raw.length() - suffix.length()); String suffix = raw.substring(number.length()); @@ -74,7 +79,7 @@ public class IntegerArgumentType implements ArgumentType { return new FixedParsedArgument<>(raw, value); } catch (NumberFormatException ignored) { - throw ERROR_NOT_A_NUMBER.create(raw); + throw ERROR_NOT_A_NUMBER.create(number); } } diff --git a/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java index feb8d64..e6e4fef 100644 --- a/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java +++ b/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java @@ -97,6 +97,39 @@ public class IntegerArgumentTypeTest { } } + @Test + public void testParseEmpty() throws Exception { + try { + type.parse("", new CommandContextBuilder<>(dispatcher, source)); + fail(); + } catch (CommandException ex) { + assertThat(ex.getType(), is(IntegerArgumentType.ERROR_NOT_A_NUMBER)); + assertThat(ex.getData(), is(ImmutableMap.of("found", ""))); + } + } + + @Test + public void testParseEmpty_suffix() throws Exception { + try { + integer(0, 100, "L").parse("", new CommandContextBuilder<>(dispatcher, source)); + fail(); + } catch (CommandException ex) { + assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX)); + assertThat(ex.getData(), is(ImmutableMap.of("suffix", "L"))); + } + } + + @Test + public void testPars_suffix_onlySuffix() throws Exception { + try { + integer(0, 100, "L").parse("L", new CommandContextBuilder<>(dispatcher, source)); + fail(); + } catch (CommandException ex) { + assertThat(ex.getType(), is(IntegerArgumentType.ERROR_NOT_A_NUMBER)); + assertThat(ex.getData(), is(ImmutableMap.of("found", ""))); + } + } + @Test public void testParseTooLow() throws Exception { try {