From c73607ba366c3c96db41c17b2ff0053f45190844 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Tue, 16 Sep 2014 12:08:15 +0200 Subject: [PATCH] Super simple start --- .../minecraft/commands/CommandDispatcher.java | 33 +++++++++++++++ .../commands/builder/CommandBuilder.java | 28 +++++++++++++ .../commands/builder/package-info.java | 4 ++ .../commands/exceptions/CommandException.java | 4 ++ .../exceptions/UnknownCommandException.java | 4 ++ .../commands/exceptions/package-info.java | 4 ++ .../net/minecraft/commands/package-info.java | 4 ++ .../commands/CommandDispatcherTest.java | 39 ++++++++++++++++++ .../commands/builder/CommandBuilderTest.java | 40 +++++++++++++++++++ 9 files changed, 160 insertions(+) create mode 100644 src/main/java/net/minecraft/commands/CommandDispatcher.java create mode 100644 src/main/java/net/minecraft/commands/builder/CommandBuilder.java create mode 100644 src/main/java/net/minecraft/commands/builder/package-info.java create mode 100644 src/main/java/net/minecraft/commands/exceptions/CommandException.java create mode 100644 src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java create mode 100644 src/main/java/net/minecraft/commands/exceptions/package-info.java create mode 100644 src/main/java/net/minecraft/commands/package-info.java create mode 100644 src/main/test/net/minecraft/commands/CommandDispatcherTest.java create mode 100644 src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java diff --git a/src/main/java/net/minecraft/commands/CommandDispatcher.java b/src/main/java/net/minecraft/commands/CommandDispatcher.java new file mode 100644 index 0000000..4ec8b1f --- /dev/null +++ b/src/main/java/net/minecraft/commands/CommandDispatcher.java @@ -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 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(); + } +} diff --git a/src/main/java/net/minecraft/commands/builder/CommandBuilder.java b/src/main/java/net/minecraft/commands/builder/CommandBuilder.java new file mode 100644 index 0000000..92f3d3d --- /dev/null +++ b/src/main/java/net/minecraft/commands/builder/CommandBuilder.java @@ -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; + } +} diff --git a/src/main/java/net/minecraft/commands/builder/package-info.java b/src/main/java/net/minecraft/commands/builder/package-info.java new file mode 100644 index 0000000..f6f5bad --- /dev/null +++ b/src/main/java/net/minecraft/commands/builder/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package net.minecraft.commands.builder; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/net/minecraft/commands/exceptions/CommandException.java b/src/main/java/net/minecraft/commands/exceptions/CommandException.java new file mode 100644 index 0000000..44f70c1 --- /dev/null +++ b/src/main/java/net/minecraft/commands/exceptions/CommandException.java @@ -0,0 +1,4 @@ +package net.minecraft.commands.exceptions; + +public class CommandException extends Exception { +} diff --git a/src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java b/src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java new file mode 100644 index 0000000..58f844e --- /dev/null +++ b/src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java @@ -0,0 +1,4 @@ +package net.minecraft.commands.exceptions; + +public class UnknownCommandException extends CommandException { +} diff --git a/src/main/java/net/minecraft/commands/exceptions/package-info.java b/src/main/java/net/minecraft/commands/exceptions/package-info.java new file mode 100644 index 0000000..703ac70 --- /dev/null +++ b/src/main/java/net/minecraft/commands/exceptions/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package net.minecraft.commands.exceptions; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/net/minecraft/commands/package-info.java b/src/main/java/net/minecraft/commands/package-info.java new file mode 100644 index 0000000..2f52d76 --- /dev/null +++ b/src/main/java/net/minecraft/commands/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package net.minecraft.commands; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/CommandDispatcherTest.java b/src/main/test/net/minecraft/commands/CommandDispatcherTest.java new file mode 100644 index 0000000..de8da64 --- /dev/null +++ b/src/main/test/net/minecraft/commands/CommandDispatcherTest.java @@ -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"); + } +} \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java b/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java new file mode 100644 index 0000000..5efcde2 --- /dev/null +++ b/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java @@ -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(); + } +} \ No newline at end of file