Added methods for getting path by node & nodes by path
This commit is contained in:
@@ -354,4 +354,44 @@ public class CommandDispatcher<S> {
|
||||
public RootCommandNode<S> getRoot() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ import com.mojang.brigadier.tree.RootCommandNode;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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() {
|
||||
return arguments.getChildren();
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
@@ -31,16 +30,12 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
this.suggestionsProvider = suggestionsProvider;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ArgumentType<T> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getMergeKey() {
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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 CommandNode<S> redirect;
|
||||
private final RedirectModifier<S> modifier;
|
||||
@@ -39,6 +39,10 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
return children.values();
|
||||
}
|
||||
|
||||
public CommandNode<S> getChild(final String name) {
|
||||
return children.get(name);
|
||||
}
|
||||
|
||||
public CommandNode<S> getRedirect() {
|
||||
return redirect;
|
||||
}
|
||||
@@ -52,7 +56,11 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
}
|
||||
|
||||
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) {
|
||||
// We've found something to merge onto
|
||||
if (node.getCommand() != null) {
|
||||
@@ -62,7 +70,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
child.addChild(grandchild);
|
||||
}
|
||||
} 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));
|
||||
@@ -90,7 +98,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
return requirement;
|
||||
}
|
||||
|
||||
protected abstract Object getMergeKey();
|
||||
public abstract String getName();
|
||||
|
||||
public abstract String getUsageText();
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getMergeKey() {
|
||||
public String getName() {
|
||||
return literal;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ public class RootCommandNode<S> extends CommandNode<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getMergeKey() {
|
||||
throw new UnsupportedOperationException("Cannot add a RootCommandNode as a child to any other CommandNode");
|
||||
public String getName() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user