Renamed CommandBuilder to LiteralArgumentBuilder

This commit is contained in:
Nathan Adams
2014-09-24 11:57:12 +02:00
parent 9585aaf0b3
commit 3ef22ef317
9 changed files with 84 additions and 88 deletions
@@ -1,7 +1,7 @@
package net.minecraft.commands; package net.minecraft.commands;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.minecraft.commands.builder.CommandBuilder; import net.minecraft.commands.builder.LiteralArgumentBuilder;
import net.minecraft.commands.exceptions.CommandException; import net.minecraft.commands.exceptions.CommandException;
import net.minecraft.commands.exceptions.UnknownCommandException; import net.minecraft.commands.exceptions.UnknownCommandException;
import net.minecraft.commands.tree.LiteralCommandNode; import net.minecraft.commands.tree.LiteralCommandNode;
@@ -11,11 +11,11 @@ import java.util.Map;
public class CommandDispatcher { public class CommandDispatcher {
private final Map<String, LiteralCommandNode> commands = Maps.newHashMap(); private final Map<String, LiteralCommandNode> commands = Maps.newHashMap();
public void register(CommandBuilder command) { public void register(LiteralArgumentBuilder command) {
if (commands.containsKey(command.getName())) { if (commands.containsKey(command.getLiteral())) {
throw new IllegalArgumentException("New command " + command.getName() + " conflicts with existing command " + command.getName()); throw new IllegalArgumentException("New command " + command.getLiteral() + " conflicts with existing command " + command.getLiteral());
} }
commands.put(command.getName(), command.build()); commands.put(command.getLiteral(), command.build());
} }
public void execute(String command) throws CommandException { public void execute(String command) throws CommandException {
@@ -5,17 +5,29 @@ import net.minecraft.commands.tree.CommandNode;
import java.util.List; import java.util.List;
public abstract class ArgumentBuilder { public abstract class ArgumentBuilder<T extends ArgumentBuilder<?>> {
private final List<ArgumentBuilder> arguments = Lists.newArrayList(); private final List<ArgumentBuilder> arguments = Lists.newArrayList();
private Runnable executor;
public ArgumentBuilder then(ArgumentBuilder argument) { protected abstract T getThis();
public T then(ArgumentBuilder argument) {
arguments.add(argument); arguments.add(argument);
return this; return getThis();
} }
public List<ArgumentBuilder> getArguments() { public List<ArgumentBuilder> getArguments() {
return arguments; return arguments;
} }
public T executes(Runnable executor) {
this.executor = executor;
return getThis();
}
public Runnable getExecutor() {
return executor;
}
public abstract CommandNode build(); public abstract CommandNode build();
} }
@@ -1,52 +0,0 @@
package net.minecraft.commands.builder;
import com.google.common.collect.Lists;
import net.minecraft.commands.tree.LiteralCommandNode;
import java.util.List;
public class CommandBuilder {
private final String name;
private final List<ArgumentBuilder> arguments = Lists.newArrayList();
private Runnable executor;
protected CommandBuilder(String name) {
this.name = name;
}
public static CommandBuilder command(String name) {
return new CommandBuilder(name);
}
public String getName() {
return name;
}
public CommandBuilder executes(Runnable executor) {
this.executor = executor;
return this;
}
public Runnable getExecutor() {
return executor;
}
public CommandBuilder then(ArgumentBuilder argument) {
arguments.add(argument);
return this;
}
public List<ArgumentBuilder> getArguments() {
return arguments;
}
public LiteralCommandNode build() {
LiteralCommandNode result = new LiteralCommandNode(getName(), getExecutor());
for (ArgumentBuilder argument : arguments) {
result.addChild(argument.build());
}
return result;
}
}
@@ -0,0 +1,35 @@
package net.minecraft.commands.builder;
import net.minecraft.commands.tree.LiteralCommandNode;
public class LiteralArgumentBuilder extends ArgumentBuilder<LiteralArgumentBuilder> {
private final String literal;
protected LiteralArgumentBuilder(String literal) {
this.literal = literal;
}
public static LiteralArgumentBuilder literal(String name) {
return new LiteralArgumentBuilder(name);
}
@Override
protected LiteralArgumentBuilder getThis() {
return this;
}
public String getLiteral() {
return literal;
}
@Override
public LiteralCommandNode build() {
LiteralCommandNode result = new LiteralCommandNode(getLiteral(), getExecutor());
for (ArgumentBuilder argument : getArguments()) {
result.addChild(argument.build());
}
return result;
}
}
@@ -3,7 +3,7 @@ package net.minecraft.commands.builder;
import net.minecraft.commands.arguments.CommandArgumentType; import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.tree.ArgumentCommandNode; import net.minecraft.commands.tree.ArgumentCommandNode;
public class RequiredArgumentBuilder<T> extends ArgumentBuilder { public class RequiredArgumentBuilder<T> extends ArgumentBuilder<RequiredArgumentBuilder<T>> {
private final String name; private final String name;
private final CommandArgumentType<T> type; private final CommandArgumentType<T> type;
@@ -16,6 +16,11 @@ public class RequiredArgumentBuilder<T> extends ArgumentBuilder {
return new RequiredArgumentBuilder<T>(name, type); return new RequiredArgumentBuilder<T>(name, type);
} }
@Override
protected RequiredArgumentBuilder<T> getThis() {
return this;
}
public CommandArgumentType<T> getType() { public CommandArgumentType<T> getType() {
return type; return type;
} }
@@ -7,7 +7,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static net.minecraft.commands.builder.CommandBuilder.command; import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@@ -22,13 +22,13 @@ public class CommandDispatcherTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testDuplicateCommand() throws Exception { public void testDuplicateCommand() throws Exception {
subject.register(command("foo").executes(runnable)); subject.register(literal("foo").executes(runnable));
subject.register(command("foo").executes(runnable)); subject.register(literal("foo").executes(runnable));
} }
@Test @Test
public void testCreateAndExecuteCommand() throws Exception { public void testCreateAndExecuteCommand() throws Exception {
subject.register(command("foo").executes(runnable)); subject.register(literal("foo").executes(runnable));
subject.execute("foo"); subject.execute("foo");
verify(runnable).run(); verify(runnable).run();
@@ -11,16 +11,11 @@ import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
public class ArgumentBuilderTest { public class ArgumentBuilderTest {
ArgumentBuilder builder; TestableArgumentBuilder builder;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
builder = new ArgumentBuilder() { builder = new TestableArgumentBuilder();
@Override
public CommandNode build() {
return null;
}
};
} }
@Test @Test
@@ -32,4 +27,16 @@ public class ArgumentBuilderTest {
assertThat(builder.getArguments(), hasSize(1)); assertThat(builder.getArguments(), hasSize(1));
assertThat(builder.getArguments(), hasItems((ArgumentBuilder) argument)); assertThat(builder.getArguments(), hasItems((ArgumentBuilder) argument));
} }
private static class TestableArgumentBuilder extends ArgumentBuilder<TestableArgumentBuilder> {
@Override
protected TestableArgumentBuilder getThis() {
return this;
}
@Override
public CommandNode build() {
return null;
}
}
} }
@@ -6,27 +6,16 @@ import org.junit.Test;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; 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.hasSize;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
public class CommandBuilderTest { public class LiteralArgumentBuilderTest {
CommandBuilder builder; LiteralArgumentBuilder builder;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
builder = new CommandBuilder("foo"); builder = new LiteralArgumentBuilder("foo");
}
@Test
public void testArguments() throws Exception {
RequiredArgumentBuilder argument = argument("bar", integer());
builder.then(argument);
assertThat(builder.getArguments(), hasSize(1));
assertThat(builder.getArguments(), hasItems((ArgumentBuilder) argument));
} }
@Test @Test
@@ -5,7 +5,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static net.minecraft.commands.builder.CommandBuilder.command; import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -15,7 +15,7 @@ public class LiteralCommandNodeTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
node = command("foo").build(); node = literal("foo").build();
} }
@Test @Test