Changed exceptions to Messages, to allow translations via library holders (ie minecraft game)

This commit is contained in:
Nathan Adams 2018-05-30 12:45:07 +02:00
parent 46a0d3be41
commit 631096e871
28 changed files with 444 additions and 329 deletions

View file

@ -3,7 +3,7 @@ import groovy.io.FileType
apply plugin: 'java-library' apply plugin: 'java-library'
apply plugin: 'maven' apply plugin: 'maven'
version = '0.1.25' version = '0.1.27'
group = 'com.mojang' group = 'com.mojang'
task wrapper(type: Wrapper) { task wrapper(type: Wrapper) {

View file

@ -9,9 +9,6 @@ import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.context.StringRange;
import com.mojang.brigadier.exceptions.CommandSyntaxException; 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.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder; import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
@ -30,11 +27,6 @@ import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class CommandDispatcher<S> { public class CommandDispatcher<S> {
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 String ARGUMENT_SEPARATOR = " ";
public static final char ARGUMENT_SEPARATOR_CHAR = ' '; public static final char ARGUMENT_SEPARATOR_CHAR = ' ';
private static final String USAGE_OPTIONAL_OPEN = "["; private static final String USAGE_OPTIONAL_OPEN = "[";
@ -81,9 +73,9 @@ public class CommandDispatcher<S> {
if (parse.getExceptions().size() == 1) { if (parse.getExceptions().size() == 1) {
throw parse.getExceptions().values().iterator().next(); throw parse.getExceptions().values().iterator().next();
} else if (parse.getContext().getRange().isEmpty()) { } else if (parse.getContext().getRange().isEmpty()) {
throw ERROR_UNKNOWN_COMMAND.createWithContext(parse.getReader()); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader());
} else { } else {
throw ERROR_UNKNOWN_ARGUMENT.createWithContext(parse.getReader()); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(parse.getReader());
} }
} }
@ -152,7 +144,7 @@ public class CommandDispatcher<S> {
if (!foundCommand) { if (!foundCommand) {
consumer.onCommandComplete(original, false, 0); 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; return forked ? successfulForks : result;
@ -190,11 +182,11 @@ public class CommandDispatcher<S> {
try { try {
child.parse(reader, context); child.parse(reader, context);
} catch (final RuntimeException ex) { } 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.canRead()) {
if (reader.peek() != ARGUMENT_SEPARATOR_CHAR) { 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) { } catch (final CommandSyntaxException ex) {

View file

@ -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;
}
}

View file

@ -1,26 +1,11 @@
package com.mojang.brigadier; package com.mojang.brigadier;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
public class StringReader implements ImmutableStringReader { public class StringReader implements ImmutableStringReader {
private static final char SYNTAX_ESCAPE = '\\'; private static final char SYNTAX_ESCAPE = '\\';
private static final char SYNTAX_QUOTE = '"'; 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 final String string;
private int cursor; private int cursor;
@ -112,13 +97,13 @@ public class StringReader implements ImmutableStringReader {
} }
final String number = string.substring(start, cursor); final String number = string.substring(start, cursor);
if (number.isEmpty()) { if (number.isEmpty()) {
throw ERROR_EXPECTED_INT.createWithContext(this); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt().createWithContext(this);
} }
try { try {
return Integer.parseInt(number); return Integer.parseInt(number);
} catch (final NumberFormatException ex) { } catch (final NumberFormatException ex) {
cursor = start; 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); final String number = string.substring(start, cursor);
if (number.isEmpty()) { if (number.isEmpty()) {
throw ERROR_EXPECTED_DOUBLE.createWithContext(this); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedDouble().createWithContext(this);
} }
try { try {
return Double.parseDouble(number); return Double.parseDouble(number);
} catch (final NumberFormatException ex) { } catch (final NumberFormatException ex) {
cursor = start; 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); final String number = string.substring(start, cursor);
if (number.isEmpty()) { if (number.isEmpty()) {
throw ERROR_EXPECTED_FLOAT.createWithContext(this); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedFloat().createWithContext(this);
} }
try { try {
return Float.parseFloat(number); return Float.parseFloat(number);
} catch (final NumberFormatException ex) { } catch (final NumberFormatException ex) {
cursor = start; 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()) { if (!canRead()) {
return ""; return "";
} else if (peek() != SYNTAX_QUOTE) { } else if (peek() != SYNTAX_QUOTE) {
throw ERROR_EXPECTED_START_OF_QUOTE.createWithContext(this); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote().createWithContext(this);
} }
skip(); skip();
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
@ -189,7 +174,7 @@ public class StringReader implements ImmutableStringReader {
escaped = false; escaped = false;
} else { } else {
setCursor(getCursor() - 1); 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) { } else if (c == SYNTAX_ESCAPE) {
escaped = true; 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 { public String readString() throws CommandSyntaxException {
@ -215,7 +200,7 @@ public class StringReader implements ImmutableStringReader {
final int start = cursor; final int start = cursor;
final String value = readString(); final String value = readString();
if (value.isEmpty()) { if (value.isEmpty()) {
throw ERROR_EXPECTED_BOOL.createWithContext(this); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedBool().createWithContext(this);
} }
if (value.equals("true")) { if (value.equals("true")) {
@ -224,13 +209,13 @@ public class StringReader implements ImmutableStringReader {
return false; return false;
} else { } else {
cursor = start; 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 { public void expect(final char c) throws CommandSyntaxException {
if (!canRead() || peek() != c) { 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(); skip();
} }

View file

@ -2,16 +2,12 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
public class DoubleArgumentType implements ArgumentType<Double> { public class DoubleArgumentType implements ArgumentType<Double> {
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<String> EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56"); private static final Collection<String> EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56");
private final double minimum; private final double minimum;
@ -52,11 +48,11 @@ public class DoubleArgumentType implements ArgumentType<Double> {
final double result = reader.readDouble(); final double result = reader.readDouble();
if (result < minimum) { if (result < minimum) {
reader.setCursor(start); reader.setCursor(start);
throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooLow().createWithContext(reader, result, minimum);
} }
if (result > maximum) { if (result > maximum) {
reader.setCursor(start); reader.setCursor(start);
throw ERROR_TOO_BIG.createWithContext(reader, result, maximum); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooHigh().createWithContext(reader, result, maximum);
} }
return result; return result;
} }

View file

@ -2,16 +2,12 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
public class FloatArgumentType implements ArgumentType<Float> { public class FloatArgumentType implements ArgumentType<Float> {
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<String> EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56"); private static final Collection<String> EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56");
private final float minimum; private final float minimum;
@ -52,11 +48,11 @@ public class FloatArgumentType implements ArgumentType<Float> {
final float result = reader.readFloat(); final float result = reader.readFloat();
if (result < minimum) { if (result < minimum) {
reader.setCursor(start); reader.setCursor(start);
throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooLow().createWithContext(reader, result, minimum);
} }
if (result > maximum) { if (result > maximum) {
reader.setCursor(start); reader.setCursor(start);
throw ERROR_TOO_BIG.createWithContext(reader, result, maximum); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooHigh().createWithContext(reader, result, maximum);
} }
return result; return result;
} }

View file

@ -2,16 +2,12 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
public class IntegerArgumentType implements ArgumentType<Integer> { public class IntegerArgumentType implements ArgumentType<Integer> {
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<String> EXAMPLES = Arrays.asList("0", "123", "-123"); private static final Collection<String> EXAMPLES = Arrays.asList("0", "123", "-123");
private final int minimum; private final int minimum;
@ -52,11 +48,11 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
final int result = reader.readInt(); final int result = reader.readInt();
if (result < minimum) { if (result < minimum) {
reader.setCursor(start); reader.setCursor(start);
throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooLow().createWithContext(reader, result, minimum);
} }
if (result > maximum) { if (result > maximum) {
reader.setCursor(start); reader.setCursor(start);
throw ERROR_TOO_BIG.createWithContext(reader, result, maximum); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh().createWithContext(reader, result, maximum);
} }
return result; return result;
} }

View file

@ -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();
}

View file

@ -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;
}
}

View file

@ -1,9 +1,4 @@
package com.mojang.brigadier.exceptions; package com.mojang.brigadier.exceptions;
import java.util.Map;
public interface CommandExceptionType { public interface CommandExceptionType {
String getTypeName();
String getErrorMessage(Map<String, String> data);
} }

View file

@ -1,35 +1,36 @@
package com.mojang.brigadier.exceptions; package com.mojang.brigadier.exceptions;
import java.util.Map; import com.mojang.brigadier.Message;
public class CommandSyntaxException extends Exception { public class CommandSyntaxException extends Exception {
public static final int CONTEXT_AMOUNT = 10; public static final int CONTEXT_AMOUNT = 10;
public static boolean ENABLE_COMMAND_STACK_TRACES = true; public static boolean ENABLE_COMMAND_STACK_TRACES = true;
public static BuiltInExceptionProvider BUILT_IN_EXCEPTIONS = new BuiltInExceptions();
private final CommandExceptionType type; private final CommandExceptionType type;
private final Map<String, String> data; private final Message message;
private final String input; private final String input;
private final int cursor; private final int cursor;
public CommandSyntaxException(final CommandExceptionType type, final Map<String, String> data) { public CommandSyntaxException(final CommandExceptionType type, final Message message) {
super(type.getTypeName(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES); super(message.getString(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES);
this.type = type; this.type = type;
this.data = data; this.message = message;
this.input = null; this.input = null;
this.cursor = -1; this.cursor = -1;
} }
public CommandSyntaxException(final CommandExceptionType type, final Map<String, String> data, final String input, final int cursor) { public CommandSyntaxException(final CommandExceptionType type, final Message message, final String input, final int cursor) {
super(type.getTypeName(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES); super(message.getString(), null, ENABLE_COMMAND_STACK_TRACES, ENABLE_COMMAND_STACK_TRACES);
this.type = type; this.type = type;
this.data = data; this.message = message;
this.input = input; this.input = input;
this.cursor = cursor; this.cursor = cursor;
} }
@Override @Override
public String getMessage() { public String getMessage() {
String message = type.getErrorMessage(data); String message = this.message.getString();
final String context = getContext(); final String context = getContext();
if (context != null) { if (context != null) {
message += " at position " + cursor + ": " + context; message += " at position " + cursor + ": " + context;
@ -37,6 +38,10 @@ public class CommandSyntaxException extends Exception {
return message; return message;
} }
public Message getRawMessage() {
return message;
}
public String getContext() { public String getContext() {
if (input == null || cursor < 0) { if (input == null || cursor < 0) {
return null; return null;
@ -58,10 +63,6 @@ public class CommandSyntaxException extends Exception {
return type; return type;
} }
public Map<String, String> getData() {
return data;
}
public String getInput() { public String getInput() {
return input; return input;
} }

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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<Object, Message> function;
public DynamicCommandExceptionType(final Function<Object, Message> 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());
}
}

View file

@ -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);
}
}

View file

@ -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<String, String> 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<String, String> createMap(final Object... values) {
if (values.length != keys.length) {
throw new IllegalArgumentException("Invalid values! (Expected: " + JOINER.join(keys) + ")");
}
ImmutableMap.Builder<String, String> 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;
}
}

View file

@ -1,54 +1,25 @@
package com.mojang.brigadier.exceptions; package com.mojang.brigadier.exceptions;
import com.google.common.collect.ImmutableMap;
import com.mojang.brigadier.ImmutableStringReader; import com.mojang.brigadier.ImmutableStringReader;
import com.mojang.brigadier.Message;
import java.util.Map;
public class SimpleCommandExceptionType implements CommandExceptionType { public class SimpleCommandExceptionType implements CommandExceptionType {
private final String name; private final Message message;
private final String message;
public SimpleCommandExceptionType(final String name, final String message) { public SimpleCommandExceptionType(final Message message) {
this.name = name;
this.message = message; this.message = message;
} }
@Override
public String getTypeName() {
return name;
}
@Override
public String getErrorMessage(final Map<String, String> data) {
return message;
}
public CommandSyntaxException create() { public CommandSyntaxException create() {
return new CommandSyntaxException(this, ImmutableMap.of()); return new CommandSyntaxException(this, message);
} }
public CommandSyntaxException createWithContext(final ImmutableStringReader reader) { public CommandSyntaxException createWithContext(final ImmutableStringReader reader) {
return new CommandSyntaxException(this, ImmutableMap.of(), reader.getString(), reader.getCursor()); return new CommandSyntaxException(this, message, 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();
} }
@Override @Override
public String toString() { public String toString() {
return message; return message.getString();
} }
} }

View file

@ -8,7 +8,6 @@ import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.context.StringRange;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder; import com.mojang.brigadier.suggestion.SuggestionsBuilder;
@ -18,8 +17,6 @@ import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate; import java.util.function.Predicate;
public class LiteralCommandNode<S> extends CommandNode<S> { public class LiteralCommandNode<S> extends CommandNode<S> {
public static final ParameterizedCommandExceptionType ERROR_INCORRECT_LITERAL = new ParameterizedCommandExceptionType("argument.literal.incorrect", "Expected literal ${expected}", "expected");
private final String literal; private final String literal;
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) { public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
@ -45,7 +42,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
return; return;
} }
throw ERROR_INCORRECT_LITERAL.createWithContext(reader, literal); throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect().createWithContext(reader, literal);
} }
private int parse(final StringReader reader) { private int parse(final StringReader reader) {

View file

@ -11,8 +11,6 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument; import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
@ -74,8 +72,7 @@ public class CommandDispatcherTest {
subject.execute("foo", source); subject.execute("foo", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -88,8 +85,7 @@ public class CommandDispatcherTest {
subject.execute("foo", source); subject.execute("foo", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -102,8 +98,7 @@ public class CommandDispatcherTest {
subject.execute("", source); subject.execute("", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -116,8 +111,7 @@ public class CommandDispatcherTest {
subject.execute("foo bar", source); subject.execute("foo bar", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument()));
assertThat(ex.getData(), is(Collections.emptyMap()));
assertThat(ex.getCursor(), is(4)); assertThat(ex.getCursor(), is(4));
} }
} }
@ -130,8 +124,7 @@ public class CommandDispatcherTest {
subject.execute("foo baz", source); subject.execute("foo baz", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument()));
assertThat(ex.getData(), is(Collections.emptyMap()));
assertThat(ex.getCursor(), is(4)); assertThat(ex.getCursor(), is(4));
} }
} }
@ -148,8 +141,7 @@ public class CommandDispatcherTest {
subject.execute("foo unknown", source); subject.execute("foo unknown", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument()));
assertThat(ex.getData(), is(Collections.emptyMap()));
assertThat(ex.getCursor(), is(4)); assertThat(ex.getCursor(), is(4));
} }
} }
@ -310,8 +302,7 @@ public class CommandDispatcherTest {
subject.execute("foo 5", source); subject.execute("foo 5", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
assertThat(ex.getData(), is(Collections.emptyMap()));
assertThat(ex.getCursor(), is(5)); assertThat(ex.getCursor(), is(5));
} }
} }
@ -335,8 +326,7 @@ public class CommandDispatcherTest {
subject.execute("foo$", source); subject.execute("foo$", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
assertThat(ex.getData(), is(Collections.emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -351,8 +341,7 @@ public class CommandDispatcherTest {
subject.execute("foo bar", source); subject.execute("foo bar", source);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt()));
assertThat(ex.getData(), is(Collections.emptyMap()));
assertThat(ex.getCursor(), is(4)); assertThat(ex.getCursor(), is(4));
} }
} }

View file

@ -1,11 +1,8 @@
package com.mojang.brigadier; package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import org.junit.Test; import org.junit.Test;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -217,8 +214,7 @@ public class StringReaderTest {
try { try {
new StringReader("hello world\"").readQuotedString(); new StringReader("hello world\"").readQuotedString();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_START_OF_QUOTE)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote()));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -228,8 +224,7 @@ public class StringReaderTest {
try { try {
new StringReader("\"hello world").readQuotedString(); new StringReader("\"hello world").readQuotedString();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_END_OF_QUOTE)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedEndOfQuote()));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
assertThat(ex.getCursor(), is(12)); assertThat(ex.getCursor(), is(12));
} }
} }
@ -239,8 +234,7 @@ public class StringReaderTest {
try { try {
new StringReader("\"hello\\nworld\"").readQuotedString(); new StringReader("\"hello\\nworld\"").readQuotedString();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_INVALID_ESCAPE)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidEscape()));
assertThat(ex.getData(), equalTo(ImmutableMap.of("character", "n")));
assertThat(ex.getCursor(), is(7)); assertThat(ex.getCursor(), is(7));
} }
} }
@ -266,8 +260,7 @@ public class StringReaderTest {
try { try {
new StringReader("12.34").readInt(); new StringReader("12.34").readInt();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_INVALID_INT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidInt()));
assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "12.34")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -277,8 +270,7 @@ public class StringReaderTest {
try { try {
new StringReader("").readInt(); new StringReader("").readInt();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt()));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -328,8 +320,7 @@ public class StringReaderTest {
try { try {
new StringReader("12.34.56").readDouble(); new StringReader("12.34.56").readDouble();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_INVALID_DOUBLE)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidDouble()));
assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "12.34.56")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -339,8 +330,7 @@ public class StringReaderTest {
try { try {
new StringReader("").readDouble(); new StringReader("").readDouble();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_DOUBLE)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedDouble()));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -390,8 +380,7 @@ public class StringReaderTest {
try { try {
new StringReader("12.34.56").readFloat(); new StringReader("12.34.56").readFloat();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_INVALID_FLOAT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidFloat()));
assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "12.34.56")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -401,8 +390,7 @@ public class StringReaderTest {
try { try {
new StringReader("").readFloat(); new StringReader("").readFloat();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_FLOAT)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedFloat()));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -437,8 +425,7 @@ public class StringReaderTest {
reader.expect('a'); reader.expect('a');
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_SYMBOL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol()));
assertThat(ex.getData(), equalTo(ImmutableMap.of("symbol", "a")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -450,8 +437,7 @@ public class StringReaderTest {
reader.expect('a'); reader.expect('a');
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_SYMBOL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol()));
assertThat(ex.getData(), equalTo(ImmutableMap.of("symbol", "a")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -470,8 +456,7 @@ public class StringReaderTest {
reader.readBoolean(); reader.readBoolean();
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_INVALID_BOOL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidBool()));
assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "tuesday")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -483,8 +468,7 @@ public class StringReaderTest {
reader.readBoolean(); reader.readBoolean();
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_BOOL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedBool()));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }

View file

@ -1,6 +1,5 @@
package com.mojang.brigadier.arguments; package com.mojang.brigadier.arguments;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
@ -12,7 +11,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static com.mojang.brigadier.arguments.DoubleArgumentType.doubleArg; 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.hasToString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -43,8 +41,7 @@ public class DoubleArgumentTypeTest {
doubleArg(0, 100).parse(reader); doubleArg(0, 100).parse(reader);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(DoubleArgumentType.ERROR_TOO_SMALL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooLow()));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "-5.0", "minimum", "0.0")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -56,8 +53,7 @@ public class DoubleArgumentTypeTest {
doubleArg(-100, 0).parse(reader); doubleArg(-100, 0).parse(reader);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(DoubleArgumentType.ERROR_TOO_BIG)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooHigh()));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "5.0", "maximum", "0.0")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }

View file

@ -1,6 +1,5 @@
package com.mojang.brigadier.arguments; package com.mojang.brigadier.arguments;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
@ -12,7 +11,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static com.mojang.brigadier.arguments.FloatArgumentType.floatArg; 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.hasToString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -43,8 +41,7 @@ public class FloatArgumentTypeTest {
floatArg(0, 100).parse(reader); floatArg(0, 100).parse(reader);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(FloatArgumentType.ERROR_TOO_SMALL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooLow()));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "-5.0", "minimum", "0.0")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -56,8 +53,7 @@ public class FloatArgumentTypeTest {
floatArg(-100, 0).parse(reader); floatArg(-100, 0).parse(reader);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(FloatArgumentType.ERROR_TOO_BIG)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooHigh()));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "5.0", "maximum", "0.0")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }

View file

@ -1,6 +1,5 @@
package com.mojang.brigadier.arguments; package com.mojang.brigadier.arguments;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
@ -12,7 +11,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; 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.hasToString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -43,8 +41,7 @@ public class IntegerArgumentTypeTest {
integer(0, 100).parse(reader); integer(0, 100).parse(reader);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooLow()));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "-5", "minimum", "0")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -56,8 +53,7 @@ public class IntegerArgumentTypeTest {
integer(-100, 0).parse(reader); integer(-100, 0).parse(reader);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh()));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "5", "maximum", "0")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }

View file

@ -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));
}
}

View file

@ -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.<String, Object>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();
}
}

View file

@ -1,11 +1,9 @@
package com.mojang.brigadier.exceptions; package com.mojang.brigadier.exceptions;
import com.google.common.testing.EqualsTester; import com.mojang.brigadier.LiteralMessage;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import org.junit.Test; import org.junit.Test;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
@ -16,39 +14,31 @@ import static org.mockito.Mockito.mock;
public class SimpleCommandSyntaxExceptionTypeTest { public class SimpleCommandSyntaxExceptionTypeTest {
@Test @Test
public void createWithContext() throws Exception { 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"); final StringReader reader = new StringReader("Foo bar");
reader.setCursor(5); reader.setCursor(5);
final CommandSyntaxException exception = type.createWithContext(reader); final CommandSyntaxException exception = type.createWithContext(reader);
assertThat(exception.getType(), is(type)); assertThat(exception.getType(), is(type));
assertThat(exception.getData(), is(Collections.emptyMap()));
assertThat(exception.getInput(), is("Foo bar")); assertThat(exception.getInput(), is("Foo bar"));
assertThat(exception.getCursor(), is(5)); 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 @Test
public void getContext_none() throws Exception { 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())); assertThat(exception.getContext(), is(nullValue()));
} }
@Test @Test
public void getContext_short() throws Exception { 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]")); assertThat(exception.getContext(), equalTo("Hello<--[HERE]"));
} }
@Test @Test
public void getContext_long() throws Exception { 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]")); assertThat(exception.getContext(), equalTo("...d! This ha<--[HERE]"));
} }
} }

View file

@ -1,6 +1,5 @@
package com.mojang.brigadier.tree; package com.mojang.brigadier.tree;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
@ -59,8 +58,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
node.parse(reader, contextBuilder); node.parse(reader, contextBuilder);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect()));
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }
@ -72,8 +70,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
node.parse(reader, contextBuilder); node.parse(reader, contextBuilder);
fail(); fail();
} catch (final CommandSyntaxException ex) { } catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL)); assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect()));
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo")));
assertThat(ex.getCursor(), is(0)); assertThat(ex.getCursor(), is(0));
} }
} }