Implemented merging of CommandNode children

This commit is contained in:
Nathan Adams
2014-09-25 17:21:26 +02:00
parent d8c9e15a1c
commit 82b576e38c
9 changed files with 166 additions and 31 deletions
@@ -25,6 +25,11 @@ public class ArgumentCommandNode<T> extends CommandNode {
return type;
}
@Override
protected Object getMergeKey() {
return name;
}
@Override
public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException {
ParsedArgument<T> parsed = type.parse(command);
@@ -48,16 +53,14 @@ public class ArgumentCommandNode<T> extends CommandNode {
if (!name.equals(that.name)) return false;
if (!type.equals(that.type)) return false;
if (!getChildren().equals(that.getChildren())) return false;
return true;
return super.equals(o);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + type.hashCode();
result = 31 * result + getChildren().hashCode();
result = 31 * super.hashCode();
return result;
}
}
@@ -1,16 +1,17 @@
package net.minecraft.commands.tree;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.commands.Command;
import net.minecraft.commands.context.CommandContextBuilder;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import java.util.List;
import java.util.Collection;
import java.util.Map;
public abstract class CommandNode {
private final Command command;
private final List<CommandNode> children = Lists.newArrayList();
private final Map<Object, CommandNode> children = Maps.newLinkedHashMap();
private Command command;
protected CommandNode(Command command) {
this.command = command;
@@ -20,13 +21,44 @@ public abstract class CommandNode {
return command;
}
public List<CommandNode> getChildren() {
return children;
public Collection<CommandNode> getChildren() {
return children.values();
}
public void addChild(CommandNode node) {
children.add(node);
CommandNode 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()) {
child.addChild(grandchild);
}
} else {
children.put(node.getMergeKey(), node);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CommandNode)) return false;
CommandNode that = (CommandNode) o;
if (!children.equals(that.children)) return false;
if (command != null ? !command.equals(that.command) : that.command != null) return false;
return true;
}
@Override
public int hashCode() {
return 31 * children.hashCode() + (command != null ? command.hashCode() : 0);
}
protected abstract Object getMergeKey();
public abstract String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException;
}
@@ -18,6 +18,11 @@ public class LiteralCommandNode extends CommandNode {
return literal;
}
@Override
protected Object getMergeKey() {
return literal;
}
@Override
public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException {
String expected = literal + (command.length() > literal.length() ? CommandDispatcher.ARGUMENT_SEPARATOR : "");
@@ -38,15 +43,13 @@ public class LiteralCommandNode extends CommandNode {
LiteralCommandNode that = (LiteralCommandNode) o;
if (!literal.equals(that.literal)) return false;
if (!getChildren().equals(that.getChildren())) return false;
return true;
return super.equals(o);
}
@Override
public int hashCode() {
int result = literal.hashCode();
result = 31 * result + getChildren().hashCode();
result = 31 * result + super.hashCode();
return result;
}
}
@@ -9,6 +9,11 @@ public class RootCommandNode extends CommandNode {
super(null);
}
@Override
protected Object getMergeKey() {
throw new UnsupportedOperationException("Cannot add a RootCommandNode as a child to any other CommandNode");
}
@Override
public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException {
return command;
@@ -18,16 +23,6 @@ public class RootCommandNode extends CommandNode {
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RootCommandNode)) return false;
RootCommandNode that = (RootCommandNode) o;
if (!getChildren().equals(that.getChildren())) return false;
return true;
}
@Override
public int hashCode() {
return getChildren().hashCode();
return super.equals(o);
}
}