Changed how parsing is done, performed by dispatcher and can now look as deep as needed

This commit is contained in:
Nathan Adams
2014-09-24 17:12:06 +02:00
parent 8da6087618
commit d678a5cb21
11 changed files with 171 additions and 103 deletions
@@ -1,32 +1,51 @@
package net.minecraft.commands;
import com.google.common.collect.Maps;
import net.minecraft.commands.builder.LiteralArgumentBuilder;
import net.minecraft.commands.context.CommandContext;
import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.context.CommandContextBuilder;
import net.minecraft.commands.exceptions.CommandException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import net.minecraft.commands.exceptions.UnknownCommandException;
import net.minecraft.commands.tree.LiteralCommandNode;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.commands.tree.CommandNode;
import net.minecraft.commands.tree.RootCommandNode;
public class CommandDispatcher {
private final Map<String, LiteralCommandNode> commands = Maps.newHashMap();
private final RootCommandNode root = new RootCommandNode();
public void register(LiteralArgumentBuilder command) {
if (commands.containsKey(command.getLiteral())) {
throw new IllegalArgumentException("New command " + command.getLiteral() + " conflicts with existing command " + command.getLiteral());
}
commands.put(command.getLiteral(), command.build());
root.addChild(command.build());
}
public void execute(String command) throws CommandException {
LiteralCommandNode node = commands.get(command);
if (node == null) {
CommandContextBuilder contextBuilder = new CommandContextBuilder();
CommandNode node = root;
while (command.length() > 0 && !node.getChildren().isEmpty()) {
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;
}
}
if (exception != null) {
break;
}
}
if (command.length() > 0) {
throw new UnknownCommandException();
}
node.getCommand().run(new CommandContext(new HashMap<String, ParsedArgument<?>>()));
CommandContext context = contextBuilder.build();
context.getCommand().run(context);
}
}
@@ -1,14 +1,21 @@
package net.minecraft.commands.context;
import com.google.common.primitives.Primitives;
import net.minecraft.commands.Command;
import java.util.Map;
public class CommandContext {
private final Map<String, ParsedArgument<?>> arguments;
private final Command command;
public CommandContext(Map<String, ParsedArgument<?>> arguments) {
public CommandContext(Map<String, ParsedArgument<?>> arguments, Command command) {
this.arguments = arguments;
this.command = command;
}
public Command getCommand() {
return command;
}
@SuppressWarnings("unchecked")
@@ -1,11 +1,13 @@
package net.minecraft.commands.context;
import com.google.common.collect.Maps;
import net.minecraft.commands.Command;
import java.util.Map;
public class CommandContextBuilder {
private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
private Command command;
public CommandContextBuilder() {
}
@@ -15,7 +17,16 @@ public class CommandContextBuilder {
return this;
}
public Map<String, ParsedArgument<?>> getArguments() {
return arguments;
}
public CommandContextBuilder withCommand(Command command) {
this.command = command;
return this;
}
public CommandContext build() {
return new CommandContext(arguments);
return new CommandContext(arguments, command);
}
}
@@ -2,6 +2,7 @@ package net.minecraft.commands.tree;
import net.minecraft.commands.Command;
import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.context.CommandContextBuilder;
import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
@@ -25,23 +26,16 @@ public class ArgumentCommandNode<T> extends CommandNode {
}
@Override
public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException {
ParsedArgument<T> parsed = type.parse(command);
int start = parsed.getRaw().length() + 1;
int start = parsed.getRaw().length();
if (start < command.length()) {
String result = command.substring(start);
contextBuilder.withArgument(name, parsed);
for (CommandNode node : getChildren()) {
try {
return node.parse(result);
} catch (IllegalArgumentSyntaxException ignored) {
}
}
throw new IllegalArgumentSyntaxException();
if (command.length() > start) {
return command.substring(start + 1);
} else {
return this;
return "";
}
}
}
@@ -2,6 +2,7 @@ package net.minecraft.commands.tree;
import com.google.common.collect.Lists;
import net.minecraft.commands.Command;
import net.minecraft.commands.context.CommandContextBuilder;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
@@ -27,5 +28,5 @@ public abstract class CommandNode {
children.add(node);
}
public abstract CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException;
public abstract String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException;
}
@@ -1,6 +1,7 @@
package net.minecraft.commands.tree;
import net.minecraft.commands.Command;
import net.minecraft.commands.context.CommandContextBuilder;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
@@ -17,26 +18,16 @@ public class LiteralCommandNode extends CommandNode {
}
@Override
public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
if (command.startsWith(literal)) {
int start = literal.length() + 1;
if (start < command.length()) {
String result = command.substring(start);
for (CommandNode node : getChildren()) {
try {
return node.parse(result);
} catch (IllegalArgumentSyntaxException ignored) {
}
}
throw new IllegalArgumentSyntaxException();
} else {
return this;
}
} else {
public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException {
if (!command.startsWith(literal)) {
throw new IllegalArgumentSyntaxException();
}
int start = literal.length();
if (command.length() > start) {
return command.substring(start + 1);
} else {
return "";
}
}
}
@@ -0,0 +1,16 @@
package net.minecraft.commands.tree;
import net.minecraft.commands.context.CommandContextBuilder;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public class RootCommandNode extends CommandNode {
public RootCommandNode() {
super(null);
}
@Override
public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException {
return command;
}
}