Code cleanup
This commit is contained in:
@@ -36,22 +36,22 @@ public class CommandDispatcher<S> {
|
||||
private final RootCommandNode<S> root = new RootCommandNode<>();
|
||||
private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() {
|
||||
@Override
|
||||
public boolean test(CommandNode<S> input) {
|
||||
public boolean test(final CommandNode<S> input) {
|
||||
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
|
||||
}
|
||||
};
|
||||
|
||||
public void register(LiteralArgumentBuilder<S> command) {
|
||||
public void register(final LiteralArgumentBuilder<S> command) {
|
||||
final LiteralCommandNode<S> build = command.build();
|
||||
root.addChild(build);
|
||||
}
|
||||
|
||||
public int execute(String input, S source) throws CommandException {
|
||||
public int execute(final String input, final S source) throws CommandException {
|
||||
final ParseResults<S> parse = parse(input, source);
|
||||
return execute(parse);
|
||||
}
|
||||
|
||||
public int execute(ParseResults<S> parse) throws CommandException {
|
||||
public int execute(final ParseResults<S> parse) throws CommandException {
|
||||
if (parse.getRemaining().length() > 0) {
|
||||
if (parse.getExceptions().size() == 1) {
|
||||
throw parse.getExceptions().values().iterator().next();
|
||||
@@ -61,32 +61,32 @@ public class CommandDispatcher<S> {
|
||||
throw ERROR_UNKNOWN_ARGUMENT.create(parse.getRemaining());
|
||||
}
|
||||
}
|
||||
CommandContext<S> context = parse.getContext().build();
|
||||
Command<S> command = context.getCommand();
|
||||
final CommandContext<S> context = parse.getContext().build();
|
||||
final Command<S> command = context.getCommand();
|
||||
if (command == null) {
|
||||
throw ERROR_UNKNOWN_COMMAND.create();
|
||||
}
|
||||
return command.run(context);
|
||||
}
|
||||
|
||||
public ParseResults<S> parse(String command, S source) throws CommandException {
|
||||
StringReader reader = new StringReader(command);
|
||||
public ParseResults<S> parse(final String command, final S source) throws CommandException {
|
||||
final StringReader reader = new StringReader(command);
|
||||
return parseNodes(root, reader, new CommandContextBuilder<>(this, source));
|
||||
}
|
||||
|
||||
private ParseResults<S> parseNodes(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
final S source = contextBuilder.getSource();
|
||||
Map<CommandNode<S>, CommandException> errors = Maps.newHashMap();
|
||||
final Map<CommandNode<S>, CommandException> errors = Maps.newHashMap();
|
||||
|
||||
for (CommandNode<S> child : node.getChildren()) {
|
||||
for (final CommandNode<S> child : node.getChildren()) {
|
||||
if (!child.canUse(source)) {
|
||||
continue;
|
||||
}
|
||||
CommandContextBuilder<S> context = contextBuilder.copy();
|
||||
int cursor = reader.getCursor();
|
||||
final CommandContextBuilder<S> context = contextBuilder.copy();
|
||||
final int cursor = reader.getCursor();
|
||||
try {
|
||||
child.parse(reader, context);
|
||||
} catch (CommandException ex) {
|
||||
} catch (final CommandException ex) {
|
||||
errors.put(child, ex);
|
||||
reader.setCursor(cursor);
|
||||
continue;
|
||||
@@ -107,13 +107,13 @@ public class CommandDispatcher<S> {
|
||||
return new ParseResults<>(contextBuilder, reader.getRemaining(), errors);
|
||||
}
|
||||
|
||||
public String[] getAllUsage(CommandNode<S> node, S source) {
|
||||
public String[] getAllUsage(final CommandNode<S> node, final S source) {
|
||||
final ArrayList<String> result = Lists.newArrayList();
|
||||
getAllUsage(node, source, result, "");
|
||||
getAllUsage(node, source, result, "");
|
||||
return result.toArray(new String[result.size()]);
|
||||
}
|
||||
|
||||
private void getAllUsage(CommandNode<S> node, S source, ArrayList<String> result, String prefix) {
|
||||
private void getAllUsage(final CommandNode<S> node, final S source, final ArrayList<String> result, final String prefix) {
|
||||
if (!node.canUse(source)) {
|
||||
return;
|
||||
}
|
||||
@@ -129,12 +129,12 @@ public class CommandDispatcher<S> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<CommandNode<S>, String> getSmartUsage(CommandNode<S> node, S source) {
|
||||
Map<CommandNode<S>, String> result = Maps.newLinkedHashMap();
|
||||
public Map<CommandNode<S>, String> getSmartUsage(final CommandNode<S> node, final S source) {
|
||||
final Map<CommandNode<S>, String> result = Maps.newLinkedHashMap();
|
||||
|
||||
final boolean optional = node.getCommand() != null;
|
||||
for (CommandNode<S> child : node.getChildren()) {
|
||||
String usage = getSmartUsage(child, source, optional, false);
|
||||
for (final CommandNode<S> child : node.getChildren()) {
|
||||
final String usage = getSmartUsage(child, source, optional, false);
|
||||
if (usage != null) {
|
||||
result.put(child, usage);
|
||||
}
|
||||
@@ -142,15 +142,15 @@ public class CommandDispatcher<S> {
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getSmartUsage(CommandNode<S> node, S source, boolean optional, boolean deep) {
|
||||
private String getSmartUsage(final CommandNode<S> node, final S source, final boolean optional, final boolean deep) {
|
||||
if (!node.canUse(source)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText();
|
||||
boolean childOptional = node.getCommand() != null;
|
||||
String open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN;
|
||||
String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
|
||||
final String self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText();
|
||||
final boolean childOptional = node.getCommand() != null;
|
||||
final String open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN;
|
||||
final String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
|
||||
|
||||
if (!deep) {
|
||||
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
|
||||
@@ -160,7 +160,7 @@ public class CommandDispatcher<S> {
|
||||
return self + ARGUMENT_SEPARATOR + usage;
|
||||
}
|
||||
} else if (children.size() > 1) {
|
||||
Set<String> childUsage = Sets.newLinkedHashSet();
|
||||
final Set<String> childUsage = Sets.newLinkedHashSet();
|
||||
for (final CommandNode<S> child : children) {
|
||||
final String usage = getSmartUsage(child, source, childOptional, true);
|
||||
if (usage != null) {
|
||||
@@ -171,7 +171,7 @@ public class CommandDispatcher<S> {
|
||||
final String usage = childUsage.iterator().next();
|
||||
return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage);
|
||||
} else if (childUsage.size() > 1) {
|
||||
StringBuilder builder = new StringBuilder(open);
|
||||
final StringBuilder builder = new StringBuilder(open);
|
||||
int count = 0;
|
||||
for (final CommandNode<S> child : children) {
|
||||
if (count > 0) {
|
||||
@@ -191,14 +191,14 @@ public class CommandDispatcher<S> {
|
||||
return self;
|
||||
}
|
||||
|
||||
private Set<String> findSuggestions(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder, Set<String> result) {
|
||||
private Set<String> findSuggestions(final CommandNode<S> node, final StringReader reader, final CommandContextBuilder<S> contextBuilder, final Set<String> result) {
|
||||
final S source = contextBuilder.getSource();
|
||||
for (CommandNode<S> child : node.getChildren()) {
|
||||
for (final CommandNode<S> child : node.getChildren()) {
|
||||
if (!child.canUse(source)) {
|
||||
continue;
|
||||
}
|
||||
CommandContextBuilder<S> context = contextBuilder.copy();
|
||||
int cursor = reader.getCursor();
|
||||
final CommandContextBuilder<S> context = contextBuilder.copy();
|
||||
final int cursor = reader.getCursor();
|
||||
try {
|
||||
child.parse(reader, context);
|
||||
if (reader.canRead()) {
|
||||
@@ -210,7 +210,7 @@ public class CommandDispatcher<S> {
|
||||
reader.setCursor(cursor);
|
||||
child.listSuggestions(reader.getRemaining(), result, context);
|
||||
}
|
||||
} catch (CommandException e) {
|
||||
} catch (final CommandException e) {
|
||||
reader.setCursor(cursor);
|
||||
child.listSuggestions(reader.getRemaining(), result, context);
|
||||
}
|
||||
@@ -219,8 +219,8 @@ public class CommandDispatcher<S> {
|
||||
return result;
|
||||
}
|
||||
|
||||
public String[] getCompletionSuggestions(String command, S source) {
|
||||
StringReader reader = new StringReader(command);
|
||||
public String[] getCompletionSuggestions(final String command, final S source) {
|
||||
final StringReader reader = new StringReader(command);
|
||||
final Set<String> nodes = findSuggestions(root, reader, new CommandContextBuilder<>(this, source), Sets.newLinkedHashSet());
|
||||
|
||||
return nodes.toArray(new String[nodes.size()]);
|
||||
|
||||
@@ -12,13 +12,13 @@ public class ParseResults<S> {
|
||||
private final String remaining;
|
||||
private final Map<CommandNode<S>, CommandException> exceptions;
|
||||
|
||||
public ParseResults(CommandContextBuilder<S> context, String remaining, Map<CommandNode<S>, CommandException> exceptions) {
|
||||
public ParseResults(final CommandContextBuilder<S> context, final String remaining, final Map<CommandNode<S>, CommandException> exceptions) {
|
||||
this.context = context;
|
||||
this.remaining = remaining;
|
||||
this.exceptions = exceptions;
|
||||
}
|
||||
|
||||
public ParseResults(CommandContextBuilder<S> context) {
|
||||
public ParseResults(final CommandContextBuilder<S> context) {
|
||||
this(context, "", Collections.emptyMap());
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public class StringReader {
|
||||
private final String string;
|
||||
private int cursor;
|
||||
|
||||
public StringReader(String string) {
|
||||
public StringReader(final String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public class StringReader {
|
||||
return string;
|
||||
}
|
||||
|
||||
public void setCursor(int cursor) {
|
||||
public void setCursor(final int cursor) {
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ public class StringReader {
|
||||
|| c == '.' || c == '+';
|
||||
}
|
||||
|
||||
public String readUnquotedString() throws CommandException {
|
||||
public String readUnquotedString() {
|
||||
final int start = cursor;
|
||||
while (canRead() && isAllowedInUnquotedString(peek())) {
|
||||
skip();
|
||||
@@ -163,7 +163,7 @@ public class StringReader {
|
||||
}
|
||||
|
||||
public boolean readBoolean() throws CommandException {
|
||||
String value = readString();
|
||||
final String value = readString();
|
||||
if (value.equals("true")) {
|
||||
return true;
|
||||
} else if (value.equals("false")) {
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.mojang.brigadier.arguments;
|
||||
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
import com.mojang.brigadier.context.ParsedArgument;
|
||||
import com.mojang.brigadier.exceptions.CommandException;
|
||||
|
||||
import java.util.Set;
|
||||
@@ -10,7 +9,8 @@ import java.util.Set;
|
||||
public interface ArgumentType<T> {
|
||||
<S> T parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException;
|
||||
|
||||
default <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {}
|
||||
default <S> void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
|
||||
}
|
||||
|
||||
default String getUsageSuffix() {
|
||||
return null;
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package com.mojang.brigadier.arguments;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
import com.mojang.brigadier.context.ParsedArgument;
|
||||
import com.mojang.brigadier.exceptions.CommandException;
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||
|
||||
public class BoolArgumentType implements ArgumentType<Boolean> {
|
||||
public static final SimpleCommandExceptionType ERROR_INVALID = new SimpleCommandExceptionType("argument.bool.invalid", "Value must be true or false");
|
||||
|
||||
private BoolArgumentType() {
|
||||
}
|
||||
|
||||
@@ -18,12 +14,12 @@ public class BoolArgumentType implements ArgumentType<Boolean> {
|
||||
return new BoolArgumentType();
|
||||
}
|
||||
|
||||
public static boolean getBool(CommandContext<?> context, String name) {
|
||||
public static boolean getBool(final CommandContext<?> context, final String name) {
|
||||
return context.getArgument(name, Boolean.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> Boolean parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
public <S> Boolean parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
return reader.readBoolean();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package com.mojang.brigadier.arguments;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
import com.mojang.brigadier.context.ParsedArgument;
|
||||
import com.mojang.brigadier.exceptions.CommandException;
|
||||
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
|
||||
|
||||
@@ -19,7 +17,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
||||
private final int maximum;
|
||||
private final String suffix;
|
||||
|
||||
private IntegerArgumentType(int minimum, int maximum, String suffix) {
|
||||
private IntegerArgumentType(final int minimum, final int maximum, final String suffix) {
|
||||
this.minimum = minimum;
|
||||
this.maximum = maximum;
|
||||
this.suffix = suffix;
|
||||
@@ -29,25 +27,25 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
||||
return integer(Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
public static IntegerArgumentType integer(int min) {
|
||||
public static IntegerArgumentType integer(final int min) {
|
||||
return integer(min, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
public static IntegerArgumentType integer(int min, int max) {
|
||||
public static IntegerArgumentType integer(final int min, final int max) {
|
||||
return integer(min, max, "");
|
||||
}
|
||||
|
||||
public static IntegerArgumentType integer(int min, int max, String suffix) {
|
||||
public static IntegerArgumentType integer(final int min, final int max, final String suffix) {
|
||||
return new IntegerArgumentType(min, max, suffix);
|
||||
}
|
||||
|
||||
public static int getInteger(CommandContext<?> context, String name) {
|
||||
public static int getInteger(final CommandContext<?> context, final String name) {
|
||||
return context.getArgument(name, int.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> Integer parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
int result = reader.readInt();
|
||||
public <S> Integer parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
final int result = reader.readInt();
|
||||
for (int i = 0; i < suffix.length(); i++) {
|
||||
if (reader.canRead() && reader.peek() == suffix.charAt(i)) {
|
||||
reader.skip();
|
||||
@@ -65,11 +63,11 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof IntegerArgumentType)) return false;
|
||||
|
||||
IntegerArgumentType that = (IntegerArgumentType) o;
|
||||
final IntegerArgumentType that = (IntegerArgumentType) o;
|
||||
return maximum == that.maximum && minimum == that.minimum && Objects.equals(suffix, that.suffix);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,15 +4,12 @@ import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
import com.mojang.brigadier.context.ParsedArgument;
|
||||
import com.mojang.brigadier.exceptions.CommandException;
|
||||
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||
|
||||
public class StringArgumentType implements ArgumentType<String> {
|
||||
private final StringType type;
|
||||
|
||||
private StringArgumentType(StringType type) {
|
||||
private StringArgumentType(final StringType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -28,14 +25,14 @@ public class StringArgumentType implements ArgumentType<String> {
|
||||
return new StringArgumentType(StringType.GREEDY_PHRASE);
|
||||
}
|
||||
|
||||
public static String getString(CommandContext<?> context, String name) {
|
||||
public static String getString(final CommandContext<?> context, final String name) {
|
||||
return context.getArgument(name, String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> String parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
public <S> String parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
if (type == StringType.GREEDY_PHRASE) {
|
||||
String text = reader.getRemaining();
|
||||
final String text = reader.getRemaining();
|
||||
reader.setCursor(reader.getTotalLength());
|
||||
return text;
|
||||
} else if (type == StringType.SINGLE_WORD) {
|
||||
@@ -50,15 +47,15 @@ public class StringArgumentType implements ArgumentType<String> {
|
||||
return "string()";
|
||||
}
|
||||
|
||||
public static String escapeIfRequired(String input) {
|
||||
public static String escapeIfRequired(final String input) {
|
||||
if (input.contains("\\") || input.contains("\"") || input.contains(CommandDispatcher.ARGUMENT_SEPARATOR)) {
|
||||
return escape(input);
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
private static String escape(String input) {
|
||||
StringBuilder result = new StringBuilder("\"");
|
||||
private static String escape(final String input) {
|
||||
final StringBuilder result = new StringBuilder("\"");
|
||||
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
final char c = input.charAt(i);
|
||||
|
||||
@@ -14,7 +14,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
|
||||
|
||||
protected abstract T getThis();
|
||||
|
||||
public T then(ArgumentBuilder<S, ?> argument) {
|
||||
public T then(final ArgumentBuilder<S, ?> argument) {
|
||||
arguments.addChild(argument.build());
|
||||
return getThis();
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
|
||||
return arguments.getChildren();
|
||||
}
|
||||
|
||||
public T executes(Command<S> command) {
|
||||
public T executes(final Command<S> command) {
|
||||
this.command = command;
|
||||
return getThis();
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
|
||||
return command;
|
||||
}
|
||||
|
||||
public T requires(Predicate<S> requirement) {
|
||||
public T requires(final Predicate<S> requirement) {
|
||||
this.requirement = requirement;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumentBuilder<S>> {
|
||||
private final String literal;
|
||||
|
||||
protected LiteralArgumentBuilder(String literal) {
|
||||
protected LiteralArgumentBuilder(final String literal) {
|
||||
this.literal = literal;
|
||||
}
|
||||
|
||||
public static <S> LiteralArgumentBuilder<S> literal(String name) {
|
||||
public static <S> LiteralArgumentBuilder<S> literal(final String name) {
|
||||
return new LiteralArgumentBuilder<>(name);
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@ public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumen
|
||||
|
||||
@Override
|
||||
public LiteralCommandNode<S> build() {
|
||||
LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement());
|
||||
final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement());
|
||||
|
||||
for (CommandNode<S> argument : getArguments()) {
|
||||
for (final CommandNode<S> argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,12 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
|
||||
private final String name;
|
||||
private final ArgumentType<T> type;
|
||||
|
||||
private RequiredArgumentBuilder(String name, ArgumentType<T> type) {
|
||||
private RequiredArgumentBuilder(final String name, final ArgumentType<T> type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public static <S, T> RequiredArgumentBuilder<S, T> argument(String name, ArgumentType<T> type) {
|
||||
public static <S, T> RequiredArgumentBuilder<S, T> argument(final String name, final ArgumentType<T> type) {
|
||||
return new RequiredArgumentBuilder<>(name, type);
|
||||
}
|
||||
|
||||
@@ -31,9 +31,9 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
|
||||
}
|
||||
|
||||
public ArgumentCommandNode<S, T> build() {
|
||||
ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement());
|
||||
final ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement());
|
||||
|
||||
for (CommandNode<S> argument : getArguments()) {
|
||||
for (final CommandNode<S> argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ public class CommandContext<S> {
|
||||
private final Map<CommandNode<S>, String> nodes;
|
||||
private final String input;
|
||||
|
||||
public CommandContext(S source, Map<String, ParsedArgument<S, ?>> arguments, Command<S> command, Map<CommandNode<S>, String> nodes, String input) {
|
||||
public CommandContext(final S source, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final Map<CommandNode<S>, String> nodes, final String input) {
|
||||
this.source = source;
|
||||
this.arguments = arguments;
|
||||
this.command = command;
|
||||
@@ -32,8 +32,8 @@ public class CommandContext<S> {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> V getArgument(String name, Class<V> clazz) {
|
||||
ParsedArgument<S, ?> argument = arguments.get(name);
|
||||
public <V> V getArgument(final String name, final Class<V> clazz) {
|
||||
final ParsedArgument<S, ?> argument = arguments.get(name);
|
||||
|
||||
if (argument == null) {
|
||||
throw new IllegalArgumentException("No such argument '" + name + "' exists on this command");
|
||||
@@ -48,11 +48,11 @@ public class CommandContext<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CommandContext)) return false;
|
||||
|
||||
CommandContext that = (CommandContext) o;
|
||||
final CommandContext that = (CommandContext) o;
|
||||
|
||||
if (!arguments.equals(that.arguments)) return false;
|
||||
if (!Iterables.elementsEqual(nodes.entrySet(), that.nodes.entrySet())) return false;
|
||||
@@ -80,7 +80,7 @@ public class CommandContext<S> {
|
||||
}
|
||||
|
||||
public CommandContext<S> copy() {
|
||||
Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap();
|
||||
final Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap();
|
||||
this.arguments.forEach((k, v) -> arguments.put(k, v.copy()));
|
||||
return new CommandContext<>(source, arguments, command, nodes, input);
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ public class CommandContextBuilder<S> {
|
||||
private S source;
|
||||
private Command<S> command;
|
||||
|
||||
public CommandContextBuilder(CommandDispatcher<S> dispatcher, S source) {
|
||||
public CommandContextBuilder(final CommandDispatcher<S> dispatcher, final S source) {
|
||||
this.dispatcher = dispatcher;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<S> withSource(S source) {
|
||||
public CommandContextBuilder<S> withSource(final S source) {
|
||||
this.source = source;
|
||||
return this;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public class CommandContextBuilder<S> {
|
||||
return source;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<S> withArgument(String name, ParsedArgument<S, ?> argument) {
|
||||
public CommandContextBuilder<S> withArgument(final String name, final ParsedArgument<S, ?> argument) {
|
||||
this.arguments.put(name, argument);
|
||||
return this;
|
||||
}
|
||||
@@ -38,12 +38,12 @@ public class CommandContextBuilder<S> {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<S> withCommand(Command<S> command) {
|
||||
public CommandContextBuilder<S> withCommand(final Command<S> command) {
|
||||
this.command = command;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<S> withNode(CommandNode<S> node, String raw) {
|
||||
public CommandContextBuilder<S> withNode(final CommandNode<S> node, final String raw) {
|
||||
if (!nodes.isEmpty()) {
|
||||
input.append(CommandDispatcher.ARGUMENT_SEPARATOR);
|
||||
}
|
||||
@@ -53,7 +53,7 @@ public class CommandContextBuilder<S> {
|
||||
}
|
||||
|
||||
public CommandContextBuilder<S> copy() {
|
||||
CommandContextBuilder<S> copy = new CommandContextBuilder<>(dispatcher, source);
|
||||
final CommandContextBuilder<S> copy = new CommandContextBuilder<>(dispatcher, source);
|
||||
copy.command = this.command;
|
||||
arguments.forEach((k, v) -> copy.arguments.put(k, v.copy()));
|
||||
copy.nodes.putAll(this.nodes);
|
||||
|
||||
@@ -4,7 +4,7 @@ public class ParsedArgument<S, T> {
|
||||
private final String raw;
|
||||
private final T result;
|
||||
|
||||
public ParsedArgument(String raw, T result) {
|
||||
public ParsedArgument(final String raw, final T result) {
|
||||
this.raw = raw;
|
||||
this.result = result;
|
||||
}
|
||||
@@ -18,11 +18,11 @@ public class ParsedArgument<S, T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ParsedArgument)) return false;
|
||||
|
||||
ParsedArgument that = (ParsedArgument) o;
|
||||
final ParsedArgument that = (ParsedArgument) o;
|
||||
|
||||
if (!raw.equals(that.raw)) return false;
|
||||
if (!result.equals(that.result)) return false;
|
||||
|
||||
@@ -6,7 +6,7 @@ public class CommandException extends Exception {
|
||||
private final CommandExceptionType type;
|
||||
private final Map<String, Object> data;
|
||||
|
||||
public CommandException(CommandExceptionType type, Map<String, Object> data) {
|
||||
public CommandException(final CommandExceptionType type, final Map<String, Object> data) {
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@@ -2,5 +2,6 @@ package com.mojang.brigadier.exceptions;
|
||||
|
||||
public interface CommandExceptionType {
|
||||
String getTypeName();
|
||||
|
||||
String getErrorMessage(CommandException exception);
|
||||
}
|
||||
|
||||
+6
-6
@@ -7,14 +7,14 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ParameterizedCommandExceptionType implements CommandExceptionType {
|
||||
private static Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)}");
|
||||
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(String name, String message, String... keys) {
|
||||
public ParameterizedCommandExceptionType(final String name, final String message, final String... keys) {
|
||||
this.name = name;
|
||||
this.message = message;
|
||||
this.keys = keys;
|
||||
@@ -26,7 +26,7 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrorMessage(CommandException exception) {
|
||||
public String getErrorMessage(final CommandException exception) {
|
||||
final Matcher matcher = PATTERN.matcher(message);
|
||||
final StringBuffer result = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
@@ -36,7 +36,7 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public CommandException create(Object... values) {
|
||||
public CommandException create(final Object... values) {
|
||||
if (values.length != keys.length) {
|
||||
throw new IllegalArgumentException("Invalid values! (Expected: " + JOINER.join(keys) + ")");
|
||||
}
|
||||
@@ -51,11 +51,11 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CommandExceptionType)) return false;
|
||||
|
||||
CommandExceptionType that = (CommandExceptionType) o;
|
||||
final CommandExceptionType that = (CommandExceptionType) o;
|
||||
|
||||
return getTypeName().equals(that.getTypeName());
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
|
||||
private final String name;
|
||||
private final String message;
|
||||
|
||||
public SimpleCommandExceptionType(String name, String message) {
|
||||
public SimpleCommandExceptionType(final String name, final String message) {
|
||||
this.name = name;
|
||||
this.message = message;
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrorMessage(CommandException exception) {
|
||||
public String getErrorMessage(final CommandException exception) {
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CommandExceptionType)) return false;
|
||||
|
||||
CommandExceptionType that = (CommandExceptionType) o;
|
||||
final CommandExceptionType that = (CommandExceptionType) o;
|
||||
|
||||
return getTypeName().equals(that.getTypeName());
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
private final String name;
|
||||
private final ArgumentType<T> type;
|
||||
|
||||
public ArgumentCommandNode(String name, ArgumentType<T> type, Command<S> command, Predicate<S> requirement) {
|
||||
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement) {
|
||||
super(command, requirement);
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
@@ -51,17 +51,17 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
int start = reader.getCursor();
|
||||
T result = type.parse(reader, contextBuilder);
|
||||
ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result);
|
||||
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
final int start = reader.getCursor();
|
||||
final T result = type.parse(reader, contextBuilder);
|
||||
final ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result);
|
||||
|
||||
contextBuilder.withArgument(name, parsed);
|
||||
contextBuilder.withNode(this, parsed.getRaw());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
|
||||
public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
|
||||
type.listSuggestions(command, output, contextBuilder);
|
||||
}
|
||||
|
||||
@@ -76,11 +76,11 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ArgumentCommandNode)) return false;
|
||||
|
||||
ArgumentCommandNode that = (ArgumentCommandNode) o;
|
||||
final ArgumentCommandNode that = (ArgumentCommandNode) o;
|
||||
|
||||
if (!name.equals(that.name)) return false;
|
||||
if (!type.equals(that.type)) return false;
|
||||
|
||||
@@ -17,10 +17,10 @@ import java.util.stream.Collectors;
|
||||
|
||||
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
private Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap();
|
||||
private final Predicate<S> requirement;
|
||||
private Command<S> command;
|
||||
private Predicate<S> requirement;
|
||||
|
||||
protected CommandNode(Command<S> command, Predicate<S> requirement) {
|
||||
protected CommandNode(final Command<S> command, final Predicate<S> requirement) {
|
||||
this.command = command;
|
||||
this.requirement = requirement;
|
||||
}
|
||||
@@ -33,18 +33,18 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
return children.values();
|
||||
}
|
||||
|
||||
public boolean canUse(S source) {
|
||||
public boolean canUse(final S source) {
|
||||
return requirement.test(source);
|
||||
}
|
||||
|
||||
public void addChild(CommandNode<S> node) {
|
||||
CommandNode<S> child = children.get(node.getMergeKey());
|
||||
public void addChild(final CommandNode<S> node) {
|
||||
final CommandNode<S> child = children.get(node.getMergeKey());
|
||||
if (child != null) {
|
||||
// We've found something to merge onto
|
||||
if (node.getCommand() != null) {
|
||||
child.command = node.getCommand();
|
||||
}
|
||||
for (CommandNode<S> grandchild : node.getChildren()) {
|
||||
for (final CommandNode<S> grandchild : node.getChildren()) {
|
||||
child.addChild(grandchild);
|
||||
}
|
||||
} else {
|
||||
@@ -55,11 +55,11 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CommandNode)) return false;
|
||||
|
||||
CommandNode<S> that = (CommandNode<S>) o;
|
||||
final CommandNode<S> that = (CommandNode<S>) o;
|
||||
|
||||
if (!children.equals(that.children)) return false;
|
||||
if (command != null ? !command.equals(that.command) : that.command != null) return false;
|
||||
@@ -89,7 +89,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
protected abstract String getSortedKey();
|
||||
|
||||
@Override
|
||||
public int compareTo(CommandNode<S> o) {
|
||||
public int compareTo(final CommandNode<S> o) {
|
||||
return ComparisonChain
|
||||
.start()
|
||||
.compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.mojang.brigadier.tree;
|
||||
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
@@ -16,7 +15,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
|
||||
private final String literal;
|
||||
|
||||
public LiteralCommandNode(String literal, Command<S> command, Predicate<S> requirement) {
|
||||
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement) {
|
||||
super(command, requirement);
|
||||
this.literal = literal;
|
||||
}
|
||||
@@ -31,7 +30,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
for (int i = 0; i < literal.length(); i++) {
|
||||
if (reader.canRead() && reader.peek() == literal.charAt(i)) {
|
||||
reader.skip();
|
||||
@@ -44,18 +43,18 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
|
||||
public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
|
||||
if (literal.startsWith(command)) {
|
||||
output.add(literal);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof LiteralCommandNode)) return false;
|
||||
|
||||
LiteralCommandNode that = (LiteralCommandNode) o;
|
||||
final LiteralCommandNode that = (LiteralCommandNode) o;
|
||||
|
||||
if (!literal.equals(that.literal)) return false;
|
||||
return super.equals(o);
|
||||
|
||||
@@ -23,15 +23,15 @@ public class RootCommandNode<S> extends CommandNode<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
|
||||
public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof RootCommandNode)) return false;
|
||||
return super.equals(o);
|
||||
|
||||
Reference in New Issue
Block a user