Added a 'source' for commands that may be passed around in the context
This commit is contained in:
@@ -10,7 +10,7 @@ import net.minecraft.commands.exceptions.UnknownCommandException;
|
||||
import net.minecraft.commands.tree.CommandNode;
|
||||
import net.minecraft.commands.tree.RootCommandNode;
|
||||
|
||||
public class CommandDispatcher {
|
||||
public class CommandDispatcher<T> {
|
||||
public static final String ARGUMENT_SEPARATOR = " ";
|
||||
|
||||
private final RootCommandNode root = new RootCommandNode();
|
||||
@@ -19,17 +19,17 @@ public class CommandDispatcher {
|
||||
root.addChild(command.build());
|
||||
}
|
||||
|
||||
public void execute(String command) throws CommandException {
|
||||
CommandContext context = parseNodes(root, command, new CommandContextBuilder());
|
||||
public void execute(String command, T source) throws CommandException {
|
||||
CommandContext<T> context = parseNodes(root, command, new CommandContextBuilder<T>(source));
|
||||
context.getCommand().run(context);
|
||||
}
|
||||
|
||||
protected CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException, UnknownCommandException {
|
||||
protected CommandContext<T> parseNodes(CommandNode node, String command, CommandContextBuilder<T> contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException, UnknownCommandException {
|
||||
IllegalArgumentSyntaxException exception = null;
|
||||
|
||||
for (CommandNode child : node.getChildren()) {
|
||||
try {
|
||||
CommandContextBuilder context = contextBuilder.copy();
|
||||
CommandContextBuilder<T> context = contextBuilder.copy();
|
||||
String remaining = child.parse(command, context);
|
||||
if (child.getCommand() != null) {
|
||||
context.withCommand(child.getCommand());
|
||||
|
||||
@@ -30,7 +30,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
||||
return new IntegerArgumentType(min, max);
|
||||
}
|
||||
|
||||
public static int getInteger(CommandContext context, String name) {
|
||||
public static int getInteger(CommandContext<?> context, String name) {
|
||||
return context.getArgument(name, int.class).getResult();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,13 @@ import net.minecraft.commands.Command;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandContext {
|
||||
public class CommandContext<T> {
|
||||
private final T source;
|
||||
private final Map<String, ParsedArgument<?>> arguments;
|
||||
private final Command command;
|
||||
|
||||
public CommandContext(Map<String, ParsedArgument<?>> arguments, Command command) {
|
||||
public CommandContext(T source, Map<String, ParsedArgument<?>> arguments, Command command) {
|
||||
this.source = source;
|
||||
this.arguments = arguments;
|
||||
this.command = command;
|
||||
}
|
||||
@@ -18,8 +20,12 @@ public class CommandContext {
|
||||
return command;
|
||||
}
|
||||
|
||||
public T getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> ParsedArgument<T> getArgument(String name, Class<T> clazz) {
|
||||
public <V> ParsedArgument<V> getArgument(String name, Class<V> clazz) {
|
||||
ParsedArgument<?> argument = arguments.get(name);
|
||||
|
||||
if (argument == null) {
|
||||
@@ -27,7 +33,7 @@ public class CommandContext {
|
||||
}
|
||||
|
||||
if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) {
|
||||
return (ParsedArgument<T>) argument;
|
||||
return (ParsedArgument<V>) argument;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz);
|
||||
}
|
||||
|
||||
@@ -5,14 +5,16 @@ import net.minecraft.commands.Command;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandContextBuilder {
|
||||
public class CommandContextBuilder<T> {
|
||||
private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
|
||||
private final T source;
|
||||
private Command command;
|
||||
|
||||
public CommandContextBuilder() {
|
||||
public CommandContextBuilder(T source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public CommandContextBuilder withArgument(String name, ParsedArgument<?> argument) {
|
||||
public CommandContextBuilder<T> withArgument(String name, ParsedArgument<?> argument) {
|
||||
this.arguments.put(name, argument);
|
||||
return this;
|
||||
}
|
||||
@@ -21,19 +23,19 @@ public class CommandContextBuilder {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
public CommandContextBuilder withCommand(Command command) {
|
||||
public CommandContextBuilder<T> withCommand(Command command) {
|
||||
this.command = command;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandContextBuilder copy() {
|
||||
CommandContextBuilder copy = new CommandContextBuilder();
|
||||
public CommandContextBuilder<T> copy() {
|
||||
CommandContextBuilder<T> copy = new CommandContextBuilder<T>(source);
|
||||
copy.command = this.command;
|
||||
copy.arguments.putAll(this.arguments);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public CommandContext build() {
|
||||
return new CommandContext(arguments, command);
|
||||
public CommandContext<T> build() {
|
||||
return new CommandContext<T>(source, arguments, command);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user