diff --git a/build.gradle b/build.gradle index f4341d8..e00b131 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ import groovy.io.FileType apply plugin: 'java-library' apply plugin: 'maven' -version = '0.1.25' +version = '0.1.27' group = 'com.mojang' task wrapper(type: Wrapper) { diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 8705d08..239e338 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -9,9 +9,6 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; -import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; -import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import com.mojang.brigadier.tree.CommandNode; @@ -30,11 +27,6 @@ import java.util.function.Predicate; import java.util.stream.Collectors; public class CommandDispatcher { - public static final SimpleCommandExceptionType ERROR_UNKNOWN_COMMAND = new SimpleCommandExceptionType("command.unknown.command", "Unknown command"); - public static final SimpleCommandExceptionType ERROR_UNKNOWN_ARGUMENT = new SimpleCommandExceptionType("command.unknown.argument", "Incorrect argument for command"); - public static final SimpleCommandExceptionType ERROR_EXPECTED_ARGUMENT_SEPARATOR = new SimpleCommandExceptionType("command.expected.separator", "Expected whitespace to end one argument, but found trailing data"); - public static final ParameterizedCommandExceptionType ERROR_PARSE_EXCEPTION = new ParameterizedCommandExceptionType("command.exception", "Could not parse command: ${message}", "message"); - public static final String ARGUMENT_SEPARATOR = " "; public static final char ARGUMENT_SEPARATOR_CHAR = ' '; private static final String USAGE_OPTIONAL_OPEN = "["; @@ -81,9 +73,9 @@ public class CommandDispatcher { if (parse.getExceptions().size() == 1) { throw parse.getExceptions().values().iterator().next(); } else if (parse.getContext().getRange().isEmpty()) { - throw ERROR_UNKNOWN_COMMAND.createWithContext(parse.getReader()); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader()); } else { - throw ERROR_UNKNOWN_ARGUMENT.createWithContext(parse.getReader()); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(parse.getReader()); } } @@ -152,7 +144,7 @@ public class CommandDispatcher { if (!foundCommand) { consumer.onCommandComplete(original, false, 0); - throw ERROR_UNKNOWN_COMMAND.createWithContext(parse.getReader()); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader()); } return forked ? successfulForks : result; @@ -190,11 +182,11 @@ public class CommandDispatcher { try { child.parse(reader, context); } catch (final RuntimeException ex) { - throw ERROR_PARSE_EXCEPTION.createWithContext(reader, ex.getMessage()); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherParseException().createWithContext(reader, ex.getMessage()); } if (reader.canRead()) { if (reader.peek() != ARGUMENT_SEPARATOR_CHAR) { - throw ERROR_EXPECTED_ARGUMENT_SEPARATOR.createWithContext(reader); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherExpectedArgumentSeparator().createWithContext(reader); } } } catch (final CommandSyntaxException ex) { diff --git a/src/main/java/com/mojang/brigadier/LiteralMessage.java b/src/main/java/com/mojang/brigadier/LiteralMessage.java new file mode 100644 index 0000000..04a0a78 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/LiteralMessage.java @@ -0,0 +1,19 @@ +package com.mojang.brigadier; + +public class LiteralMessage implements Message { + private final String string; + + public LiteralMessage(final String string) { + this.string = string; + } + + @Override + public String getString() { + return string; + } + + @Override + public String toString() { + return string; + } +} diff --git a/src/main/java/com/mojang/brigadier/StringReader.java b/src/main/java/com/mojang/brigadier/StringReader.java index b562e1e..390e6ef 100644 --- a/src/main/java/com/mojang/brigadier/StringReader.java +++ b/src/main/java/com/mojang/brigadier/StringReader.java @@ -1,26 +1,11 @@ package com.mojang.brigadier; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; -import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; public class StringReader implements ImmutableStringReader { private static final char SYNTAX_ESCAPE = '\\'; private static final char SYNTAX_QUOTE = '"'; - public static final SimpleCommandExceptionType ERROR_EXPECTED_START_OF_QUOTE = new SimpleCommandExceptionType("parsing.quote.expected.start", "Expected quote to start a string"); - public static final SimpleCommandExceptionType ERROR_EXPECTED_END_OF_QUOTE = new SimpleCommandExceptionType("parsing.quote.expected.end", "Unclosed quoted string"); - public static final ParameterizedCommandExceptionType ERROR_INVALID_ESCAPE = new ParameterizedCommandExceptionType("parsing.quote.escape", "Invalid escape sequence '\\${character}' in quoted string)", "character"); - public static final ParameterizedCommandExceptionType ERROR_INVALID_BOOL = new ParameterizedCommandExceptionType("parsing.bool.invalid", "Invalid bool, expected true or false but found '${value}'", "value"); - public static final ParameterizedCommandExceptionType ERROR_INVALID_INT = new ParameterizedCommandExceptionType("parsing.int.invalid", "Invalid integer '${value}'", "value"); - public static final SimpleCommandExceptionType ERROR_EXPECTED_INT = new SimpleCommandExceptionType("parsing.int.expected", "Expected integer"); - public static final ParameterizedCommandExceptionType ERROR_INVALID_DOUBLE = new ParameterizedCommandExceptionType("parsing.double.invalid", "Invalid double '${value}'", "value"); - public static final SimpleCommandExceptionType ERROR_EXPECTED_DOUBLE = new SimpleCommandExceptionType("parsing.double.expected", "Expected double"); - public static final ParameterizedCommandExceptionType ERROR_INVALID_FLOAT = new ParameterizedCommandExceptionType("parsing.float.invalid", "Invalid float '${value}'", "value"); - public static final SimpleCommandExceptionType ERROR_EXPECTED_FLOAT = new SimpleCommandExceptionType("parsing.float.expected", "Expected float"); - public static final SimpleCommandExceptionType ERROR_EXPECTED_BOOL = new SimpleCommandExceptionType("parsing.bool.expected", "Expected bool"); - public static final ParameterizedCommandExceptionType ERROR_EXPECTED_SYMBOL = new ParameterizedCommandExceptionType("parsing.expected", "Expected '${symbol}'", "symbol"); - private final String string; private int cursor; @@ -112,13 +97,13 @@ public class StringReader implements ImmutableStringReader { } final String number = string.substring(start, cursor); if (number.isEmpty()) { - throw ERROR_EXPECTED_INT.createWithContext(this); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt().createWithContext(this); } try { return Integer.parseInt(number); } catch (final NumberFormatException ex) { cursor = start; - throw ERROR_INVALID_INT.createWithContext(this, number); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidInt().createWithContext(this, number); } } @@ -129,13 +114,13 @@ public class StringReader implements ImmutableStringReader { } final String number = string.substring(start, cursor); if (number.isEmpty()) { - throw ERROR_EXPECTED_DOUBLE.createWithContext(this); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedDouble().createWithContext(this); } try { return Double.parseDouble(number); } catch (final NumberFormatException ex) { cursor = start; - throw ERROR_INVALID_DOUBLE.createWithContext(this, number); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidDouble().createWithContext(this, number); } } @@ -146,13 +131,13 @@ public class StringReader implements ImmutableStringReader { } final String number = string.substring(start, cursor); if (number.isEmpty()) { - throw ERROR_EXPECTED_FLOAT.createWithContext(this); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedFloat().createWithContext(this); } try { return Float.parseFloat(number); } catch (final NumberFormatException ex) { cursor = start; - throw ERROR_INVALID_FLOAT.createWithContext(this, number); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidFloat().createWithContext(this, number); } } @@ -176,7 +161,7 @@ public class StringReader implements ImmutableStringReader { if (!canRead()) { return ""; } else if (peek() != SYNTAX_QUOTE) { - throw ERROR_EXPECTED_START_OF_QUOTE.createWithContext(this); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote().createWithContext(this); } skip(); final StringBuilder result = new StringBuilder(); @@ -189,7 +174,7 @@ public class StringReader implements ImmutableStringReader { escaped = false; } else { setCursor(getCursor() - 1); - throw ERROR_INVALID_ESCAPE.createWithContext(this, String.valueOf(c)); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidEscape().createWithContext(this, String.valueOf(c)); } } else if (c == SYNTAX_ESCAPE) { escaped = true; @@ -200,7 +185,7 @@ public class StringReader implements ImmutableStringReader { } } - throw ERROR_EXPECTED_END_OF_QUOTE.createWithContext(this); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedEndOfQuote().createWithContext(this); } public String readString() throws CommandSyntaxException { @@ -215,7 +200,7 @@ public class StringReader implements ImmutableStringReader { final int start = cursor; final String value = readString(); if (value.isEmpty()) { - throw ERROR_EXPECTED_BOOL.createWithContext(this); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedBool().createWithContext(this); } if (value.equals("true")) { @@ -224,13 +209,13 @@ public class StringReader implements ImmutableStringReader { return false; } else { cursor = start; - throw ERROR_INVALID_BOOL.createWithContext(this, value); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidBool().createWithContext(this, value); } } public void expect(final char c) throws CommandSyntaxException { if (!canRead() || peek() != c) { - throw ERROR_EXPECTED_SYMBOL.createWithContext(this, String.valueOf(c)); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol().createWithContext(this, String.valueOf(c)); } skip(); } diff --git a/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java index f77f61c..ea3286c 100644 --- a/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java @@ -2,16 +2,12 @@ package com.mojang.brigadier.arguments; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; import java.util.Arrays; import java.util.Collection; public class DoubleArgumentType implements ArgumentType { - public static final ParameterizedCommandExceptionType ERROR_TOO_SMALL = new ParameterizedCommandExceptionType("argument.double.low", "Double must not be less than ${minimum}, found ${found}", "found", "minimum"); - public static final ParameterizedCommandExceptionType ERROR_TOO_BIG = new ParameterizedCommandExceptionType("argument.double.big", "Double must not be more than ${maximum}, found ${found}", "found", "maximum"); private static final Collection EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56"); private final double minimum; @@ -52,11 +48,11 @@ public class DoubleArgumentType implements ArgumentType { final double result = reader.readDouble(); if (result < minimum) { reader.setCursor(start); - throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooLow().createWithContext(reader, result, minimum); } if (result > maximum) { reader.setCursor(start); - throw ERROR_TOO_BIG.createWithContext(reader, result, maximum); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooHigh().createWithContext(reader, result, maximum); } return result; } diff --git a/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java index f7a5764..997b0b6 100644 --- a/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java @@ -2,16 +2,12 @@ package com.mojang.brigadier.arguments; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; import java.util.Arrays; import java.util.Collection; public class FloatArgumentType implements ArgumentType { - public static final ParameterizedCommandExceptionType ERROR_TOO_SMALL = new ParameterizedCommandExceptionType("argument.float.low", "Float must not be less than ${minimum}, found ${found}", "found", "minimum"); - public static final ParameterizedCommandExceptionType ERROR_TOO_BIG = new ParameterizedCommandExceptionType("argument.float.big", "Float must not be more than ${maximum}, found ${found}", "found", "maximum"); private static final Collection EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56"); private final float minimum; @@ -52,11 +48,11 @@ public class FloatArgumentType implements ArgumentType { final float result = reader.readFloat(); if (result < minimum) { reader.setCursor(start); - throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooLow().createWithContext(reader, result, minimum); } if (result > maximum) { reader.setCursor(start); - throw ERROR_TOO_BIG.createWithContext(reader, result, maximum); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooHigh().createWithContext(reader, result, maximum); } return result; } diff --git a/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java index 757e9f3..58e2014 100644 --- a/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java @@ -2,16 +2,12 @@ package com.mojang.brigadier.arguments; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; import java.util.Arrays; import java.util.Collection; public class IntegerArgumentType implements ArgumentType { - public static final ParameterizedCommandExceptionType ERROR_TOO_SMALL = new ParameterizedCommandExceptionType("argument.integer.low", "Integer must not be less than ${minimum}, found ${found}", "found", "minimum"); - public static final ParameterizedCommandExceptionType ERROR_TOO_BIG = new ParameterizedCommandExceptionType("argument.integer.big", "Integer must not be more than ${maximum}, found ${found}", "found", "maximum"); private static final Collection EXAMPLES = Arrays.asList("0", "123", "-123"); private final int minimum; @@ -52,11 +48,11 @@ public class IntegerArgumentType implements ArgumentType { final int result = reader.readInt(); if (result < minimum) { reader.setCursor(start); - throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooLow().createWithContext(reader, result, minimum); } if (result > maximum) { reader.setCursor(start); - throw ERROR_TOO_BIG.createWithContext(reader, result, maximum); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh().createWithContext(reader, result, maximum); } return result; } diff --git a/src/main/java/com/mojang/brigadier/exceptions/BuiltInExceptionProvider.java b/src/main/java/com/mojang/brigadier/exceptions/BuiltInExceptionProvider.java new file mode 100644 index 0000000..ab365b7 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/exceptions/BuiltInExceptionProvider.java @@ -0,0 +1,49 @@ +package com.mojang.brigadier.exceptions; + +public interface BuiltInExceptionProvider { + Dynamic2CommandExceptionType doubleTooLow(); + + Dynamic2CommandExceptionType doubleTooHigh(); + + Dynamic2CommandExceptionType floatTooLow(); + + Dynamic2CommandExceptionType floatTooHigh(); + + Dynamic2CommandExceptionType integerTooLow(); + + Dynamic2CommandExceptionType integerTooHigh(); + + DynamicCommandExceptionType literalIncorrect(); + + SimpleCommandExceptionType readerExpectedStartOfQuote(); + + SimpleCommandExceptionType readerExpectedEndOfQuote(); + + DynamicCommandExceptionType readerInvalidEscape(); + + DynamicCommandExceptionType readerInvalidBool(); + + DynamicCommandExceptionType readerInvalidInt(); + + SimpleCommandExceptionType readerExpectedInt(); + + DynamicCommandExceptionType readerInvalidDouble(); + + SimpleCommandExceptionType readerExpectedDouble(); + + DynamicCommandExceptionType readerInvalidFloat(); + + SimpleCommandExceptionType readerExpectedFloat(); + + SimpleCommandExceptionType readerExpectedBool(); + + DynamicCommandExceptionType readerExpectedSymbol(); + + SimpleCommandExceptionType dispatcherUnknownCommand(); + + SimpleCommandExceptionType dispatcherUnknownArgument(); + + SimpleCommandExceptionType dispatcherExpectedArgumentSeparator(); + + DynamicCommandExceptionType dispatcherParseException(); +} diff --git a/src/main/java/com/mojang/brigadier/exceptions/BuiltInExceptions.java b/src/main/java/com/mojang/brigadier/exceptions/BuiltInExceptions.java new file mode 100644 index 0000000..91e8fdd --- /dev/null +++ b/src/main/java/com/mojang/brigadier/exceptions/BuiltInExceptions.java @@ -0,0 +1,149 @@ +package com.mojang.brigadier.exceptions; + +import com.mojang.brigadier.LiteralMessage; + +public class BuiltInExceptions implements BuiltInExceptionProvider { + private static final Dynamic2CommandExceptionType DOUBLE_TOO_SMALL = new Dynamic2CommandExceptionType((found, min) -> new LiteralMessage("Double must not be less than " + min + ", found " + found)); + private static final Dynamic2CommandExceptionType DOUBLE_TOO_BIG = new Dynamic2CommandExceptionType((found, max) -> new LiteralMessage("Double must not be more than " + max + ", found " + found)); + + private static final Dynamic2CommandExceptionType FLOAT_TOO_SMALL = new Dynamic2CommandExceptionType((found, min) -> new LiteralMessage("Float must not be less than " + min + ", found " + found)); + private static final Dynamic2CommandExceptionType FLOAT_TOO_BIG = new Dynamic2CommandExceptionType((found, max) -> new LiteralMessage("Float must not be more than " + max + ", found " + found)); + + private static final Dynamic2CommandExceptionType INTEGER_TOO_SMALL = new Dynamic2CommandExceptionType((found, min) -> new LiteralMessage("Integer must not be less than " + min + ", found " + found)); + private static final Dynamic2CommandExceptionType INTEGER_TOO_BIG = new Dynamic2CommandExceptionType((found, max) -> new LiteralMessage("Integer must not be more than " + max + ", found " + found)); + + private static final DynamicCommandExceptionType LITERAL_INCORRECT = new DynamicCommandExceptionType(expected -> new LiteralMessage("Expected literal " + expected)); + + private static final SimpleCommandExceptionType READER_EXPECTED_START_OF_QUOTE = new SimpleCommandExceptionType(new LiteralMessage("Expected quote to start a string")); + private static final SimpleCommandExceptionType READER_EXPECTED_END_OF_QUOTE = new SimpleCommandExceptionType(new LiteralMessage("Unclosed quoted string")); + private static final DynamicCommandExceptionType READER_INVALID_ESCAPE = new DynamicCommandExceptionType(character -> new LiteralMessage("Invalid escape sequence '" + character + "' in quoted string")); + private static final DynamicCommandExceptionType READER_INVALID_BOOL = new DynamicCommandExceptionType(value -> new LiteralMessage("Invalid bool, expected true or false but found '" + value + "'")); + private static final DynamicCommandExceptionType READER_INVALID_INT = new DynamicCommandExceptionType(value -> new LiteralMessage("Invalid integer '" + value + "'")); + private static final SimpleCommandExceptionType READER_EXPECTED_INT = new SimpleCommandExceptionType(new LiteralMessage("Expected integer")); + private static final DynamicCommandExceptionType READER_INVALID_DOUBLE = new DynamicCommandExceptionType(value -> new LiteralMessage("Invalid double '" + value + "'")); + private static final SimpleCommandExceptionType READER_EXPECTED_DOUBLE = new SimpleCommandExceptionType(new LiteralMessage("Expected double")); + private static final DynamicCommandExceptionType READER_INVALID_FLOAT = new DynamicCommandExceptionType(value -> new LiteralMessage("Invalid float '" + value + "'")); + private static final SimpleCommandExceptionType READER_EXPECTED_FLOAT = new SimpleCommandExceptionType(new LiteralMessage("Expected float")); + private static final SimpleCommandExceptionType READER_EXPECTED_BOOL = new SimpleCommandExceptionType(new LiteralMessage("Expected bool")); + private static final DynamicCommandExceptionType READER_EXPECTED_SYMBOL = new DynamicCommandExceptionType(symbol -> new LiteralMessage("Expected '" + symbol + "'")); + + private static final SimpleCommandExceptionType DISPATCHER_UNKNOWN_COMMAND = new SimpleCommandExceptionType(new LiteralMessage("Unknown command")); + private static final SimpleCommandExceptionType DISPATCHER_UNKNOWN_ARGUMENT = new SimpleCommandExceptionType(new LiteralMessage("Incorrect argument for command")); + private static final SimpleCommandExceptionType DISPATCHER_EXPECTED_ARGUMENT_SEPARATOR = new SimpleCommandExceptionType(new LiteralMessage("Expected whitespace to end one argument, but found trailing data")); + private static final DynamicCommandExceptionType DISPATCHER_PARSE_EXCEPTION = new DynamicCommandExceptionType(message -> new LiteralMessage("Could not parse command: " + message)); + + @Override + public Dynamic2CommandExceptionType doubleTooLow() { + return DOUBLE_TOO_SMALL; + } + + @Override + public Dynamic2CommandExceptionType doubleTooHigh() { + return DOUBLE_TOO_BIG; + } + + @Override + public Dynamic2CommandExceptionType floatTooLow() { + return FLOAT_TOO_SMALL; + } + + @Override + public Dynamic2CommandExceptionType floatTooHigh() { + return FLOAT_TOO_BIG; + } + + @Override + public Dynamic2CommandExceptionType integerTooLow() { + return INTEGER_TOO_SMALL; + } + + @Override + public Dynamic2CommandExceptionType integerTooHigh() { + return INTEGER_TOO_BIG; + } + + @Override + public DynamicCommandExceptionType literalIncorrect() { + return LITERAL_INCORRECT; + } + + @Override + public SimpleCommandExceptionType readerExpectedStartOfQuote() { + return READER_EXPECTED_START_OF_QUOTE; + } + + @Override + public SimpleCommandExceptionType readerExpectedEndOfQuote() { + return READER_EXPECTED_END_OF_QUOTE; + } + + @Override + public DynamicCommandExceptionType readerInvalidEscape() { + return READER_INVALID_ESCAPE; + } + + @Override + public DynamicCommandExceptionType readerInvalidBool() { + return READER_INVALID_BOOL; + } + + @Override + public DynamicCommandExceptionType readerInvalidInt() { + return READER_INVALID_INT; + } + + @Override + public SimpleCommandExceptionType readerExpectedInt() { + return READER_EXPECTED_INT; + } + + @Override + public DynamicCommandExceptionType readerInvalidDouble() { + return READER_INVALID_DOUBLE; + } + + @Override + public SimpleCommandExceptionType readerExpectedDouble() { + return READER_EXPECTED_DOUBLE; + } + + @Override + public DynamicCommandExceptionType readerInvalidFloat() { + return READER_INVALID_FLOAT; + } + + @Override + public SimpleCommandExceptionType readerExpectedFloat() { + return READER_EXPECTED_FLOAT; + } + + @Override + public SimpleCommandExceptionType readerExpectedBool() { + return READER_EXPECTED_BOOL; + } + + @Override + public DynamicCommandExceptionType readerExpectedSymbol() { + return READER_EXPECTED_SYMBOL; + } + + @Override + public SimpleCommandExceptionType dispatcherUnknownCommand() { + return DISPATCHER_UNKNOWN_COMMAND; + } + + @Override + public SimpleCommandExceptionType dispatcherUnknownArgument() { + return DISPATCHER_UNKNOWN_ARGUMENT; + } + + @Override + public SimpleCommandExceptionType dispatcherExpectedArgumentSeparator() { + return DISPATCHER_EXPECTED_ARGUMENT_SEPARATOR; + } + + @Override + public DynamicCommandExceptionType dispatcherParseException() { + return DISPATCHER_PARSE_EXCEPTION; + } +} diff --git a/src/main/java/com/mojang/brigadier/exceptions/CommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/CommandExceptionType.java index 41e36fc..14dbd9d 100644 --- a/src/main/java/com/mojang/brigadier/exceptions/CommandExceptionType.java +++ b/src/main/java/com/mojang/brigadier/exceptions/CommandExceptionType.java @@ -1,9 +1,4 @@ package com.mojang.brigadier.exceptions; -import java.util.Map; - public interface CommandExceptionType { - String getTypeName(); - - String getErrorMessage(Map data); } diff --git a/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java b/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java index 4b08d15..24beefd 100644 --- a/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java +++ b/src/main/java/com/mojang/brigadier/exceptions/CommandSyntaxException.java @@ -1,35 +1,36 @@ package com.mojang.brigadier.exceptions; -import java.util.Map; +import com.mojang.brigadier.Message; public class CommandSyntaxException extends Exception { public static final int CONTEXT_AMOUNT = 10; public static boolean ENABLE_COMMAND_STACK_TRACES = true; + public static BuiltInExceptionProvider BUILT_IN_EXCEPTIONS = new BuiltInExceptions(); private final CommandExceptionType type; - private final Map data; + private final Message message; private final String input; private final int cursor; - public CommandSyntaxException(final CommandExceptionType type, final Map data) { - super(type.getTypeName(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES); + public CommandSyntaxException(final CommandExceptionType type, final Message message) { + super(message.getString(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES); this.type = type; - this.data = data; + this.message = message; this.input = null; this.cursor = -1; } - public CommandSyntaxException(final CommandExceptionType type, final Map data, final String input, final int cursor) { - super(type.getTypeName(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES); + public CommandSyntaxException(final CommandExceptionType type, final Message message, final String input, final int cursor) { + super(message.getString(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES); this.type = type; - this.data = data; + this.message = message; this.input = input; this.cursor = cursor; } @Override public String getMessage() { - String message = type.getErrorMessage(data); + String message = this.message.getString(); final String context = getContext(); if (context != null) { message += " at position " + cursor + ": " + context; @@ -37,6 +38,10 @@ public class CommandSyntaxException extends Exception { return message; } + public Message getRawMessage() { + return message; + } + public String getContext() { if (input == null || cursor < 0) { return null; @@ -58,10 +63,6 @@ public class CommandSyntaxException extends Exception { return type; } - public Map getData() { - return data; - } - public String getInput() { return input; } diff --git a/src/main/java/com/mojang/brigadier/exceptions/Dynamic2CommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/Dynamic2CommandExceptionType.java new file mode 100644 index 0000000..5f4b233 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/exceptions/Dynamic2CommandExceptionType.java @@ -0,0 +1,24 @@ +package com.mojang.brigadier.exceptions; + +import com.mojang.brigadier.ImmutableStringReader; +import com.mojang.brigadier.Message; + +public class Dynamic2CommandExceptionType implements CommandExceptionType { + private final Function function; + + public Dynamic2CommandExceptionType(final Function function) { + this.function = function; + } + + public CommandSyntaxException create(final Object a, final Object b) { + return new CommandSyntaxException(this, function.apply(a, b)); + } + + public CommandSyntaxException createWithContext(final ImmutableStringReader reader, final Object a, final Object b) { + return new CommandSyntaxException(this, function.apply(a, b), reader.getString(), reader.getCursor()); + } + + public interface Function { + Message apply(Object a, Object b); + } +} diff --git a/src/main/java/com/mojang/brigadier/exceptions/Dynamic3CommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/Dynamic3CommandExceptionType.java new file mode 100644 index 0000000..8e0f00f --- /dev/null +++ b/src/main/java/com/mojang/brigadier/exceptions/Dynamic3CommandExceptionType.java @@ -0,0 +1,24 @@ +package com.mojang.brigadier.exceptions; + +import com.mojang.brigadier.ImmutableStringReader; +import com.mojang.brigadier.Message; + +public class Dynamic3CommandExceptionType implements CommandExceptionType { + private final Function function; + + public Dynamic3CommandExceptionType(final Function function) { + this.function = function; + } + + public CommandSyntaxException create(final Object a, final Object b, final Object c) { + return new CommandSyntaxException(this, function.apply(a, b, c)); + } + + public CommandSyntaxException createWithContext(final ImmutableStringReader reader, final Object a, final Object b, final Object c) { + return new CommandSyntaxException(this, function.apply(a, b, c), reader.getString(), reader.getCursor()); + } + + public interface Function { + Message apply(Object a, Object b, Object c); + } +} diff --git a/src/main/java/com/mojang/brigadier/exceptions/Dynamic4CommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/Dynamic4CommandExceptionType.java new file mode 100644 index 0000000..2f0df23 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/exceptions/Dynamic4CommandExceptionType.java @@ -0,0 +1,24 @@ +package com.mojang.brigadier.exceptions; + +import com.mojang.brigadier.ImmutableStringReader; +import com.mojang.brigadier.Message; + +public class Dynamic4CommandExceptionType implements CommandExceptionType { + private final Function function; + + public Dynamic4CommandExceptionType(final Function function) { + this.function = function; + } + + public CommandSyntaxException create(final Object a, final Object b, final Object c, final Object d) { + return new CommandSyntaxException(this, function.apply(a, b, c, d)); + } + + public CommandSyntaxException createWithContext(final ImmutableStringReader reader, final Object a, final Object b, final Object c, final Object d) { + return new CommandSyntaxException(this, function.apply(a, b, c, d), reader.getString(), reader.getCursor()); + } + + public interface Function { + Message apply(Object a, Object b, Object c, Object d); + } +} diff --git a/src/main/java/com/mojang/brigadier/exceptions/DynamicCommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/DynamicCommandExceptionType.java new file mode 100644 index 0000000..c562f61 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/exceptions/DynamicCommandExceptionType.java @@ -0,0 +1,22 @@ +package com.mojang.brigadier.exceptions; + +import com.mojang.brigadier.ImmutableStringReader; +import com.mojang.brigadier.Message; + +import java.util.function.Function; + +public class DynamicCommandExceptionType implements CommandExceptionType { + private final Function function; + + public DynamicCommandExceptionType(final Function function) { + this.function = function; + } + + public CommandSyntaxException create(final Object arg) { + return new CommandSyntaxException(this, function.apply(arg)); + } + + public CommandSyntaxException createWithContext(final ImmutableStringReader reader, final Object arg) { + return new CommandSyntaxException(this, function.apply(arg), reader.getString(), reader.getCursor()); + } +} diff --git a/src/main/java/com/mojang/brigadier/exceptions/DynamicNCommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/DynamicNCommandExceptionType.java new file mode 100644 index 0000000..f3ab756 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/exceptions/DynamicNCommandExceptionType.java @@ -0,0 +1,24 @@ +package com.mojang.brigadier.exceptions; + +import com.mojang.brigadier.ImmutableStringReader; +import com.mojang.brigadier.Message; + +public class DynamicNCommandExceptionType implements CommandExceptionType { + private final Function function; + + public DynamicNCommandExceptionType(final Function function) { + this.function = function; + } + + public CommandSyntaxException create(final Object a, final Object... args) { + return new CommandSyntaxException(this, function.apply(args)); + } + + public CommandSyntaxException createWithContext(final ImmutableStringReader reader, final Object... args) { + return new CommandSyntaxException(this, function.apply(args), reader.getString(), reader.getCursor()); + } + + public interface Function { + Message apply(Object[] args); + } +} diff --git a/src/main/java/com/mojang/brigadier/exceptions/ParameterizedCommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/ParameterizedCommandExceptionType.java deleted file mode 100644 index d0e7c36..0000000 --- a/src/main/java/com/mojang/brigadier/exceptions/ParameterizedCommandExceptionType.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.mojang.brigadier.exceptions; - -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableMap; -import com.mojang.brigadier.ImmutableStringReader; - -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ParameterizedCommandExceptionType implements CommandExceptionType { - private static final Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)}"); - private static final Joiner JOINER = Joiner.on(", "); - - private final String name; - private final String message; - private final String[] keys; - - public ParameterizedCommandExceptionType(final String name, final String message, final String... keys) { - this.name = name; - this.message = message; - this.keys = keys; - } - - @Override - public String getTypeName() { - return name; - } - - @Override - public String getErrorMessage(final Map data) { - final Matcher matcher = PATTERN.matcher(message); - final StringBuffer result = new StringBuffer(); - while (matcher.find()) { - matcher.appendReplacement(result, Matcher.quoteReplacement(data.get(matcher.group(1)))); - } - matcher.appendTail(result); - return result.toString(); - } - - public CommandSyntaxException create(final Object... values) { - return new CommandSyntaxException(this, createMap(values)); - } - - public CommandSyntaxException createWithContext(final ImmutableStringReader reader, final Object... values) { - return new CommandSyntaxException(this, createMap(values), reader.getString(), reader.getCursor()); - } - - public Map createMap(final Object... values) { - if (values.length != keys.length) { - throw new IllegalArgumentException("Invalid values! (Expected: " + JOINER.join(keys) + ")"); - } - - ImmutableMap.Builder builder = ImmutableMap.builder(); - - for (int i = 0; i < keys.length; i++) { - builder = builder.put(keys[i], String.valueOf(values[i])); - } - return builder.build(); - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (!(o instanceof CommandExceptionType)) return false; - - final CommandExceptionType that = (CommandExceptionType) o; - - return getTypeName().equals(that.getTypeName()); - } - - @Override - public int hashCode() { - return getTypeName().hashCode(); - } - - @Override - public String toString() { - return message; - } -} diff --git a/src/main/java/com/mojang/brigadier/exceptions/SimpleCommandExceptionType.java b/src/main/java/com/mojang/brigadier/exceptions/SimpleCommandExceptionType.java index 339a277..4f50fd6 100644 --- a/src/main/java/com/mojang/brigadier/exceptions/SimpleCommandExceptionType.java +++ b/src/main/java/com/mojang/brigadier/exceptions/SimpleCommandExceptionType.java @@ -1,54 +1,25 @@ package com.mojang.brigadier.exceptions; -import com.google.common.collect.ImmutableMap; import com.mojang.brigadier.ImmutableStringReader; - -import java.util.Map; +import com.mojang.brigadier.Message; public class SimpleCommandExceptionType implements CommandExceptionType { - private final String name; - private final String message; + private final Message message; - public SimpleCommandExceptionType(final String name, final String message) { - this.name = name; + public SimpleCommandExceptionType(final Message message) { this.message = message; } - @Override - public String getTypeName() { - return name; - } - - @Override - public String getErrorMessage(final Map data) { - return message; - } - public CommandSyntaxException create() { - return new CommandSyntaxException(this, ImmutableMap.of()); + return new CommandSyntaxException(this, message); } public CommandSyntaxException createWithContext(final ImmutableStringReader reader) { - return new CommandSyntaxException(this, ImmutableMap.of(), reader.getString(), reader.getCursor()); - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (!(o instanceof CommandExceptionType)) return false; - - final CommandExceptionType that = (CommandExceptionType) o; - - return getTypeName().equals(that.getTypeName()); - } - - @Override - public int hashCode() { - return getTypeName().hashCode(); + return new CommandSyntaxException(this, message, reader.getString(), reader.getCursor()); } @Override public String toString() { - return message; + return message.getString(); } } diff --git a/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java b/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java index 705c9f3..4f3e8a6 100644 --- a/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java @@ -8,7 +8,6 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -18,8 +17,6 @@ import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; public class LiteralCommandNode extends CommandNode { - public static final ParameterizedCommandExceptionType ERROR_INCORRECT_LITERAL = new ParameterizedCommandExceptionType("argument.literal.incorrect", "Expected literal ${expected}", "expected"); - private final String literal; public LiteralCommandNode(final String literal, final Command command, final Predicate requirement, final CommandNode redirect, final RedirectModifier modifier, final boolean forks) { @@ -45,7 +42,7 @@ public class LiteralCommandNode extends CommandNode { return; } - throw ERROR_INCORRECT_LITERAL.createWithContext(reader, literal); + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect().createWithContext(reader, literal); } private int parse(final StringReader reader) { diff --git a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java index c3ad143..873c003 100644 --- a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java +++ b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java @@ -11,8 +11,6 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import java.util.Collections; - import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument; @@ -74,8 +72,7 @@ public class CommandDispatcherTest { subject.execute("foo", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand())); assertThat(ex.getCursor(), is(0)); } } @@ -88,8 +85,7 @@ public class CommandDispatcherTest { subject.execute("foo", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand())); assertThat(ex.getCursor(), is(0)); } } @@ -102,8 +98,7 @@ public class CommandDispatcherTest { subject.execute("", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand())); assertThat(ex.getCursor(), is(0)); } } @@ -116,8 +111,7 @@ public class CommandDispatcherTest { subject.execute("foo bar", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument())); assertThat(ex.getCursor(), is(4)); } } @@ -130,8 +124,7 @@ public class CommandDispatcherTest { subject.execute("foo baz", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument())); assertThat(ex.getCursor(), is(4)); } } @@ -148,8 +141,7 @@ public class CommandDispatcherTest { subject.execute("foo unknown", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument())); assertThat(ex.getCursor(), is(4)); } } @@ -310,8 +302,7 @@ public class CommandDispatcherTest { subject.execute("foo 5", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand())); assertThat(ex.getCursor(), is(5)); } } @@ -335,8 +326,7 @@ public class CommandDispatcherTest { subject.execute("foo$", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand())); assertThat(ex.getCursor(), is(0)); } } @@ -351,8 +341,7 @@ public class CommandDispatcherTest { subject.execute("foo bar", source); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT)); - assertThat(ex.getData(), is(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt())); assertThat(ex.getCursor(), is(4)); } } diff --git a/src/test/java/com/mojang/brigadier/StringReaderTest.java b/src/test/java/com/mojang/brigadier/StringReaderTest.java index ce09896..91dacfd 100644 --- a/src/test/java/com/mojang/brigadier/StringReaderTest.java +++ b/src/test/java/com/mojang/brigadier/StringReaderTest.java @@ -1,11 +1,8 @@ package com.mojang.brigadier; -import com.google.common.collect.ImmutableMap; import com.mojang.brigadier.exceptions.CommandSyntaxException; import org.junit.Test; -import java.util.Collections; - import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -217,8 +214,7 @@ public class StringReaderTest { try { new StringReader("hello world\"").readQuotedString(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_START_OF_QUOTE)); - assertThat(ex.getData(), equalTo(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote())); assertThat(ex.getCursor(), is(0)); } } @@ -228,8 +224,7 @@ public class StringReaderTest { try { new StringReader("\"hello world").readQuotedString(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_END_OF_QUOTE)); - assertThat(ex.getData(), equalTo(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedEndOfQuote())); assertThat(ex.getCursor(), is(12)); } } @@ -239,8 +234,7 @@ public class StringReaderTest { try { new StringReader("\"hello\\nworld\"").readQuotedString(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_INVALID_ESCAPE)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("character", "n"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidEscape())); assertThat(ex.getCursor(), is(7)); } } @@ -266,8 +260,7 @@ public class StringReaderTest { try { new StringReader("12.34").readInt(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_INVALID_INT)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "12.34"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidInt())); assertThat(ex.getCursor(), is(0)); } } @@ -277,8 +270,7 @@ public class StringReaderTest { try { new StringReader("").readInt(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT)); - assertThat(ex.getData(), equalTo(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt())); assertThat(ex.getCursor(), is(0)); } } @@ -328,8 +320,7 @@ public class StringReaderTest { try { new StringReader("12.34.56").readDouble(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_INVALID_DOUBLE)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "12.34.56"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidDouble())); assertThat(ex.getCursor(), is(0)); } } @@ -339,8 +330,7 @@ public class StringReaderTest { try { new StringReader("").readDouble(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_DOUBLE)); - assertThat(ex.getData(), equalTo(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedDouble())); assertThat(ex.getCursor(), is(0)); } } @@ -390,8 +380,7 @@ public class StringReaderTest { try { new StringReader("12.34.56").readFloat(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_INVALID_FLOAT)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "12.34.56"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidFloat())); assertThat(ex.getCursor(), is(0)); } } @@ -401,8 +390,7 @@ public class StringReaderTest { try { new StringReader("").readFloat(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_FLOAT)); - assertThat(ex.getData(), equalTo(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedFloat())); assertThat(ex.getCursor(), is(0)); } } @@ -437,8 +425,7 @@ public class StringReaderTest { reader.expect('a'); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_SYMBOL)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("symbol", "a"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol())); assertThat(ex.getCursor(), is(0)); } } @@ -450,8 +437,7 @@ public class StringReaderTest { reader.expect('a'); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_SYMBOL)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("symbol", "a"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol())); assertThat(ex.getCursor(), is(0)); } } @@ -470,8 +456,7 @@ public class StringReaderTest { reader.readBoolean(); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_INVALID_BOOL)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "tuesday"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidBool())); assertThat(ex.getCursor(), is(0)); } } @@ -483,8 +468,7 @@ public class StringReaderTest { reader.readBoolean(); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_BOOL)); - assertThat(ex.getData(), equalTo(Collections.emptyMap())); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedBool())); assertThat(ex.getCursor(), is(0)); } } diff --git a/src/test/java/com/mojang/brigadier/arguments/DoubleArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/DoubleArgumentTypeTest.java index 3578f48..13f90af 100644 --- a/src/test/java/com/mojang/brigadier/arguments/DoubleArgumentTypeTest.java +++ b/src/test/java/com/mojang/brigadier/arguments/DoubleArgumentTypeTest.java @@ -1,6 +1,5 @@ package com.mojang.brigadier.arguments; -import com.google.common.collect.ImmutableMap; import com.google.common.testing.EqualsTester; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.context.CommandContextBuilder; @@ -12,7 +11,6 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import static com.mojang.brigadier.arguments.DoubleArgumentType.doubleArg; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasToString; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -43,8 +41,7 @@ public class DoubleArgumentTypeTest { doubleArg(0, 100).parse(reader); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(DoubleArgumentType.ERROR_TOO_SMALL)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("found", "-5.0", "minimum", "0.0"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooLow())); assertThat(ex.getCursor(), is(0)); } } @@ -56,8 +53,7 @@ public class DoubleArgumentTypeTest { doubleArg(-100, 0).parse(reader); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(DoubleArgumentType.ERROR_TOO_BIG)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("found", "5.0", "maximum", "0.0"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooHigh())); assertThat(ex.getCursor(), is(0)); } } diff --git a/src/test/java/com/mojang/brigadier/arguments/FloatArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/FloatArgumentTypeTest.java index 8a35985..0c1852d 100644 --- a/src/test/java/com/mojang/brigadier/arguments/FloatArgumentTypeTest.java +++ b/src/test/java/com/mojang/brigadier/arguments/FloatArgumentTypeTest.java @@ -1,6 +1,5 @@ package com.mojang.brigadier.arguments; -import com.google.common.collect.ImmutableMap; import com.google.common.testing.EqualsTester; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.context.CommandContextBuilder; @@ -12,7 +11,6 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import static com.mojang.brigadier.arguments.FloatArgumentType.floatArg; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasToString; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -43,8 +41,7 @@ public class FloatArgumentTypeTest { floatArg(0, 100).parse(reader); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(FloatArgumentType.ERROR_TOO_SMALL)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("found", "-5.0", "minimum", "0.0"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooLow())); assertThat(ex.getCursor(), is(0)); } } @@ -56,8 +53,7 @@ public class FloatArgumentTypeTest { floatArg(-100, 0).parse(reader); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(FloatArgumentType.ERROR_TOO_BIG)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("found", "5.0", "maximum", "0.0"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooHigh())); assertThat(ex.getCursor(), is(0)); } } diff --git a/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java index ba218a7..3cb8c2f 100644 --- a/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java +++ b/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java @@ -1,6 +1,5 @@ package com.mojang.brigadier.arguments; -import com.google.common.collect.ImmutableMap; import com.google.common.testing.EqualsTester; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.context.CommandContextBuilder; @@ -12,7 +11,6 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasToString; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -43,8 +41,7 @@ public class IntegerArgumentTypeTest { integer(0, 100).parse(reader); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("found", "-5", "minimum", "0"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooLow())); assertThat(ex.getCursor(), is(0)); } } @@ -56,8 +53,7 @@ public class IntegerArgumentTypeTest { integer(-100, 0).parse(reader); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG)); - assertThat(ex.getData(), equalTo(ImmutableMap.of("found", "5", "maximum", "0"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh())); assertThat(ex.getCursor(), is(0)); } } diff --git a/src/test/java/com/mojang/brigadier/exceptions/DynamicCommandSyntaxExceptionTypeTest.java b/src/test/java/com/mojang/brigadier/exceptions/DynamicCommandSyntaxExceptionTypeTest.java new file mode 100644 index 0000000..4b94ab0 --- /dev/null +++ b/src/test/java/com/mojang/brigadier/exceptions/DynamicCommandSyntaxExceptionTypeTest.java @@ -0,0 +1,29 @@ +package com.mojang.brigadier.exceptions; + +import com.mojang.brigadier.LiteralMessage; +import com.mojang.brigadier.StringReader; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@SuppressWarnings("ThrowableResultOfMethodCallIgnored") +public class DynamicCommandSyntaxExceptionTypeTest { + private DynamicCommandExceptionType type; + + @Before + public void setUp() throws Exception { + type = new DynamicCommandExceptionType(name -> new LiteralMessage("Hello, " + name + "!")); + } + + @Test + public void createWithContext() throws Exception { + final StringReader reader = new StringReader("Foo bar"); + reader.setCursor(5); + final CommandSyntaxException exception = type.createWithContext(reader, "World"); + assertThat(exception.getType(), is(type)); + assertThat(exception.getInput(), is("Foo bar")); + assertThat(exception.getCursor(), is(5)); + } +} \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/exceptions/ParameterizedCommandSyntaxExceptionTypeTest.java b/src/test/java/com/mojang/brigadier/exceptions/ParameterizedCommandSyntaxExceptionTypeTest.java deleted file mode 100644 index 549b701..0000000 --- a/src/test/java/com/mojang/brigadier/exceptions/ParameterizedCommandSyntaxExceptionTypeTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mojang.brigadier.exceptions; - -import com.google.common.collect.ImmutableMap; -import com.google.common.testing.EqualsTester; -import com.mojang.brigadier.StringReader; -import org.junit.Before; -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; - -@SuppressWarnings("ThrowableResultOfMethodCallIgnored") -public class ParameterizedCommandSyntaxExceptionTypeTest { - private ParameterizedCommandExceptionType type; - - @Before - public void setUp() throws Exception { - type = new ParameterizedCommandExceptionType("foo", "Hello, ${name}!", "name"); - } - - @Test(expected = IllegalArgumentException.class) - public void createMap_TooManyArguments() throws Exception { - type.createMap("World", "Universe"); - } - - @Test - public void createWithContext() throws Exception { - final StringReader reader = new StringReader("Foo bar"); - reader.setCursor(5); - final CommandSyntaxException exception = type.createWithContext(reader, "World"); - assertThat(exception.getType(), is(type)); - assertThat(exception.getData(), is(ImmutableMap.of("name", "World"))); - assertThat(exception.getInput(), is("Foo bar")); - assertThat(exception.getCursor(), is(5)); - } - - @Test - public void testEquals() throws Exception { - new EqualsTester() - .addEqualityGroup(new ParameterizedCommandExceptionType("foo", "Hello, world!"), new ParameterizedCommandExceptionType("foo", "Hello, universe!"), new ParameterizedCommandExceptionType("foo", "Hello, world!", "bar")) - .addEqualityGroup(new ParameterizedCommandExceptionType("bar", "Hello, world!"), new ParameterizedCommandExceptionType("bar", "Hello, universe!"), new ParameterizedCommandExceptionType("bar", "Hello, world!", "bar")) - .testEquals(); - } -} \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/exceptions/SimpleCommandSyntaxExceptionTypeTest.java b/src/test/java/com/mojang/brigadier/exceptions/SimpleCommandSyntaxExceptionTypeTest.java index 77f3d0c..365abee 100644 --- a/src/test/java/com/mojang/brigadier/exceptions/SimpleCommandSyntaxExceptionTypeTest.java +++ b/src/test/java/com/mojang/brigadier/exceptions/SimpleCommandSyntaxExceptionTypeTest.java @@ -1,11 +1,9 @@ package com.mojang.brigadier.exceptions; -import com.google.common.testing.EqualsTester; +import com.mojang.brigadier.LiteralMessage; import com.mojang.brigadier.StringReader; import org.junit.Test; -import java.util.Collections; - import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; @@ -16,39 +14,31 @@ import static org.mockito.Mockito.mock; public class SimpleCommandSyntaxExceptionTypeTest { @Test public void createWithContext() throws Exception { - final SimpleCommandExceptionType type = new SimpleCommandExceptionType("foo", "bar"); + final SimpleCommandExceptionType type = new SimpleCommandExceptionType(new LiteralMessage("error")); final StringReader reader = new StringReader("Foo bar"); reader.setCursor(5); final CommandSyntaxException exception = type.createWithContext(reader); assertThat(exception.getType(), is(type)); - assertThat(exception.getData(), is(Collections.emptyMap())); assertThat(exception.getInput(), is("Foo bar")); assertThat(exception.getCursor(), is(5)); } - @Test - public void testEquals() throws Exception { - new EqualsTester() - .addEqualityGroup(new SimpleCommandExceptionType("foo", "Hello, world!"), new SimpleCommandExceptionType("foo", "Hello, universe!")) - .addEqualityGroup(new SimpleCommandExceptionType("bar", "Hello, world!"), new SimpleCommandExceptionType("bar", "Hello, universe!")) - .testEquals(); - } @Test public void getContext_none() throws Exception { - final CommandSyntaxException exception = new CommandSyntaxException(mock(CommandExceptionType.class), Collections.emptyMap()); + final CommandSyntaxException exception = new CommandSyntaxException(mock(CommandExceptionType.class), new LiteralMessage("error")); assertThat(exception.getContext(), is(nullValue())); } @Test public void getContext_short() throws Exception { - final CommandSyntaxException exception = new CommandSyntaxException(mock(CommandExceptionType.class), Collections.emptyMap(), "Hello world!", 5); + final CommandSyntaxException exception = new CommandSyntaxException(mock(CommandExceptionType.class), new LiteralMessage("error"), "Hello world!", 5); assertThat(exception.getContext(), equalTo("Hello<--[HERE]")); } @Test public void getContext_long() throws Exception { - final CommandSyntaxException exception = new CommandSyntaxException(mock(CommandExceptionType.class), Collections.emptyMap(), "Hello world! This has an error in it. Oh dear!", 20); + final CommandSyntaxException exception = new CommandSyntaxException(mock(CommandExceptionType.class), new LiteralMessage("error"), "Hello world! This has an error in it. Oh dear!", 20); assertThat(exception.getContext(), equalTo("...d! This ha<--[HERE]")); } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java index 6a001b4..3715ba6 100644 --- a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java @@ -1,6 +1,5 @@ package com.mojang.brigadier.tree; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.testing.EqualsTester; import com.mojang.brigadier.Command; @@ -59,8 +58,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { node.parse(reader, contextBuilder); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL)); - assertThat(ex.getData(), is(ImmutableMap.of("expected", "foo"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect())); assertThat(ex.getCursor(), is(0)); } } @@ -72,8 +70,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { node.parse(reader, contextBuilder); fail(); } catch (final CommandSyntaxException ex) { - assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL)); - assertThat(ex.getData(), is(ImmutableMap.of("expected", "foo"))); + assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect())); assertThat(ex.getCursor(), is(0)); } }