Fix parsing

This commit is contained in:
Németh Noel 2021-07-09 15:17:35 +02:00
parent dff8bc61f6
commit cfad7a4145
5 changed files with 26 additions and 10 deletions

View File

@ -35,7 +35,12 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelativeVec {
}
@Override
Function<String, ? extends Number> getNumberParser() {
Function<String, ? extends Number> getRelativeNumberParser() {
return Double::parseDouble;
}
@Override
Function<String, ? extends Number> getAbsoluteNumberParser() {
return Integer::parseInt;
}
}

View File

@ -25,7 +25,8 @@ public abstract class ArgumentRelativeVec extends Argument<RelativeVec> {
this.numberCount = numberCount;
}
abstract Function<String, ? extends Number> getNumberParser();
abstract Function<String, ? extends Number> getRelativeNumberParser();
abstract Function<String, ? extends Number> getAbsoluteNumberParser();
@NotNull
@Override
@ -34,7 +35,7 @@ public abstract class ArgumentRelativeVec extends Argument<RelativeVec> {
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());
}
/**

View File

@ -32,7 +32,12 @@ public class ArgumentRelativeVec2 extends ArgumentRelativeVec {
}
@Override
Function<String, ? extends Number> getNumberParser() {
return Float::parseFloat;
Function<String, ? extends Number> getRelativeNumberParser() {
return Double::parseDouble;
}
@Override
Function<String, ? extends Number> getAbsoluteNumberParser() {
return Double::parseDouble;
}
}

View File

@ -32,7 +32,12 @@ public class ArgumentRelativeVec3 extends ArgumentRelativeVec {
}
@Override
Function<String, ? extends Number> getNumberParser() {
return Float::parseFloat;
Function<String, ? extends Number> getRelativeNumberParser() {
return Double::parseDouble;
}
@Override
Function<String, ? extends Number> getAbsoluteNumberParser() {
return Double::parseDouble;
}
}

View File

@ -109,7 +109,7 @@ public final class RelativeVec {
return relativeZ;
}
public static RelativeVec parse(String[] input, Function<String, ? extends Number> numberParser) throws ArgumentSyntaxException {
public static RelativeVec parse(String[] input, Function<String, ? extends Number> relativeNumberParser, Function<String, ? extends Number> 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);