From 81ed5f0521cce76a590a48e63d62e0df78f726b2 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Wed, 24 Sep 2014 13:53:23 +0200 Subject: [PATCH] Moved CommandArgumentParseResult to its own class --- .../arguments/CommandArgumentType.java | 21 ++----------------- .../arguments/IntegerArgumentType.java | 5 +++-- .../commands/context/CommandContext.java | 11 +++++----- .../context/CommandContextBuilder.java | 5 ++--- .../commands/context/ParsedArgument.java | 19 +++++++++++++++++ .../commands/tree/ArgumentCommandNode.java | 3 ++- .../arguments/IntegerArgumentTypeTest.java | 7 ++++--- 7 files changed, 37 insertions(+), 34 deletions(-) create mode 100644 src/main/java/net/minecraft/commands/context/ParsedArgument.java diff --git a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java index 875f51f..108f493 100644 --- a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java @@ -1,26 +1,9 @@ package net.minecraft.commands.arguments; +import net.minecraft.commands.context.ParsedArgument; import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; public interface CommandArgumentType { - CommandArgumentParseResult parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException; - - class CommandArgumentParseResult { - private final String raw; - private final T result; - - public CommandArgumentParseResult(String raw, T result) { - this.raw = raw; - this.result = result; - } - - public String getRaw() { - return raw; - } - - public T getResult() { - return result; - } - } + ParsedArgument parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException; } diff --git a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java index 76e75b2..f6821f3 100644 --- a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java @@ -2,6 +2,7 @@ package net.minecraft.commands.arguments; import com.google.common.base.Splitter; import net.minecraft.commands.context.CommandContext; +import net.minecraft.commands.context.ParsedArgument; import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; @@ -33,7 +34,7 @@ public class IntegerArgumentType implements CommandArgumentType { } @Override - public CommandArgumentParseResult parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException { + public ParsedArgument parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException { String raw = SPLITTER.split(command).iterator().next(); try { @@ -46,7 +47,7 @@ public class IntegerArgumentType implements CommandArgumentType { throw new ArgumentValidationException(); } - return new CommandArgumentParseResult(raw, value); + return new ParsedArgument(raw, value); } catch (NumberFormatException ignored) { throw new IllegalArgumentSyntaxException(); } diff --git a/src/main/java/net/minecraft/commands/context/CommandContext.java b/src/main/java/net/minecraft/commands/context/CommandContext.java index b09474f..efda5f2 100644 --- a/src/main/java/net/minecraft/commands/context/CommandContext.java +++ b/src/main/java/net/minecraft/commands/context/CommandContext.java @@ -1,27 +1,26 @@ package net.minecraft.commands.context; import com.google.common.primitives.Primitives; -import net.minecraft.commands.arguments.CommandArgumentType; import java.util.Map; public class CommandContext { - private final Map> arguments; + private final Map> arguments; - public CommandContext(Map> arguments) { + public CommandContext(Map> arguments) { this.arguments = arguments; } @SuppressWarnings("unchecked") - public CommandArgumentType.CommandArgumentParseResult getArgument(String name, Class clazz) { - CommandArgumentType.CommandArgumentParseResult argument = arguments.get(name); + public ParsedArgument getArgument(String name, Class clazz) { + ParsedArgument argument = arguments.get(name); if (argument == null) { throw new IllegalArgumentException("No such argument '" + name + "' exists on this command"); } if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) { - return (CommandArgumentType.CommandArgumentParseResult) argument; + return (ParsedArgument) argument; } else { throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz); } diff --git a/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java b/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java index a614380..2051ae2 100644 --- a/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java +++ b/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java @@ -1,17 +1,16 @@ package net.minecraft.commands.context; import com.google.common.collect.Maps; -import net.minecraft.commands.arguments.CommandArgumentType; import java.util.Map; public class CommandContextBuilder { - private final Map> arguments = Maps.newHashMap(); + private final Map> arguments = Maps.newHashMap(); public CommandContextBuilder() { } - public CommandContextBuilder withArgument(String name, CommandArgumentType.CommandArgumentParseResult argument) { + public CommandContextBuilder withArgument(String name, ParsedArgument argument) { this.arguments.put(name, argument); return this; } diff --git a/src/main/java/net/minecraft/commands/context/ParsedArgument.java b/src/main/java/net/minecraft/commands/context/ParsedArgument.java new file mode 100644 index 0000000..d77a19c --- /dev/null +++ b/src/main/java/net/minecraft/commands/context/ParsedArgument.java @@ -0,0 +1,19 @@ +package net.minecraft.commands.context; + +public class ParsedArgument { + private final String raw; + private final T result; + + public ParsedArgument(String raw, T result) { + this.raw = raw; + this.result = result; + } + + public String getRaw() { + return raw; + } + + public T getResult() { + return result; + } +} diff --git a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java index 4d8af85..dd583ca 100644 --- a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java @@ -1,6 +1,7 @@ package net.minecraft.commands.tree; import net.minecraft.commands.arguments.CommandArgumentType; +import net.minecraft.commands.context.ParsedArgument; import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; @@ -24,7 +25,7 @@ public class ArgumentCommandNode extends CommandNode { @Override public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException { - CommandArgumentType.CommandArgumentParseResult parsed = type.parse(command); + ParsedArgument parsed = type.parse(command); int start = parsed.getRaw().length() + 1; if (start < command.length()) { diff --git a/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java b/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java index d3f24fc..a61e085 100644 --- a/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java +++ b/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java @@ -2,6 +2,7 @@ package net.minecraft.commands.arguments; import net.minecraft.commands.context.CommandContext; import net.minecraft.commands.context.CommandContextBuilder; +import net.minecraft.commands.context.ParsedArgument; import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; import org.junit.Before; @@ -21,7 +22,7 @@ public class IntegerArgumentTypeTest { @Test public void testParse() throws Exception { - CommandArgumentType.CommandArgumentParseResult result = type.parse("50"); + ParsedArgument result = type.parse("50"); assertThat(result.getRaw(), is("50")); assertThat(result.getResult(), is(50)); @@ -39,7 +40,7 @@ public class IntegerArgumentTypeTest { @Test public void testParseLowerLimit() throws Exception { - CommandArgumentType.CommandArgumentParseResult result = type.parse("-100"); + ParsedArgument result = type.parse("-100"); assertThat(result.getRaw(), is("-100")); assertThat(result.getResult(), is(-100)); @@ -52,7 +53,7 @@ public class IntegerArgumentTypeTest { @Test public void testParseHigherLimit() throws Exception { - CommandArgumentType.CommandArgumentParseResult result = type.parse("100"); + ParsedArgument result = type.parse("100"); assertThat(result.getRaw(), is("100")); assertThat(result.getResult(), is(100));