From 934ba3336fa55de093ad4780073c73287c582057 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Wed, 17 Sep 2014 13:00:41 +0200 Subject: [PATCH] Added commandbuilder.then() and argumentbuilder --- .../arguments/CommandArgumentType.java | 4 +++ .../arguments/IntegerArgumentType.java | 23 +++++++++++++++ .../commands/arguments/package-info.java | 4 +++ .../commands/builder/ArgumentBuilder.java | 18 ++++++++++++ .../commands/builder/CommandBuilder.java | 14 +++++++++ .../builder/RequiredArgumentBuilder.java | 17 +++++++++++ .../commands/builder/ArgumentBuilderTest.java | 29 +++++++++++++++++++ .../commands/builder/CommandBuilderTest.java | 29 +++++++++++++++++++ 8 files changed, 138 insertions(+) create mode 100644 src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java create mode 100644 src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java create mode 100644 src/main/java/net/minecraft/commands/arguments/package-info.java create mode 100644 src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java create mode 100644 src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java create mode 100644 src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java create mode 100644 src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java diff --git a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java new file mode 100644 index 0000000..282074c --- /dev/null +++ b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java @@ -0,0 +1,4 @@ +package net.minecraft.commands.arguments; + +public interface CommandArgumentType { +} diff --git a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java new file mode 100644 index 0000000..72bad36 --- /dev/null +++ b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java @@ -0,0 +1,23 @@ +package net.minecraft.commands.arguments; + +public class IntegerArgumentType implements CommandArgumentType { + private final int minimum; + private final int maximum; + + protected IntegerArgumentType(int minimum, int maximum) { + this.minimum = minimum; + this.maximum = maximum; + } + + public static IntegerArgumentType integer() { + return integer(Integer.MIN_VALUE); + } + + public static IntegerArgumentType integer(int min) { + return integer(min, Integer.MAX_VALUE); + } + + public static IntegerArgumentType integer(int min, int max) { + return new IntegerArgumentType(min, max); + } +} diff --git a/src/main/java/net/minecraft/commands/arguments/package-info.java b/src/main/java/net/minecraft/commands/arguments/package-info.java new file mode 100644 index 0000000..8b993d7 --- /dev/null +++ b/src/main/java/net/minecraft/commands/arguments/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package net.minecraft.commands.arguments; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java b/src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java new file mode 100644 index 0000000..f273a75 --- /dev/null +++ b/src/main/java/net/minecraft/commands/builder/ArgumentBuilder.java @@ -0,0 +1,18 @@ +package net.minecraft.commands.builder; + +import com.google.common.collect.Lists; + +import java.util.List; + +public abstract class ArgumentBuilder { + private final List arguments = Lists.newArrayList(); + + public ArgumentBuilder then(ArgumentBuilder argument) { + arguments.add(argument); + return this; + } + + public List getArguments() { + return arguments; + } +} diff --git a/src/main/java/net/minecraft/commands/builder/CommandBuilder.java b/src/main/java/net/minecraft/commands/builder/CommandBuilder.java index 411e9b3..7d3ec2a 100644 --- a/src/main/java/net/minecraft/commands/builder/CommandBuilder.java +++ b/src/main/java/net/minecraft/commands/builder/CommandBuilder.java @@ -1,7 +1,12 @@ package net.minecraft.commands.builder; +import com.google.common.collect.Lists; + +import java.util.List; + public class CommandBuilder { private final String name; + private final List arguments = Lists.newArrayList(); private Runnable executor; protected CommandBuilder(String name) { @@ -24,4 +29,13 @@ public class CommandBuilder { public Runnable getExecutor() { return executor; } + + public CommandBuilder then(ArgumentBuilder argument) { + arguments.add(argument); + return this; + } + + public List getArguments() { + return arguments; + } } diff --git a/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java b/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java new file mode 100644 index 0000000..2da607c --- /dev/null +++ b/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java @@ -0,0 +1,17 @@ +package net.minecraft.commands.builder; + +import net.minecraft.commands.arguments.CommandArgumentType; + +public class RequiredArgumentBuilder extends ArgumentBuilder { + private final String name; + private final CommandArgumentType type; + + protected RequiredArgumentBuilder(String name, CommandArgumentType type) { + this.name = name; + this.type = type; + } + + public static RequiredArgumentBuilder argument(String name, CommandArgumentType type) { + return new RequiredArgumentBuilder(name, type); + } +} diff --git a/src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java b/src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java new file mode 100644 index 0000000..4c8dca4 --- /dev/null +++ b/src/main/test/net/minecraft/commands/builder/ArgumentBuilderTest.java @@ -0,0 +1,29 @@ +package net.minecraft.commands.builder; + +import org.junit.Before; +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.junit.Assert.assertThat; + +public class ArgumentBuilderTest { + ArgumentBuilder builder; + + @Before + public void setUp() throws Exception { + builder = new ArgumentBuilder() {}; + } + + @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)); + } +} \ 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..a62d6a9 --- /dev/null +++ b/src/main/test/net/minecraft/commands/builder/CommandBuilderTest.java @@ -0,0 +1,29 @@ +package net.minecraft.commands.builder; + +import org.junit.Before; +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.junit.Assert.assertThat; + +public class CommandBuilderTest { + CommandBuilder 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)); + } +} \ No newline at end of file