Always try every route to find a matching command
This commit is contained in:
@@ -3,6 +3,7 @@ package net.minecraft.commands;
|
||||
import net.minecraft.commands.builder.LiteralArgumentBuilder;
|
||||
import net.minecraft.commands.context.CommandContext;
|
||||
import net.minecraft.commands.context.CommandContextBuilder;
|
||||
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
||||
import net.minecraft.commands.exceptions.CommandException;
|
||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||
import net.minecraft.commands.exceptions.UnknownCommandException;
|
||||
@@ -19,35 +20,33 @@ public class CommandDispatcher {
|
||||
}
|
||||
|
||||
public void execute(String command) throws CommandException {
|
||||
CommandContextBuilder contextBuilder = new CommandContextBuilder();
|
||||
CommandNode node = root;
|
||||
CommandContext context = parseNodes(root, command, new CommandContextBuilder());
|
||||
context.getCommand().run(context);
|
||||
}
|
||||
|
||||
while (command.length() > 0 && !node.getChildren().isEmpty()) {
|
||||
IllegalArgumentSyntaxException exception = null;
|
||||
protected CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException, UnknownCommandException {
|
||||
IllegalArgumentSyntaxException exception = null;
|
||||
|
||||
for (CommandNode child : node.getChildren()) {
|
||||
try {
|
||||
command = child.parse(command, contextBuilder);
|
||||
if (child.getCommand() != null) {
|
||||
contextBuilder.withCommand(child.getCommand());
|
||||
}
|
||||
node = child;
|
||||
break;
|
||||
} catch (IllegalArgumentSyntaxException ex) {
|
||||
exception = ex;
|
||||
for (CommandNode child : node.getChildren()) {
|
||||
try {
|
||||
CommandContextBuilder context = contextBuilder.copy();
|
||||
String remaining = child.parse(command, context);
|
||||
if (child.getCommand() != null) {
|
||||
context.withCommand(child.getCommand());
|
||||
}
|
||||
}
|
||||
|
||||
if (exception != null) {
|
||||
break;
|
||||
return parseNodes(child, remaining, context);
|
||||
} catch (IllegalArgumentSyntaxException ex) {
|
||||
exception = ex;
|
||||
}
|
||||
}
|
||||
|
||||
if (exception != null) {
|
||||
throw exception;
|
||||
}
|
||||
if (command.length() > 0) {
|
||||
throw new UnknownCommandException();
|
||||
}
|
||||
|
||||
CommandContext context = contextBuilder.build();
|
||||
context.getCommand().run(context);
|
||||
return contextBuilder.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,13 @@ public class CommandContextBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandContextBuilder copy() {
|
||||
CommandContextBuilder copy = new CommandContextBuilder();
|
||||
copy.command = this.command;
|
||||
copy.arguments.putAll(this.arguments);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public CommandContext build() {
|
||||
return new CommandContext(arguments, command);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user