diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index ecd1cba..f27e5e1 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -32,7 +32,7 @@ public class CommandDispatcher { private static final String USAGE_REQUIRED_CLOSE = ")"; private static final String USAGE_OR = "|"; - private final RootCommandNode root = new RootCommandNode<>(); + private final RootCommandNode root; private final Predicate> hasCommand = new Predicate>() { @Override public boolean test(final CommandNode input) { @@ -40,9 +40,18 @@ public class CommandDispatcher { } }; - public void register(final LiteralArgumentBuilder command) { + public CommandDispatcher(final RootCommandNode root) { + this.root = root; + } + + public CommandDispatcher() { + this(new RootCommandNode<>()); + } + + public LiteralCommandNode register(final LiteralArgumentBuilder command) { final LiteralCommandNode build = command.build(); root.addChild(build); + return build; } public int execute(final String input, final S source) throws CommandException { @@ -126,7 +135,8 @@ public class CommandDispatcher { } if (node.getRedirect() != null) { - result.add(prefix.isEmpty() ? node.getUsageText() + ARGUMENT_SEPARATOR + "..." : prefix + ARGUMENT_SEPARATOR + "..."); + final String redirect = node.getRedirect() == root ? "..." : "-> " + node.getRedirect().getUsageText(); + result.add(prefix.isEmpty() ? node.getUsageText() + ARGUMENT_SEPARATOR + redirect : prefix + ARGUMENT_SEPARATOR + redirect); } else if (!node.getChildren().isEmpty()) { for (final CommandNode child : node.getChildren()) { getAllUsage(child, source, result, prefix.isEmpty() ? child.getUsageText() : prefix + ARGUMENT_SEPARATOR + child.getUsageText()); @@ -159,7 +169,8 @@ public class CommandDispatcher { if (!deep) { if (node.getRedirect() != null) { - return self + ARGUMENT_SEPARATOR + "..."; + final String redirect = node.getRedirect() == root ? "..." : "-> " + node.getRedirect().getUsageText(); + return self + ARGUMENT_SEPARATOR + redirect; } else { final Collection> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList()); if (children.size() == 1) { diff --git a/src/test/java/com/mojang/brigadier/CommandDispatcherUsagesTest.java b/src/test/java/com/mojang/brigadier/CommandDispatcherUsagesTest.java index 3023a78..2a76d02 100644 --- a/src/test/java/com/mojang/brigadier/CommandDispatcherUsagesTest.java +++ b/src/test/java/com/mojang/brigadier/CommandDispatcherUsagesTest.java @@ -91,6 +91,10 @@ public class CommandDispatcherUsagesTest { literal("j") .redirect(subject.getRoot()) ); + subject.register( + literal("k") + .redirect(get("h")) + ); } private CommandNode get(final String command) { @@ -141,6 +145,7 @@ public class CommandDispatcherUsagesTest { "i 1", "i 2", "j ...", + "k -> h", })); } @@ -157,6 +162,7 @@ public class CommandDispatcherUsagesTest { .put(get("h"), "h [1|2|3]") .put(get("i"), "i [1|2]") .put(get("j"), "j ...") + .put(get("k"), "k -> h") .build() )); }