Skip to content

Commit

Permalink
Add CommandElement to handle Longs
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Mar 6, 2016
1 parent 596a97c commit 419f3a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,34 @@ public Object parseValue(CommandSource source, CommandArgs args) throws Argument
}
}

/**
* Require an argument to be a long (base 10).
* Gives values of type {@link Integer}
*
* @param key The key to store the parsed argument under
* @return the element to match the input
*/
public static CommandElement longNum(Text key) {
return new LongElement(key);
}

private static class LongElement extends KeyElement {

private LongElement(Text key) {
super(key);
}

@Override
public Long parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
final String input = args.next();
try {
return Long.parseLong(input);
} catch (NumberFormatException ex) {
throw args.createError(t("Expected a long, but input '%s' was not", input));
}
}
}

/**
* Require an argument to be an double-precision floating point number.
* Gives values of type {@link Double}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.spongepowered.api.command.args.GenericArguments.enumValue;
import static org.spongepowered.api.command.args.GenericArguments.firstParsing;
import static org.spongepowered.api.command.args.GenericArguments.integer;
import static org.spongepowered.api.command.args.GenericArguments.longNum;
import static org.spongepowered.api.command.args.GenericArguments.none;
import static org.spongepowered.api.command.args.GenericArguments.optional;
import static org.spongepowered.api.command.args.GenericArguments.optionalWeak;
Expand Down Expand Up @@ -175,6 +176,15 @@ public void testInteger() throws ArgumentParseException {
parseForInput("notanumber", integer(untr("a value")));
}

@Test
public void testLong() throws ArgumentParseException {
CommandContext context = parseForInput("524903294023901", longNum(untr("a value")));
assertEquals(524903294023901L, context.getOne("a value").get());

this.expected.expect(ArgumentParseException.class);
parseForInput("notanumber", integer(untr("a value")));
}

@Test
public void testBool() throws ArgumentParseException {
CommandElement boolEl = bool(untr("val"));
Expand Down

0 comments on commit 419f3a1

Please sign in to comment.