Pass S (source type, was sometimes T) through to all nodes, so Command can have source type
This commit is contained in:
@@ -2,6 +2,6 @@ package com.mojang.brigadier;
|
||||
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
|
||||
public interface Command {
|
||||
void run(CommandContext context);
|
||||
public interface Command<S> {
|
||||
void run(CommandContext<S> context);
|
||||
}
|
||||
|
||||
@@ -17,14 +17,7 @@ import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandDispatcher<T> {
|
||||
private static final Predicate<CommandNode> HAS_COMMAND = new Predicate<CommandNode>() {
|
||||
@Override
|
||||
public boolean test(CommandNode input) {
|
||||
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(HAS_COMMAND));
|
||||
}
|
||||
};
|
||||
|
||||
public class CommandDispatcher<S> {
|
||||
public static final SimpleCommandExceptionType ERROR_UNKNOWN_COMMAND = new SimpleCommandExceptionType("command.unknown", "Unknown command");
|
||||
public static final String ARGUMENT_SEPARATOR = " ";
|
||||
private static final String USAGE_OPTIONAL_OPEN = "[";
|
||||
@@ -33,23 +26,29 @@ public class CommandDispatcher<T> {
|
||||
private static final String USAGE_REQUIRED_CLOSE = ")";
|
||||
private static final String USAGE_OR = "|";
|
||||
|
||||
private final RootCommandNode root = new RootCommandNode();
|
||||
private final RootCommandNode<S> root = new RootCommandNode<>();
|
||||
private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() {
|
||||
@Override
|
||||
public boolean test(CommandNode<S> input) {
|
||||
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
|
||||
}
|
||||
};
|
||||
|
||||
public void register(LiteralArgumentBuilder command) {
|
||||
public void register(LiteralArgumentBuilder<S> command) {
|
||||
root.addChild(command.build());
|
||||
}
|
||||
|
||||
public void execute(String command, T source) throws CommandException {
|
||||
CommandContext<T> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
||||
public void execute(String command, S source) throws CommandException {
|
||||
CommandContext<S> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
||||
context.getCommand().run(context);
|
||||
}
|
||||
|
||||
private CommandContext<T> parseNodes(CommandNode node, String command, CommandContextBuilder<T> contextBuilder) throws CommandException {
|
||||
private CommandContext<S> parseNodes(CommandNode<S> node, String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
CommandException exception = null;
|
||||
|
||||
for (CommandNode child : node.getChildren()) {
|
||||
for (CommandNode<S> child : node.getChildren()) {
|
||||
try {
|
||||
CommandContextBuilder<T> context = contextBuilder.copy();
|
||||
CommandContextBuilder<S> context = contextBuilder.copy();
|
||||
String remaining = child.parse(command, context);
|
||||
if (child.getCommand() != null) {
|
||||
context.withCommand(child.getCommand());
|
||||
@@ -74,10 +73,10 @@ public class CommandDispatcher<T> {
|
||||
return contextBuilder.build();
|
||||
}
|
||||
|
||||
public String getUsage(String command, T source) throws CommandException {
|
||||
CommandContext<T> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
||||
CommandNode base = Iterables.getLast(context.getNodes().keySet());
|
||||
List<CommandNode> children = base.getChildren().stream().filter(HAS_COMMAND).collect(Collectors.toList());
|
||||
public String getUsage(String command, S source) throws CommandException {
|
||||
CommandContext<S> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
||||
CommandNode<S> base = Iterables.getLast(context.getNodes().keySet());
|
||||
List<CommandNode<S>> children = base.getChildren().stream().filter(hasCommand).collect(Collectors.toList());
|
||||
boolean optional = base.getCommand() != null;
|
||||
|
||||
if (children.isEmpty()) {
|
||||
@@ -113,10 +112,10 @@ public class CommandDispatcher<T> {
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private Set<String> findSuggestions(CommandNode node, String command, CommandContextBuilder<T> contextBuilder, Set<String> result) {
|
||||
for (CommandNode child : node.getChildren()) {
|
||||
private Set<String> findSuggestions(CommandNode<S> node, String command, CommandContextBuilder<S> contextBuilder, Set<String> result) {
|
||||
for (CommandNode<S> child : node.getChildren()) {
|
||||
try {
|
||||
CommandContextBuilder<T> context = contextBuilder.copy();
|
||||
CommandContextBuilder<S> context = contextBuilder.copy();
|
||||
String remaining = child.parse(command, context);
|
||||
if (remaining.isEmpty()) {
|
||||
child.listSuggestions(command, result);
|
||||
@@ -131,7 +130,7 @@ public class CommandDispatcher<T> {
|
||||
return result;
|
||||
}
|
||||
|
||||
public String[] getCompletionSuggestions(String command, T source) {
|
||||
public String[] getCompletionSuggestions(String command, S source) {
|
||||
final Set<String> nodes = findSuggestions(root, command, new CommandContextBuilder<>(source), Sets.newLinkedHashSet());
|
||||
|
||||
return nodes.toArray(new String[nodes.size()]);
|
||||
|
||||
@@ -6,29 +6,29 @@ import com.mojang.brigadier.tree.RootCommandNode;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public abstract class ArgumentBuilder<T extends ArgumentBuilder<?>> {
|
||||
private final RootCommandNode arguments = new RootCommandNode();
|
||||
private Command command;
|
||||
public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, ?>> {
|
||||
private final RootCommandNode<S> arguments = new RootCommandNode<>();
|
||||
private Command<S> command;
|
||||
|
||||
protected abstract T getThis();
|
||||
|
||||
public T then(ArgumentBuilder argument) {
|
||||
public T then(ArgumentBuilder<S, ?> argument) {
|
||||
arguments.addChild(argument.build());
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public Collection<CommandNode> getArguments() {
|
||||
public Collection<CommandNode<S>> getArguments() {
|
||||
return arguments.getChildren();
|
||||
}
|
||||
|
||||
public T executes(Command command) {
|
||||
public T executes(Command<S> command) {
|
||||
this.command = command;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
protected Command getCommand() {
|
||||
protected Command<S> getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
public abstract CommandNode build();
|
||||
public abstract CommandNode<S> build();
|
||||
}
|
||||
|
||||
@@ -3,19 +3,19 @@ package com.mojang.brigadier.builder;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
|
||||
public class LiteralArgumentBuilder extends ArgumentBuilder<LiteralArgumentBuilder> {
|
||||
public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumentBuilder<S>> {
|
||||
private final String literal;
|
||||
|
||||
protected LiteralArgumentBuilder(String literal) {
|
||||
this.literal = literal;
|
||||
}
|
||||
|
||||
public static LiteralArgumentBuilder literal(String name) {
|
||||
return new LiteralArgumentBuilder(name);
|
||||
public static <S> LiteralArgumentBuilder<S> literal(String name) {
|
||||
return new LiteralArgumentBuilder<>(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LiteralArgumentBuilder getThis() {
|
||||
protected LiteralArgumentBuilder<S> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ public class LiteralArgumentBuilder extends ArgumentBuilder<LiteralArgumentBuild
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiteralCommandNode build() {
|
||||
LiteralCommandNode result = new LiteralCommandNode(getLiteral(), getCommand());
|
||||
public LiteralCommandNode<S> build() {
|
||||
LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand());
|
||||
|
||||
for (CommandNode argument : getArguments()) {
|
||||
for (CommandNode<S> argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.mojang.brigadier.arguments.CommandArgumentType;
|
||||
import com.mojang.brigadier.tree.ArgumentCommandNode;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
|
||||
public class RequiredArgumentBuilder<T> extends ArgumentBuilder<RequiredArgumentBuilder<T>> {
|
||||
public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredArgumentBuilder<S, T>> {
|
||||
private final String name;
|
||||
private final CommandArgumentType<T> type;
|
||||
|
||||
@@ -13,12 +13,12 @@ public class RequiredArgumentBuilder<T> extends ArgumentBuilder<RequiredArgument
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public static <T> RequiredArgumentBuilder<T> argument(String name, CommandArgumentType<T> type) {
|
||||
public static <S, T> RequiredArgumentBuilder<S, T> argument(String name, CommandArgumentType<T> type) {
|
||||
return new RequiredArgumentBuilder<>(name, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RequiredArgumentBuilder<T> getThis() {
|
||||
protected RequiredArgumentBuilder<S, T> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -30,10 +30,10 @@ public class RequiredArgumentBuilder<T> extends ArgumentBuilder<RequiredArgument
|
||||
return name;
|
||||
}
|
||||
|
||||
public ArgumentCommandNode<T> build() {
|
||||
ArgumentCommandNode<T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand());
|
||||
public ArgumentCommandNode<S, T> build() {
|
||||
ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand());
|
||||
|
||||
for (CommandNode argument : getArguments()) {
|
||||
for (CommandNode<S> argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,26 +9,26 @@ import com.mojang.brigadier.tree.CommandNode;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandContext<T> {
|
||||
public class CommandContext<S> {
|
||||
private final Joiner JOINER = Joiner.on(CommandDispatcher.ARGUMENT_SEPARATOR);
|
||||
|
||||
private final T source;
|
||||
private final S source;
|
||||
private final Map<String, ParsedArgument<?>> arguments;
|
||||
private final Command command;
|
||||
private final Map<CommandNode, String> nodes;
|
||||
private final Command<S> command;
|
||||
private final Map<CommandNode<S>, String> nodes;
|
||||
|
||||
public CommandContext(T source, Map<String, ParsedArgument<?>> arguments, Command command, Map<CommandNode, String> nodes) {
|
||||
public CommandContext(S source, Map<String, ParsedArgument<?>> arguments, Command<S> command, Map<CommandNode<S>, String> nodes) {
|
||||
this.source = source;
|
||||
this.arguments = arguments;
|
||||
this.command = command;
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
public Command getCommand() {
|
||||
public Command<S> getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
public T getSource() {
|
||||
public S getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public class CommandContext<T> {
|
||||
return JOINER.join(nodes.values());
|
||||
}
|
||||
|
||||
public Map<CommandNode, String> getNodes() {
|
||||
public Map<CommandNode<S>, String> getNodes() {
|
||||
return nodes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@ import com.mojang.brigadier.tree.CommandNode;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandContextBuilder<T> {
|
||||
public class CommandContextBuilder<S> {
|
||||
private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
|
||||
private final Map<CommandNode, String> nodes = Maps.newLinkedHashMap();
|
||||
private final T source;
|
||||
private Command command;
|
||||
private final Map<CommandNode<S>, String> nodes = Maps.newLinkedHashMap();
|
||||
private final S source;
|
||||
private Command<S> command;
|
||||
|
||||
public CommandContextBuilder(T source) {
|
||||
public CommandContextBuilder(S source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<T> withArgument(String name, ParsedArgument<?> argument) {
|
||||
public CommandContextBuilder<S> withArgument(String name, ParsedArgument<?> argument) {
|
||||
this.arguments.put(name, argument);
|
||||
return this;
|
||||
}
|
||||
@@ -25,25 +25,25 @@ public class CommandContextBuilder<T> {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<T> withCommand(Command command) {
|
||||
public CommandContextBuilder<S> withCommand(Command<S> command) {
|
||||
this.command = command;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<T> withNode(CommandNode node, String raw) {
|
||||
public CommandContextBuilder<S> withNode(CommandNode<S> node, String raw) {
|
||||
this.nodes.put(node, raw);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<T> copy() {
|
||||
CommandContextBuilder<T> copy = new CommandContextBuilder<>(source);
|
||||
public CommandContextBuilder<S> copy() {
|
||||
CommandContextBuilder<S> copy = new CommandContextBuilder<>(source);
|
||||
copy.command = this.command;
|
||||
copy.arguments.putAll(this.arguments);
|
||||
copy.nodes.putAll(this.nodes);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public CommandContext<T> build() {
|
||||
public CommandContext<S> build() {
|
||||
return new CommandContext<>(source, arguments, command, nodes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ import com.mojang.brigadier.exceptions.CommandException;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class ArgumentCommandNode<T> extends CommandNode {
|
||||
public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
private static final String USAGE_ARGUMENT_OPEN = "<";
|
||||
private static final String USAGE_ARGUMENT_CLOSE = ">";
|
||||
|
||||
private final String name;
|
||||
private final CommandArgumentType<T> type;
|
||||
|
||||
public ArgumentCommandNode(String name, CommandArgumentType<T> type, Command command) {
|
||||
public ArgumentCommandNode(String name, CommandArgumentType<T> type, Command<S> command) {
|
||||
super(command);
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
@@ -40,7 +40,7 @@ public class ArgumentCommandNode<T> extends CommandNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(String command, CommandContextBuilder<?> contextBuilder) throws CommandException {
|
||||
public String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
ParsedArgument<T> parsed = type.parse(command);
|
||||
int start = parsed.getRaw().length();
|
||||
|
||||
|
||||
@@ -9,30 +9,30 @@ import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class CommandNode {
|
||||
private final Map<Object, CommandNode> children = Maps.newLinkedHashMap();
|
||||
private Command command;
|
||||
public abstract class CommandNode<S> {
|
||||
private final Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap();
|
||||
private Command<S> command;
|
||||
|
||||
protected CommandNode(Command command) {
|
||||
protected CommandNode(Command<S> command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public Command getCommand() {
|
||||
public Command<S> getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
public Collection<CommandNode> getChildren() {
|
||||
public Collection<CommandNode<S>> getChildren() {
|
||||
return children.values();
|
||||
}
|
||||
|
||||
public void addChild(CommandNode node) {
|
||||
CommandNode child = children.get(node.getMergeKey());
|
||||
public void addChild(CommandNode<S> node) {
|
||||
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 grandchild : node.getChildren()) {
|
||||
for (CommandNode<S> grandchild : node.getChildren()) {
|
||||
child.addChild(grandchild);
|
||||
}
|
||||
} else {
|
||||
@@ -45,7 +45,7 @@ public abstract class CommandNode {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CommandNode)) return false;
|
||||
|
||||
CommandNode that = (CommandNode) o;
|
||||
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;
|
||||
@@ -62,7 +62,7 @@ public abstract class CommandNode {
|
||||
|
||||
public abstract String getUsageText();
|
||||
|
||||
public abstract String parse(String command, CommandContextBuilder<?> contextBuilder) throws CommandException;
|
||||
public abstract String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException;
|
||||
|
||||
public abstract void listSuggestions(String command, Set<String> output);
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class LiteralCommandNode extends CommandNode {
|
||||
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;
|
||||
|
||||
public LiteralCommandNode(String literal, Command command) {
|
||||
public LiteralCommandNode(String literal, Command<S> command) {
|
||||
super(command);
|
||||
this.literal = literal;
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class LiteralCommandNode extends CommandNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(String command, CommandContextBuilder<?> contextBuilder) throws CommandException {
|
||||
public String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
String expected = literal + (command.length() > literal.length() ? CommandDispatcher.ARGUMENT_SEPARATOR : "");
|
||||
|
||||
if (!command.startsWith(expected)) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.mojang.brigadier.exceptions.CommandException;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class RootCommandNode extends CommandNode {
|
||||
public class RootCommandNode<S> extends CommandNode<S> {
|
||||
public RootCommandNode() {
|
||||
super(null);
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class RootCommandNode extends CommandNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(String command, CommandContextBuilder<?> contextBuilder) throws CommandException {
|
||||
public String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
return command;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user