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

View file

@ -1,7 +1,7 @@
package net.minecraft.commands;
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.UnknownCommandException;
import net.minecraft.commands.tree.LiteralCommandNode;
@ -11,11 +11,11 @@ import java.util.Map;
public class CommandDispatcher {
private final Map<String, LiteralCommandNode> commands = Maps.newHashMap();
public void register(CommandBuilder command) {
if (commands.containsKey(command.getName())) {
throw new IllegalArgumentException("New command " + command.getName() + " conflicts with existing command " + command.getName());
public void register(LiteralArgumentBuilder command) {
if (commands.containsKey(command.getLiteral())) {
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 {

View file

@ -5,17 +5,29 @@ import net.minecraft.commands.tree.CommandNode;
import java.util.List;
public abstract class ArgumentBuilder {
public abstract class ArgumentBuilder<T extends ArgumentBuilder<?>> {
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);
return this;
return getThis();
}
public List<ArgumentBuilder> getArguments() {
return arguments;
}
public T executes(Runnable executor) {
this.executor = executor;
return getThis();
}
public Runnable getExecutor() {
return executor;
}
public abstract CommandNode build();
}

View file

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

View file

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

View file

@ -3,7 +3,7 @@ package net.minecraft.commands.builder;
import net.minecraft.commands.arguments.CommandArgumentType;
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 CommandArgumentType<T> type;
@ -16,6 +16,11 @@ public class RequiredArgumentBuilder<T> extends ArgumentBuilder {
return new RequiredArgumentBuilder<T>(name, type);
}
@Override
protected RequiredArgumentBuilder<T> getThis() {
return this;
}
public CommandArgumentType<T> getType() {
return type;
}

View file

@ -7,7 +7,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
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;
@RunWith(MockitoJUnitRunner.class)
@ -22,13 +22,13 @@ public class CommandDispatcherTest {
@Test(expected = IllegalArgumentException.class)
public void testDuplicateCommand() throws Exception {
subject.register(command("foo").executes(runnable));
subject.register(command("foo").executes(runnable));
subject.register(literal("foo").executes(runnable));
subject.register(literal("foo").executes(runnable));
}
@Test
public void testCreateAndExecuteCommand() throws Exception {
subject.register(command("foo").executes(runnable));
subject.register(literal("foo").executes(runnable));
subject.execute("foo");
verify(runnable).run();

View file

@ -11,16 +11,11 @@ import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
public class ArgumentBuilderTest {
ArgumentBuilder builder;
TestableArgumentBuilder builder;
@Before
public void setUp() throws Exception {
builder = new ArgumentBuilder() {
@Override
public CommandNode build() {
return null;
}
};
builder = new TestableArgumentBuilder();
}
@Test
@ -32,4 +27,16 @@ public class ArgumentBuilderTest {
assertThat(builder.getArguments(), hasSize(1));
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;
}
}
}

View file

@ -6,27 +6,16 @@ import org.junit.Test;
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 {
CommandBuilder builder;
public class LiteralArgumentBuilderTest {
LiteralArgumentBuilder builder;
@Before
public void setUp() throws Exception {
builder = new CommandBuilder("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));
builder = new LiteralArgumentBuilder("foo");
}
@Test

View file

@ -5,7 +5,7 @@ import org.junit.Before;
import org.junit.Test;
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 org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@ -15,7 +15,7 @@ public class LiteralCommandNodeTest {
@Before
public void setUp() throws Exception {
node = command("foo").build();
node = literal("foo").build();
}
@Test