diff --git a/common/src/main/java/revxrsal/commands/core/BaseCommandDispatcher.java b/common/src/main/java/revxrsal/commands/core/BaseCommandDispatcher.java index 2ae38092..ac55dc33 100644 --- a/common/src/main/java/revxrsal/commands/core/BaseCommandDispatcher.java +++ b/common/src/main/java/revxrsal/commands/core/BaseCommandDispatcher.java @@ -17,6 +17,7 @@ import revxrsal.commands.process.ValueResolver.ValueResolverContext; import java.util.List; +import java.util.function.Function; public final class BaseCommandDispatcher { @@ -270,5 +271,32 @@ public ValueContextR(List input, @Override public String pop() { return argumentStack.pop(); } + + private T num(Function f) { + String input = pop(); + try { + if (input.startsWith("0x")) + return (T) Integer.valueOf(input.substring(2), 16); + return f.apply(input); + } catch (NumberFormatException e) { + throw new InvalidNumberException(parameter(), input); + } + } + + @Override public int popInt() { + return num(Integer::parseInt); + } + + @Override public double popDouble() { + return num(Double::parseDouble); + } + + @Override public float popFloat() { + return num(Float::parseFloat); + } + + @Override public long popLong() { + return num(Long::parseLong); + } } } diff --git a/common/src/main/java/revxrsal/commands/process/ValueResolver.java b/common/src/main/java/revxrsal/commands/process/ValueResolver.java index 124f557c..5f9886b3 100644 --- a/common/src/main/java/revxrsal/commands/process/ValueResolver.java +++ b/common/src/main/java/revxrsal/commands/process/ValueResolver.java @@ -5,8 +5,11 @@ import revxrsal.commands.command.ArgumentStack; import revxrsal.commands.command.CommandParameter; import revxrsal.commands.exception.CommandExceptionHandler; +import revxrsal.commands.exception.InvalidNumberException; import revxrsal.commands.process.ParameterResolver.ParameterResolverContext; +import java.util.NoSuchElementException; + /** * A resolver for resolving values that, by default, require data from the arguments * to resolve their value. @@ -61,10 +64,50 @@ interface ValueResolverContext extends ParameterResolverContext { * Returns (and removes) the first value in the {@link #arguments() argument stack}. * * @return The first value. - * @throws java.util.NoSuchElementException if the stack is empty + * @throws NoSuchElementException if the stack is empty */ String pop(); + /** + * Returns (and removes) the first value in the {@link #arguments() argument stack} + * and parses it into an integer. + * + * @return The first value, as an int. + * @throws NoSuchElementException if the stack is empty + * @throws InvalidNumberException if an invalid number is inputted + */ + int popInt(); + + /** + * Returns (and removes) the first value in the {@link #arguments() argument stack} + * and parses it into a double. + * + * @return The first value, as a double. + * @throws NoSuchElementException if the stack is empty + * @throws InvalidNumberException if an invalid number is inputted + */ + double popDouble(); + + /** + * Returns (and removes) the first value in the {@link #arguments() argument stack} + * and parses it into a float. + * + * @return The first value, as a float. + * @throws NoSuchElementException if the stack is empty + * @throws InvalidNumberException if an invalid number is inputted + */ + float popFloat(); + + /** + * Returns (and removes) the first value in the {@link #arguments() argument stack} + * and parses it into a double. + * + * @return The first value, as a long. + * @throws NoSuchElementException if the stack is empty + * @throws InvalidNumberException if an invalid number is inputted + */ + long popLong(); + } }