Just use a regular builder
This commit is contained in:
parent
c73607ba36
commit
dadedbb322
4 changed files with 28 additions and 72 deletions
|
@ -10,16 +10,11 @@ import java.util.Map;
|
|||
public class CommandDispatcher {
|
||||
private final Map<String, Runnable> commands = Maps.newHashMap();
|
||||
|
||||
public CommandBuilder createCommand(final String name) {
|
||||
return new CommandBuilder() {
|
||||
@Override
|
||||
public void onFinish() {
|
||||
if (commands.containsKey(name)) {
|
||||
throw new IllegalArgumentException("New command " + name + " conflicts with existing command " + name);
|
||||
}
|
||||
commands.put(name, getCommandExecutor());
|
||||
}
|
||||
};
|
||||
public void register(CommandBuilder command) {
|
||||
if (commands.containsKey(command.getName())) {
|
||||
throw new IllegalArgumentException("New command " + command.getName() + " conflicts with existing command " + command.getName());
|
||||
}
|
||||
commands.put(command.getName(), command.getExecutor());
|
||||
}
|
||||
|
||||
public void execute(String command) throws CommandException {
|
||||
|
|
|
@ -1,28 +1,27 @@
|
|||
package net.minecraft.commands.builder;
|
||||
|
||||
public abstract class CommandBuilder {
|
||||
private boolean finished;
|
||||
private Runnable commandExecutor;
|
||||
public class CommandBuilder {
|
||||
private final String name;
|
||||
private Runnable executor;
|
||||
|
||||
public void finish() {
|
||||
if (finished) {
|
||||
throw new IllegalStateException("Cannot finish() multiple times!");
|
||||
}
|
||||
if (commandExecutor == null) {
|
||||
throw new IllegalStateException("Cannot finish() without a command executor!");
|
||||
}
|
||||
onFinish();
|
||||
finished = true;
|
||||
protected CommandBuilder(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
protected abstract void onFinish();
|
||||
public static CommandBuilder command(String name) {
|
||||
return new CommandBuilder(name);
|
||||
}
|
||||
|
||||
public CommandBuilder executes(Runnable runnable) {
|
||||
this.commandExecutor = runnable;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public CommandBuilder executes(Runnable executor) {
|
||||
this.executor = executor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Runnable getCommandExecutor() {
|
||||
return commandExecutor;
|
||||
public Runnable getExecutor() {
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,9 +5,11 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import static net.minecraft.commands.builder.CommandBuilder.command;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandDispatcherTest {
|
||||
CommandDispatcher subject;
|
||||
|
@ -20,16 +22,16 @@ public class CommandDispatcherTest {
|
|||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDuplicateCommand() throws Exception {
|
||||
subject.createCommand("foo").executes(runnable).finish();
|
||||
subject.createCommand("foo").executes(runnable).finish();
|
||||
subject.register(command("foo").executes(runnable));
|
||||
subject.register(command("foo").executes(runnable));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAndExecuteCommand() throws Exception {
|
||||
subject.createCommand("foo").executes(runnable).finish();
|
||||
subject.register(command("foo").executes(runnable));
|
||||
|
||||
subject.execute("foo");
|
||||
Mockito.verify(runnable).run();
|
||||
verify(runnable).run();
|
||||
}
|
||||
|
||||
@Test(expected = UnknownCommandException.class)
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
package net.minecraft.commands.builder;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandBuilderTest {
|
||||
@Mock(answer = Answers.CALLS_REAL_METHODS) CommandBuilder builder;
|
||||
@Mock Runnable commandExecutor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Mockito.doNothing().when(builder).onFinish();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinish() throws Exception {
|
||||
builder.executes(commandExecutor);
|
||||
builder.finish();
|
||||
|
||||
Mockito.verify(builder).onFinish();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testFinishTwice() throws Exception {
|
||||
builder.executes(commandExecutor);
|
||||
builder.finish();
|
||||
builder.finish();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testFinishWithoutCommandExecutor() throws Exception {
|
||||
builder.finish();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue