Super simple start
This commit is contained in:
parent
4801ab8dc9
commit
c73607ba36
9 changed files with 160 additions and 0 deletions
33
src/main/java/net/minecraft/commands/CommandDispatcher.java
Normal file
33
src/main/java/net/minecraft/commands/CommandDispatcher.java
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package net.minecraft.commands;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import net.minecraft.commands.builder.CommandBuilder;
|
||||||
|
import net.minecraft.commands.exceptions.CommandException;
|
||||||
|
import net.minecraft.commands.exceptions.UnknownCommandException;
|
||||||
|
|
||||||
|
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 execute(String command) throws CommandException {
|
||||||
|
Runnable runnable = commands.get(command);
|
||||||
|
if (runnable == null) {
|
||||||
|
throw new UnknownCommandException();
|
||||||
|
}
|
||||||
|
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package net.minecraft.commands.builder;
|
||||||
|
|
||||||
|
public abstract class CommandBuilder {
|
||||||
|
private boolean finished;
|
||||||
|
private Runnable commandExecutor;
|
||||||
|
|
||||||
|
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 abstract void onFinish();
|
||||||
|
|
||||||
|
public CommandBuilder executes(Runnable runnable) {
|
||||||
|
this.commandExecutor = runnable;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Runnable getCommandExecutor() {
|
||||||
|
return commandExecutor;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
@ParametersAreNonnullByDefault
|
||||||
|
package net.minecraft.commands.builder;
|
||||||
|
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
|
@ -0,0 +1,4 @@
|
||||||
|
package net.minecraft.commands.exceptions;
|
||||||
|
|
||||||
|
public class CommandException extends Exception {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package net.minecraft.commands.exceptions;
|
||||||
|
|
||||||
|
public class UnknownCommandException extends CommandException {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
@ParametersAreNonnullByDefault
|
||||||
|
package net.minecraft.commands.exceptions;
|
||||||
|
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
4
src/main/java/net/minecraft/commands/package-info.java
Normal file
4
src/main/java/net/minecraft/commands/package-info.java
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
@ParametersAreNonnullByDefault
|
||||||
|
package net.minecraft.commands;
|
||||||
|
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
|
@ -0,0 +1,39 @@
|
||||||
|
package net.minecraft.commands;
|
||||||
|
|
||||||
|
import net.minecraft.commands.exceptions.UnknownCommandException;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class CommandDispatcherTest {
|
||||||
|
CommandDispatcher subject;
|
||||||
|
@Mock Runnable runnable;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
subject = new CommandDispatcher();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testDuplicateCommand() throws Exception {
|
||||||
|
subject.createCommand("foo").executes(runnable).finish();
|
||||||
|
subject.createCommand("foo").executes(runnable).finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateAndExecuteCommand() throws Exception {
|
||||||
|
subject.createCommand("foo").executes(runnable).finish();
|
||||||
|
|
||||||
|
subject.execute("foo");
|
||||||
|
Mockito.verify(runnable).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnknownCommandException.class)
|
||||||
|
public void testExecuteUnknownCommand() throws Exception {
|
||||||
|
subject.execute("foo");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
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