diff --git a/src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java b/src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java index f273a75..c0bf8cc 100644 --- a/src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java +++ b/src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java @@ -1,6 +1,7 @@ package net.minecraft.commands.builder; import com.google.common.collect.Lists; +import net.minecraft.commands.tree.CommandNode; import java.util.List; @@ -15,4 +16,6 @@ public abstract class ArgumentBuilder { public List getArguments() { return arguments; } + + public abstract CommandNode build(); } diff --git a/src/main/java/net/minecraft/commands/builder/CommandBuilder.java b/src/main/java/net/minecraft/commands/builder/CommandBuilder.java index 7d3ec2a..311a1a2 100644 --- a/src/main/java/net/minecraft/commands/builder/CommandBuilder.java +++ b/src/main/java/net/minecraft/commands/builder/CommandBuilder.java @@ -1,6 +1,7 @@ package net.minecraft.commands.builder; import com.google.common.collect.Lists; +import net.minecraft.commands.tree.LiteralCommandNode; import java.util.List; @@ -38,4 +39,14 @@ public class CommandBuilder { public List getArguments() { return arguments; } + + public LiteralCommandNode build() { + LiteralCommandNode result = new LiteralCommandNode(getName()); + + for (ArgumentBuilder argument : arguments) { + result.addChild(argument.build()); + } + + return result; + } } diff --git a/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java b/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java index 2da607c..2d99607 100644 --- a/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java +++ b/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java @@ -1,6 +1,7 @@ package net.minecraft.commands.builder; import net.minecraft.commands.arguments.CommandArgumentType; +import net.minecraft.commands.tree.ArgumentCommandNode; public class RequiredArgumentBuilder extends ArgumentBuilder { private final String name; @@ -14,4 +15,22 @@ public class RequiredArgumentBuilder extends ArgumentBuilder { public static RequiredArgumentBuilder argument(String name, CommandArgumentType type) { return new RequiredArgumentBuilder(name, type); } + + public CommandArgumentType getType() { + return type; + } + + public String getName() { + return name; + } + + public ArgumentCommandNode build() { + ArgumentCommandNode result = new ArgumentCommandNode(getName(), getType()); + + for (ArgumentBuilder argument : getArguments()) { + result.addChild(argument.build()); + } + + return result; + } } diff --git a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java new file mode 100644 index 0000000..f278cd4 --- /dev/null +++ b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java @@ -0,0 +1,21 @@ +package net.minecraft.commands.tree; + +import net.minecraft.commands.arguments.CommandArgumentType; + +public class ArgumentCommandNode extends CommandNode { + private final String name; + private final CommandArgumentType type; + + public ArgumentCommandNode(String name, CommandArgumentType type) { + this.name = name; + this.type = type; + } + + public String getName() { + return name; + } + + public CommandArgumentType getType() { + return type; + } +} diff --git a/src/main/java/net/minecraft/commands/tree/CommandNode.java b/src/main/java/net/minecraft/commands/tree/CommandNode.java new file mode 100644 index 0000000..fc8dd10 --- /dev/null +++ b/src/main/java/net/minecraft/commands/tree/CommandNode.java @@ -0,0 +1,17 @@ +package net.minecraft.commands.tree; + +import com.google.common.collect.Lists; + +import java.util.List; + +public abstract class CommandNode { + private final List children = Lists.newArrayList(); + + public List getChildren() { + return children; + } + + public void addChild(CommandNode node) { + children.add(node); + } +} diff --git a/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java new file mode 100644 index 0000000..b0ec504 --- /dev/null +++ b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java @@ -0,0 +1,13 @@ +package net.minecraft.commands.tree; + +public class LiteralCommandNode extends CommandNode { + private final String literal; + + public LiteralCommandNode(String literal) { + this.literal = literal; + } + + public String getLiteral() { + return literal; + } +} diff --git a/src/main/java/net/minecraft/commands/tree/package-info.java b/src/main/java/net/minecraft/commands/tree/package-info.java new file mode 100644 index 0000000..3742bbb --- /dev/null +++ b/src/main/java/net/minecraft/commands/tree/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package net.minecraft.commands.tree; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/CommandNodeTest.java b/src/main/test/net/minecraft/commands/CommandNodeTest.java new file mode 100644 index 0000000..20cfaa8 --- /dev/null +++ b/src/main/test/net/minecraft/commands/CommandNodeTest.java @@ -0,0 +1,8 @@ +package net.minecraft.commands; + +import net.minecraft.commands.tree.CommandNode; + +public class CommandNodeTest { + CommandNode node; + +} \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java b/src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java index 4c8dca4..d10efd4 100644 --- a/src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java +++ b/src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java @@ -1,5 +1,6 @@ package net.minecraft.commands.builder; +import net.minecraft.commands.tree.CommandNode; import org.junit.Before; import org.junit.Test; @@ -14,7 +15,12 @@ public class ArgumentBuilderTest { @Before public void setUp() throws Exception { - builder = new ArgumentBuilder() {}; + builder = new ArgumentBuilder() { + @Override + public CommandNode build() { + return null; + } + }; } @Test diff --git a/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java b/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java index a62d6a9..aefbbce 100644 --- a/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java +++ b/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java @@ -1,5 +1,6 @@ package net.minecraft.commands.builder; +import net.minecraft.commands.tree.LiteralCommandNode; import org.junit.Before; import org.junit.Test; @@ -7,6 +8,7 @@ import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; public class CommandBuilderTest { @@ -26,4 +28,20 @@ public class CommandBuilderTest { assertThat(builder.getArguments(), hasSize(1)); assertThat(builder.getArguments(), hasItems((ArgumentBuilder) argument)); } + + @Test + public void testBuild() throws Exception { + LiteralCommandNode node = builder.build(); + + assertThat(node.getLiteral(), is("foo")); + } + + @Test + public void testBuildWithChildren() throws Exception { + builder.then(argument("bar", integer())); + builder.then(argument("baz", integer())); + LiteralCommandNode node = builder.build(); + + assertThat(node.getChildren(), hasSize(2)); + } } \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/builder/RequiredArgumentBuilderTest.java b/src/main/test/net/minecraft/commands/builder/RequiredArgumentBuilderTest.java new file mode 100644 index 0000000..56e6a80 --- /dev/null +++ b/src/main/test/net/minecraft/commands/builder/RequiredArgumentBuilderTest.java @@ -0,0 +1,40 @@ +package net.minecraft.commands.builder; + +import net.minecraft.commands.arguments.CommandArgumentType; +import net.minecraft.commands.tree.ArgumentCommandNode; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +import static net.minecraft.commands.arguments.IntegerArgumentType.integer; +import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class RequiredArgumentBuilderTest { + @Mock CommandArgumentType type; + RequiredArgumentBuilder builder; + + @Before + public void setUp() throws Exception { + builder = argument("foo", type); + } + + @Test + public void testBuild() throws Exception { + ArgumentCommandNode node = builder.build(); + + assertThat(node.getName(), is("foo")); + assertThat(node.getType(), is(type)); + } + + @Test + public void testBuildWithChildren() throws Exception { + builder.then(argument("bar", integer())); + builder.then(argument("baz", integer())); + ArgumentCommandNode node = builder.build(); + + assertThat(node.getChildren(), hasSize(2)); + } +} \ No newline at end of file