diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 9c6d296..eebdb1f 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -162,4 +162,8 @@ public class CommandDispatcher { return nodes.toArray(new String[nodes.size()]); } + + public RootCommandNode getRoot() { + return root; + } } diff --git a/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java b/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java index f71192f..4313ee8 100644 --- a/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java +++ b/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java @@ -28,7 +28,7 @@ public abstract class ArgumentBuilder> { return getThis(); } - protected Command getCommand() { + public Command getCommand() { return command; } diff --git a/src/main/java/com/mojang/brigadier/builder/LiteralArgumentBuilder.java b/src/main/java/com/mojang/brigadier/builder/LiteralArgumentBuilder.java index 75c292b..5fce01d 100644 --- a/src/main/java/com/mojang/brigadier/builder/LiteralArgumentBuilder.java +++ b/src/main/java/com/mojang/brigadier/builder/LiteralArgumentBuilder.java @@ -19,7 +19,7 @@ public class LiteralArgumentBuilder extends ArgumentBuilder extends ArgumentBuilder getType() { + public CommandArgumentType getType() { return type; } - private String getName() { + public String getName() { return name; } diff --git a/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java b/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java index 3df7b65..b6bfb2d 100644 --- a/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java @@ -2,6 +2,8 @@ package com.mojang.brigadier.tree; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.CommandArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.ParsedArgument; import com.mojang.brigadier.exceptions.CommandException; @@ -60,6 +62,16 @@ public class ArgumentCommandNode extends CommandNode { type.listSuggestions(command, output); } + @Override + public RequiredArgumentBuilder createBuilder() { + final RequiredArgumentBuilder builder = RequiredArgumentBuilder.argument(name, type); + builder.requires(getRequirement()); + if (getCommand() != null) { + builder.executes(getCommand()); + } + return builder; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/mojang/brigadier/tree/CommandNode.java b/src/main/java/com/mojang/brigadier/tree/CommandNode.java index b81ba4c..58805db 100644 --- a/src/main/java/com/mojang/brigadier/tree/CommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/CommandNode.java @@ -2,6 +2,7 @@ package com.mojang.brigadier.tree; import com.google.common.collect.Maps; import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandException; @@ -65,6 +66,10 @@ public abstract class CommandNode { return 31 * children.hashCode() + (command != null ? command.hashCode() : 0); } + public Predicate getRequirement() { + return requirement; + } + protected abstract Object getMergeKey(); public abstract String getUsageText(); @@ -72,4 +77,6 @@ public abstract class CommandNode { public abstract String parse(String command, CommandContextBuilder contextBuilder) throws CommandException; public abstract void listSuggestions(String command, Set output); + + public abstract ArgumentBuilder createBuilder(); } diff --git a/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java b/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java index a12f81d..cc50084 100644 --- a/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java @@ -2,6 +2,8 @@ package com.mojang.brigadier.tree; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; @@ -70,4 +72,14 @@ public class LiteralCommandNode extends CommandNode { result = 31 * result + super.hashCode(); return result; } + + @Override + public LiteralArgumentBuilder createBuilder() { + final LiteralArgumentBuilder builder = LiteralArgumentBuilder.literal(this.literal); + builder.requires(getRequirement()); + if (getCommand() != null) { + builder.executes(getCommand()); + } + return builder; + } } diff --git a/src/main/java/com/mojang/brigadier/tree/RootCommandNode.java b/src/main/java/com/mojang/brigadier/tree/RootCommandNode.java index 16677e3..082844a 100644 --- a/src/main/java/com/mojang/brigadier/tree/RootCommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/RootCommandNode.java @@ -1,5 +1,6 @@ package com.mojang.brigadier.tree; +import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandException; @@ -35,4 +36,9 @@ public class RootCommandNode extends CommandNode { if (!(o instanceof RootCommandNode)) return false; return super.equals(o); } + + @Override + public ArgumentBuilder createBuilder() { + throw new IllegalStateException("Cannot convert root into a builder"); + } } diff --git a/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java index 043ae93..462e957 100644 --- a/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java @@ -5,6 +5,7 @@ import com.google.common.collect.Sets; import com.google.common.testing.EqualsTester; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandException; import org.junit.Before; @@ -105,4 +106,13 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest { ) .testEquals(); } + + @Test + public void testCreateBuilder() throws Exception { + final RequiredArgumentBuilder builder = node.createBuilder(); + assertThat(builder.getName(), is(node.getName())); + assertThat(builder.getType(), is(node.getType())); + assertThat(builder.getRequirement(), is(node.getRequirement())); + assertThat(builder.getCommand(), is(node.getCommand())); + } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java index e1462a2..34643b1 100644 --- a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java @@ -4,6 +4,8 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; import com.google.common.testing.EqualsTester; import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.exceptions.CommandException; import org.junit.Before; @@ -117,4 +119,12 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { ) .testEquals(); } + + @Test + public void testCreateBuilder() throws Exception { + final LiteralArgumentBuilder builder = node.createBuilder(); + assertThat(builder.getLiteral(), is(node.getLiteral())); + assertThat(builder.getRequirement(), is(node.getRequirement())); + assertThat(builder.getCommand(), is(node.getCommand())); + } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/tree/RootCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/RootCommandNodeTest.java index ac491fc..33a6437 100644 --- a/src/test/java/com/mojang/brigadier/tree/RootCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/RootCommandNodeTest.java @@ -48,6 +48,11 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest { assertThat(set, is(empty())); } + @Test(expected = IllegalStateException.class) + public void testCreateBuilder() throws Exception { + node.createBuilder(); + } + @Test public void testEquals() throws Exception { new EqualsTester()