Build stuff into nodes! They may not do anything, but it's node-tastic!

This commit is contained in:
Nathan Adams 2014-09-17 15:29:16 +02:00
parent 934ba3336f
commit 4f20d02a97
11 changed files with 161 additions and 1 deletions

View file

@ -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<ArgumentBuilder> getArguments() {
return arguments;
}
public abstract CommandNode build();
}

View file

@ -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<ArgumentBuilder> getArguments() {
return arguments;
}
public LiteralCommandNode build() {
LiteralCommandNode result = new LiteralCommandNode(getName());
for (ArgumentBuilder argument : arguments) {
result.addChild(argument.build());
}
return result;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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<CommandNode> children = Lists.newArrayList();
public List<CommandNode> getChildren() {
return children;
}
public void addChild(CommandNode node) {
children.add(node);
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,4 @@
@ParametersAreNonnullByDefault
package net.minecraft.commands.tree;
import javax.annotation.ParametersAreNonnullByDefault;

View file

@ -0,0 +1,8 @@
package net.minecraft.commands;
import net.minecraft.commands.tree.CommandNode;
public class CommandNodeTest {
CommandNode node;
}

View file

@ -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

View file

@ -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));
}
}

View file

@ -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));
}
}