From cfad7a41459f86c68a139ad939d6f5562b7c834d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Fri, 9 Jul 2021 15:17:35 +0200 Subject: [PATCH] Fix parsing --- .../relative/ArgumentRelativeBlockPosition.java | 7 ++++++- .../builder/arguments/relative/ArgumentRelativeVec.java | 5 +++-- .../builder/arguments/relative/ArgumentRelativeVec2.java | 9 +++++++-- .../builder/arguments/relative/ArgumentRelativeVec3.java | 9 +++++++-- .../net/minestom/server/utils/location/RelativeVec.java | 6 +++--- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java index 04ff60147..403cad931 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java @@ -35,7 +35,12 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelativeVec { } @Override - Function getNumberParser() { + Function getRelativeNumberParser() { + return Double::parseDouble; + } + + @Override + Function getAbsoluteNumberParser() { return Integer::parseInt; } } diff --git a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec.java b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec.java index 6b68cbff8..eae69e898 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec.java @@ -25,7 +25,8 @@ public abstract class ArgumentRelativeVec extends Argument { this.numberCount = numberCount; } - abstract Function getNumberParser(); + abstract Function getRelativeNumberParser(); + abstract Function getAbsoluteNumberParser(); @NotNull @Override @@ -34,7 +35,7 @@ public abstract class ArgumentRelativeVec extends Argument { if (split.length != getNumberCount()) { throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR); } - return RelativeVec.parse(split, getNumberParser()); + return RelativeVec.parse(split, getRelativeNumberParser()); } /** diff --git a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec2.java b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec2.java index 663dbfa3a..6e71d42b1 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec2.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec2.java @@ -32,7 +32,12 @@ public class ArgumentRelativeVec2 extends ArgumentRelativeVec { } @Override - Function getNumberParser() { - return Float::parseFloat; + Function getRelativeNumberParser() { + return Double::parseDouble; + } + + @Override + Function getAbsoluteNumberParser() { + return Double::parseDouble; } } diff --git a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java index 2750202dc..2616203ec 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java @@ -32,7 +32,12 @@ public class ArgumentRelativeVec3 extends ArgumentRelativeVec { } @Override - Function getNumberParser() { - return Float::parseFloat; + Function getRelativeNumberParser() { + return Double::parseDouble; + } + + @Override + Function getAbsoluteNumberParser() { + return Double::parseDouble; } } diff --git a/src/main/java/net/minestom/server/utils/location/RelativeVec.java b/src/main/java/net/minestom/server/utils/location/RelativeVec.java index 002cb545a..c5ba1452d 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeVec.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeVec.java @@ -109,7 +109,7 @@ public final class RelativeVec { return relativeZ; } - public static RelativeVec parse(String[] input, Function numberParser) throws ArgumentSyntaxException { + public static RelativeVec parse(String[] input, Function relativeNumberParser, Function absoluteNumberParser) throws ArgumentSyntaxException { // Check if the value has enough element to be correct if (input.length != 3 && input.length != 2) { throw new ArgumentSyntaxException("Invalid number of values", String.join(StringUtils.SPACE, input), INVALID_NUMBER_COUNT_ERROR); @@ -125,10 +125,10 @@ public final class RelativeVec { if (element.length() != RELATIVE_CHAR.length()) { final String potentialNumber = element.substring(1); - coordinates[i] = numberParser.apply(potentialNumber).doubleValue(); + coordinates[i] = relativeNumberParser.apply(potentialNumber).doubleValue(); } } else { - coordinates[i] = numberParser.apply(element).doubleValue(); + coordinates[i] = absoluteNumberParser.apply(element).doubleValue(); } } catch (NumberFormatException e) { throw new ArgumentSyntaxException("Invalid number", String.join(StringUtils.SPACE, input), INVALID_NUMBER_ERROR);