Added methods for getting path by node & nodes by path

This commit is contained in:
Nathan Adams 2017-11-13 12:40:05 +01:00
parent 6b57d5f260
commit 1ce9894c07
7 changed files with 86 additions and 17 deletions

View file

@ -354,4 +354,44 @@ public class CommandDispatcher<S> {
public RootCommandNode<S> getRoot() { public RootCommandNode<S> getRoot() {
return root; return root;
} }
public Collection<String> getPath(final CommandNode<S> target) {
final List<List<CommandNode<S>>> nodes = new ArrayList<>();
addPaths(root, nodes, new ArrayList<>());
for (final List<CommandNode<S>> list : nodes) {
if (list.get(list.size() - 1) == target) {
final List<String> result = new ArrayList<>(list.size());
for (final CommandNode<S> node : list) {
if (node != root) {
result.add(node.getName());
}
}
return result;
}
}
return Collections.emptyList();
}
public CommandNode<S> findNode(final Collection<String> path) {
CommandNode<S> node = root;
for (final String name : path) {
node = node.getChild(name);
if (node == null) {
return null;
}
}
return node;
}
private void addPaths(final CommandNode<S> node, final List<List<CommandNode<S>>> result, final List<CommandNode<S>> parents) {
final List<CommandNode<S>> current = new ArrayList<>(parents);
current.add(node);
result.add(current);
for (final CommandNode<S> child : node.getChildren()) {
addPaths(child, result, current);
}
}
} }

View file

@ -7,8 +7,6 @@ import com.mojang.brigadier.tree.RootCommandNode;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> { public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
@ -28,6 +26,14 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return getThis(); return getThis();
} }
public T then(final CommandNode<S> argument) {
if (target != null) {
throw new IllegalStateException("Cannot add children to a redirected node");
}
arguments.addChild(argument);
return getThis();
}
public Collection<CommandNode<S>> getArguments() { public Collection<CommandNode<S>> getArguments() {
return arguments.getChildren(); return arguments.getChildren();
} }

View file

@ -13,7 +13,6 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public class ArgumentCommandNode<S, T> extends CommandNode<S> { public class ArgumentCommandNode<S, T> extends CommandNode<S> {
@ -31,16 +30,12 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
this.suggestionsProvider = suggestionsProvider; this.suggestionsProvider = suggestionsProvider;
} }
public String getName() {
return name;
}
public ArgumentType<T> getType() { public ArgumentType<T> getType() {
return type; return type;
} }
@Override @Override
protected Object getMergeKey() { public String getName() {
return name; return name;
} }

View file

@ -18,7 +18,7 @@ import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> { public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap(); private Map<String, CommandNode<S>> children = Maps.newLinkedHashMap();
private final Predicate<S> requirement; private final Predicate<S> requirement;
private final CommandNode<S> redirect; private final CommandNode<S> redirect;
private final RedirectModifier<S> modifier; private final RedirectModifier<S> modifier;
@ -39,6 +39,10 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
return children.values(); return children.values();
} }
public CommandNode<S> getChild(final String name) {
return children.get(name);
}
public CommandNode<S> getRedirect() { public CommandNode<S> getRedirect() {
return redirect; return redirect;
} }
@ -52,7 +56,11 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
} }
public void addChild(final CommandNode<S> node) { public void addChild(final CommandNode<S> node) {
final CommandNode<S> child = children.get(node.getMergeKey()); if (node instanceof RootCommandNode) {
throw new UnsupportedOperationException("Cannot add a RootCommandNode as a child to any other CommandNode");
}
final CommandNode<S> child = children.get(node.getName());
if (child != null) { if (child != null) {
// We've found something to merge onto // We've found something to merge onto
if (node.getCommand() != null) { if (node.getCommand() != null) {
@ -62,7 +70,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
child.addChild(grandchild); child.addChild(grandchild);
} }
} else { } else {
children.put(node.getMergeKey(), node); children.put(node.getName(), node);
} }
children = children.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); children = children.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
@ -90,7 +98,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
return requirement; return requirement;
} }
protected abstract Object getMergeKey(); public abstract String getName();
public abstract String getUsageText(); public abstract String getUsageText();

View file

@ -30,7 +30,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
} }
@Override @Override
protected Object getMergeKey() { public String getName() {
return literal; return literal;
} }

View file

@ -16,8 +16,8 @@ public class RootCommandNode<S> extends CommandNode<S> {
} }
@Override @Override
protected Object getMergeKey() { public String getName() {
throw new UnsupportedOperationException("Cannot add a RootCommandNode as a child to any other CommandNode"); return "";
} }
@Override @Override

View file

@ -11,9 +11,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.function.Function;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
@ -22,6 +20,7 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.argThat;
@ -357,4 +356,25 @@ public class CommandDispatcherTest {
assertThat(ex.getCursor(), is(4)); assertThat(ex.getCursor(), is(4));
} }
} }
@Test
public void testGetPath() {
final LiteralCommandNode<Object> bar = literal("bar").build();
subject.register(literal("foo").then(bar));
assertThat(subject.getPath(bar), equalTo(Lists.newArrayList("foo", "bar")));
}
@Test
public void testFindNodeExists() {
final LiteralCommandNode<Object> bar = literal("bar").build();
subject.register(literal("foo").then(bar));
assertThat(subject.findNode(Lists.newArrayList("foo", "bar")), is(bar));
}
@Test
public void testFindNodeDoesntExist() {
assertThat(subject.findNode(Lists.newArrayList("foo", "bar")), is(nullValue()));
}
} }