Added ArgumentValidationException as separate from illegal syntax

This commit is contained in:
Nathan Adams 2014-09-24 11:30:13 +02:00
parent 0a71730bdc
commit 9585aaf0b3
11 changed files with 44 additions and 34 deletions

View file

@ -1,9 +1,10 @@
package net.minecraft.commands.arguments; package net.minecraft.commands.arguments;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public interface CommandArgumentType<T> { public interface CommandArgumentType<T> {
CommandArgumentParseResult<T> parse(String command) throws IllegalCommandArgumentException; CommandArgumentParseResult<T> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException;
class CommandArgumentParseResult<T> { class CommandArgumentParseResult<T> {
private final String raw; private final String raw;

View file

@ -1,7 +1,8 @@
package net.minecraft.commands.arguments; package net.minecraft.commands.arguments;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public class IntegerArgumentType implements CommandArgumentType<Integer> { public class IntegerArgumentType implements CommandArgumentType<Integer> {
private static final Splitter SPLITTER = Splitter.on(' ').limit(2); private static final Splitter SPLITTER = Splitter.on(' ').limit(2);
@ -27,22 +28,22 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
} }
@Override @Override
public CommandArgumentParseResult<Integer> parse(String command) throws IllegalCommandArgumentException { public CommandArgumentParseResult<Integer> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
String raw = SPLITTER.split(command).iterator().next(); String raw = SPLITTER.split(command).iterator().next();
try { try {
int value = Integer.parseInt(raw); int value = Integer.parseInt(raw);
if (value < minimum) { if (value < minimum) {
throw new IllegalCommandArgumentException(); throw new ArgumentValidationException();
} }
if (value > maximum) { if (value > maximum) {
throw new IllegalCommandArgumentException(); throw new ArgumentValidationException();
} }
return new CommandArgumentParseResult<Integer>(raw, value); return new CommandArgumentParseResult<Integer>(raw, value);
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
throw new IllegalCommandArgumentException(); throw new IllegalArgumentSyntaxException();
} }
} }
} }

View file

@ -0,0 +1,4 @@
package net.minecraft.commands.exceptions;
public class ArgumentValidationException extends CommandException {
}

View file

@ -0,0 +1,4 @@
package net.minecraft.commands.exceptions;
public class IllegalArgumentSyntaxException extends CommandException {
}

View file

@ -1,4 +0,0 @@
package net.minecraft.commands.exceptions;
public class IllegalCommandArgumentException extends CommandException {
}

View file

@ -1,7 +1,8 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import net.minecraft.commands.arguments.CommandArgumentType; import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public class ArgumentCommandNode<T> extends CommandNode { public class ArgumentCommandNode<T> extends CommandNode {
private final String name; private final String name;
@ -21,7 +22,7 @@ public class ArgumentCommandNode<T> extends CommandNode {
} }
@Override @Override
public CommandNode parse(String command) throws IllegalCommandArgumentException { public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
CommandArgumentType.CommandArgumentParseResult<T> parsed = type.parse(command); CommandArgumentType.CommandArgumentParseResult<T> parsed = type.parse(command);
int start = parsed.getRaw().length() + 1; int start = parsed.getRaw().length() + 1;
@ -31,11 +32,11 @@ public class ArgumentCommandNode<T> extends CommandNode {
for (CommandNode node : getChildren()) { for (CommandNode node : getChildren()) {
try { try {
return node.parse(result); return node.parse(result);
} catch (IllegalCommandArgumentException ignored) { } catch (IllegalArgumentSyntaxException ignored) {
} }
} }
throw new IllegalCommandArgumentException(); throw new IllegalArgumentSyntaxException();
} else { } else {
return this; return this;
} }

View file

@ -1,7 +1,8 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import java.util.List; import java.util.List;
@ -16,5 +17,5 @@ public abstract class CommandNode {
children.add(node); children.add(node);
} }
public abstract CommandNode parse(String command) throws IllegalCommandArgumentException; public abstract CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException;
} }

View file

@ -1,6 +1,7 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public class LiteralCommandNode extends CommandNode { public class LiteralCommandNode extends CommandNode {
private final String literal; private final String literal;
@ -20,7 +21,7 @@ public class LiteralCommandNode extends CommandNode {
} }
@Override @Override
public CommandNode parse(String command) throws IllegalCommandArgumentException { public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
if (command.startsWith(literal)) { if (command.startsWith(literal)) {
int start = literal.length() + 1; int start = literal.length() + 1;
@ -30,16 +31,16 @@ public class LiteralCommandNode extends CommandNode {
for (CommandNode node : getChildren()) { for (CommandNode node : getChildren()) {
try { try {
return node.parse(result); return node.parse(result);
} catch (IllegalCommandArgumentException ignored) { } catch (IllegalArgumentSyntaxException ignored) {
} }
} }
throw new IllegalCommandArgumentException(); throw new IllegalArgumentSyntaxException();
} else { } else {
return this; return this;
} }
} else { } else {
throw new IllegalCommandArgumentException(); throw new IllegalArgumentSyntaxException();
} }
} }
} }

View file

@ -1,6 +1,7 @@
package net.minecraft.commands.arguments; package net.minecraft.commands.arguments;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -24,12 +25,12 @@ public class IntegerArgumentTypeTest {
assertThat(result.getResult(), is(50)); assertThat(result.getResult(), is(50));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalArgumentSyntaxException.class)
public void testParseInvalid() throws Exception { public void testParseInvalid() throws Exception {
type.parse("fifty"); type.parse("fifty");
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = ArgumentValidationException.class)
public void testParseTooLow() throws Exception { public void testParseTooLow() throws Exception {
type.parse("-101"); type.parse("-101");
} }
@ -42,7 +43,7 @@ public class IntegerArgumentTypeTest {
assertThat(result.getResult(), is(-100)); assertThat(result.getResult(), is(-100));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = ArgumentValidationException.class)
public void testParseTooHigh() throws Exception { public void testParseTooHigh() throws Exception {
type.parse("101"); type.parse("101");
} }

View file

@ -1,6 +1,6 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -22,7 +22,7 @@ public class ArgumentCommandNodeTest {
assertThat((ArgumentCommandNode) node.parse("123"), is(node)); assertThat((ArgumentCommandNode) node.parse("123"), is(node));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalArgumentSyntaxException.class)
public void testParseInvalid() throws Exception { public void testParseInvalid() throws Exception {
node.parse("bar"); node.parse("bar");
} }
@ -36,14 +36,14 @@ public class ArgumentCommandNodeTest {
assertThat(node.parse("123 123"), is(child)); assertThat(node.parse("123 123"), is(child));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalArgumentSyntaxException.class)
public void testParseInvalidChild() throws Exception { public void testParseInvalidChild() throws Exception {
node.addChild(argument("bar", integer()).build()); node.addChild(argument("bar", integer()).build());
node.parse("123 bar"); node.parse("123 bar");
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalArgumentSyntaxException.class)
public void testParseNoChildren() throws Exception { public void testParseNoChildren() throws Exception {
node.parse("123 123"); node.parse("123 123");
} }

View file

@ -1,6 +1,6 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -23,7 +23,7 @@ public class LiteralCommandNodeTest {
assertThat((LiteralCommandNode) node.parse("foo"), is(node)); assertThat((LiteralCommandNode) node.parse("foo"), is(node));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalArgumentSyntaxException.class)
public void testParseInvalid() throws Exception { public void testParseInvalid() throws Exception {
node.parse("bar"); node.parse("bar");
} }
@ -37,14 +37,14 @@ public class LiteralCommandNodeTest {
assertThat(node.parse("foo 123"), is(child)); assertThat(node.parse("foo 123"), is(child));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalArgumentSyntaxException.class)
public void testParseInvalidChild() throws Exception { public void testParseInvalidChild() throws Exception {
node.addChild(argument("bar", integer()).build()); node.addChild(argument("bar", integer()).build());
node.parse("foo bar"); node.parse("foo bar");
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalArgumentSyntaxException.class)
public void testParseNoChildren() throws Exception { public void testParseNoChildren() throws Exception {
node.parse("foo 123"); node.parse("foo 123");
} }