Removed concept of argument suffix

This commit is contained in:
Nathan Adams 2017-11-06 16:17:21 +01:00
parent c746232ffc
commit e342380600
7 changed files with 9 additions and 158 deletions

View file

@ -11,9 +11,4 @@ public interface ArgumentType<T> {
default <S> void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
}
default String getUsageSuffix() {
return null;
}
}

View file

@ -6,21 +6,16 @@ import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import java.util.Objects;
public class FloatArgumentType implements ArgumentType<Float> {
public static final ParameterizedCommandExceptionType ERROR_WRONG_SUFFIX = new ParameterizedCommandExceptionType("argument.float.wrongsuffix", "Expected suffix '${suffix}'", "suffix");
public static final ParameterizedCommandExceptionType ERROR_TOO_SMALL = new ParameterizedCommandExceptionType("argument.float.low", "Float must not be less than ${minimum}, found ${found}", "found", "minimum");
public static final ParameterizedCommandExceptionType ERROR_TOO_BIG = new ParameterizedCommandExceptionType("argument.float.big", "Float must not be more than ${maximum}, found ${found}", "found", "maximum");
private final float minimum;
private final float maximum;
private final String suffix;
private FloatArgumentType(final float minimum, final float maximum, final String suffix) {
private FloatArgumentType(final float minimum, final float maximum) {
this.minimum = minimum;
this.maximum = maximum;
this.suffix = suffix;
}
public static FloatArgumentType floatArg() {
@ -32,11 +27,7 @@ public class FloatArgumentType implements ArgumentType<Float> {
}
public static FloatArgumentType floatArg(final float min, final float max) {
return floatArg(min, max, "");
}
public static FloatArgumentType floatArg(final float min, final float max, final String suffix) {
return new FloatArgumentType(min, max, suffix);
return new FloatArgumentType(min, max);
}
public static float getFloat(final CommandContext<?> context, final String name) {
@ -47,14 +38,6 @@ public class FloatArgumentType implements ArgumentType<Float> {
public <S> Float parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException {
final int start = reader.getCursor();
final float result = (float) reader.readDouble();
for (int i = 0; i < suffix.length(); i++) {
if (reader.canRead() && reader.peek() == suffix.charAt(i)) {
reader.skip();
} else {
reader.setCursor(start);
throw ERROR_WRONG_SUFFIX.createWithContext(reader, suffix);
}
}
if (result < minimum) {
reader.setCursor(start);
throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum);
@ -72,7 +55,7 @@ public class FloatArgumentType implements ArgumentType<Float> {
if (!(o instanceof FloatArgumentType)) return false;
final FloatArgumentType that = (FloatArgumentType) o;
return maximum == that.maximum && minimum == that.minimum && Objects.equals(suffix, that.suffix);
return maximum == that.maximum && minimum == that.minimum;
}
@Override
@ -90,9 +73,4 @@ public class FloatArgumentType implements ArgumentType<Float> {
return "float(" + minimum + ", " + maximum + ")";
}
}
@Override
public String getUsageSuffix() {
return suffix.length() == 0 ? null : suffix;
}
}

View file

@ -6,21 +6,16 @@ import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import java.util.Objects;
public class IntegerArgumentType implements ArgumentType<Integer> {
public static final ParameterizedCommandExceptionType ERROR_WRONG_SUFFIX = new ParameterizedCommandExceptionType("argument.integer.wrongsuffix", "Expected suffix '${suffix}'", "suffix");
public static final ParameterizedCommandExceptionType ERROR_TOO_SMALL = new ParameterizedCommandExceptionType("argument.integer.low", "Integer must not be less than ${minimum}, found ${found}", "found", "minimum");
public static final ParameterizedCommandExceptionType ERROR_TOO_BIG = new ParameterizedCommandExceptionType("argument.integer.big", "Integer must not be more than ${maximum}, found ${found}", "found", "maximum");
private final int minimum;
private final int maximum;
private final String suffix;
private IntegerArgumentType(final int minimum, final int maximum, final String suffix) {
private IntegerArgumentType(final int minimum, final int maximum) {
this.minimum = minimum;
this.maximum = maximum;
this.suffix = suffix;
}
public static IntegerArgumentType integer() {
@ -32,11 +27,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
}
public static IntegerArgumentType integer(final int min, final int max) {
return integer(min, max, "");
}
public static IntegerArgumentType integer(final int min, final int max, final String suffix) {
return new IntegerArgumentType(min, max, suffix);
return new IntegerArgumentType(min, max);
}
public static int getInteger(final CommandContext<?> context, final String name) {
@ -47,14 +38,6 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
public <S> Integer parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException {
final int start = reader.getCursor();
final int result = reader.readInt();
for (int i = 0; i < suffix.length(); i++) {
if (reader.canRead() && reader.peek() == suffix.charAt(i)) {
reader.skip();
} else {
reader.setCursor(start);
throw ERROR_WRONG_SUFFIX.createWithContext(reader, suffix);
}
}
if (result < minimum) {
reader.setCursor(start);
throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum);
@ -72,7 +55,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
if (!(o instanceof IntegerArgumentType)) return false;
final IntegerArgumentType that = (IntegerArgumentType) o;
return maximum == that.maximum && minimum == that.minimum && Objects.equals(suffix, that.suffix);
return maximum == that.maximum && minimum == that.minimum;
}
@Override
@ -90,9 +73,4 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
return "integer(" + minimum + ", " + maximum + ")";
}
}
@Override
public String getUsageSuffix() {
return suffix.length() == 0 ? null : suffix;
}
}

View file

@ -43,11 +43,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
@Override
public String getUsageText() {
String usage = USAGE_ARGUMENT_OPEN + name + USAGE_ARGUMENT_CLOSE;
if (type.getUsageSuffix() != null) {
usage += type.getUsageSuffix();
}
return usage;
return USAGE_ARGUMENT_OPEN + name + USAGE_ARGUMENT_CLOSE;
}
@Override

View file

@ -35,45 +35,12 @@ public class FloatArgumentTypeTest {
}
@Test
public void parse_noSuffix() throws Exception {
public void parse() throws Exception {
final StringReader reader = new StringReader("15");
assertThat(floatArg().parse(reader, context), is(15f));
assertThat(reader.canRead(), is(false));
}
@Test
public void parse_suffix() throws Exception {
final StringReader reader = new StringReader("15L");
assertThat(floatArg(0, 100, "L").parse(reader, context), is(15f));
assertThat(reader.canRead(), is(false));
}
@Test
public void parse_suffix_incorrect() throws Exception {
final StringReader reader = new StringReader("15W");
try {
floatArg(0, 100, "L").parse(reader, context);
fail();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(FloatArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
assertThat(ex.getCursor(), is(0));
}
}
@Test
public void parse_suffix_missing() throws Exception {
final StringReader reader = new StringReader("15");
try {
floatArg(0, 100, "L").parse(reader, context);
fail();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(FloatArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
assertThat(ex.getCursor(), is(0));
}
}
@Test
public void parse_tooSmall() throws Exception {
final StringReader reader = new StringReader("-5");
@ -115,8 +82,6 @@ public class FloatArgumentTypeTest {
.addEqualityGroup(floatArg(-100, 100), floatArg(-100, 100))
.addEqualityGroup(floatArg(-100, 50), floatArg(-100, 50))
.addEqualityGroup(floatArg(-50, 100), floatArg(-50, 100))
.addEqualityGroup(floatArg(-50, 100, "foo"), floatArg(-50, 100, "foo"))
.addEqualityGroup(floatArg(-50, 100, "bar"), floatArg(-50, 100, "bar"))
.testEquals();
}
@ -127,14 +92,4 @@ public class FloatArgumentTypeTest {
assertThat(floatArg(-100, 100), hasToString("float(-100.0, 100.0)"));
assertThat(floatArg(Integer.MIN_VALUE, 100), hasToString("float(-2.14748365E9, 100.0)"));
}
@Test
public void testUsageSuffix() throws Exception {
assertThat(floatArg().getUsageSuffix(), equalTo(null));
}
@Test
public void testUsageSuffix_suffix() throws Exception {
assertThat(floatArg(0, 100, "L").getUsageSuffix(), equalTo("L"));
}
}

View file

@ -35,45 +35,12 @@ public class IntegerArgumentTypeTest {
}
@Test
public void parse_noSuffix() throws Exception {
public void parse() throws Exception {
final StringReader reader = new StringReader("15");
assertThat(integer().parse(reader, context), is(15));
assertThat(reader.canRead(), is(false));
}
@Test
public void parse_suffix() throws Exception {
final StringReader reader = new StringReader("15L");
assertThat(integer(0, 100, "L").parse(reader, context), is(15));
assertThat(reader.canRead(), is(false));
}
@Test
public void parse_suffix_incorrect() throws Exception {
final StringReader reader = new StringReader("15W");
try {
integer(0, 100, "L").parse(reader, context);
fail();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
assertThat(ex.getCursor(), is(0));
}
}
@Test
public void parse_suffix_missing() throws Exception {
final StringReader reader = new StringReader("15");
try {
integer(0, 100, "L").parse(reader, context);
fail();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
assertThat(ex.getCursor(), is(0));
}
}
@Test
public void parse_tooSmall() throws Exception {
final StringReader reader = new StringReader("-5");
@ -115,8 +82,6 @@ public class IntegerArgumentTypeTest {
.addEqualityGroup(integer(-100, 100), integer(-100, 100))
.addEqualityGroup(integer(-100, 50), integer(-100, 50))
.addEqualityGroup(integer(-50, 100), integer(-50, 100))
.addEqualityGroup(integer(-50, 100, "foo"), integer(-50, 100, "foo"))
.addEqualityGroup(integer(-50, 100, "bar"), integer(-50, 100, "bar"))
.testEquals();
}
@ -127,14 +92,4 @@ public class IntegerArgumentTypeTest {
assertThat(integer(-100, 100), hasToString("integer(-100, 100)"));
assertThat(integer(Integer.MIN_VALUE, 100), hasToString("integer(-2147483648, 100)"));
}
@Test
public void testUsageSuffix() throws Exception {
assertThat(integer().getUsageSuffix(), equalTo(null));
}
@Test
public void testUsageSuffix_suffix() throws Exception {
assertThat(integer(0, 100, "L").getUsageSuffix(), equalTo("L"));
}
}

View file

@ -51,12 +51,6 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
assertThat(node.getUsageText(), is("<foo>"));
}
@Test
public void testUsage_suffix() throws Exception {
node = argument("foo", integer(0, 100, "L")).build();
assertThat(node.getUsageText(), is("<foo>L"));
}
@Test
public void testSuggestions() throws Exception {
final Set<String> set = Sets.newHashSet();