Simplify parsing

This commit is contained in:
Németh Noel 2021-07-09 01:34:17 +02:00
parent b419ce88c1
commit 86be5ad9c5
3 changed files with 37 additions and 94 deletions

View File

@ -22,50 +22,7 @@ public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
@NotNull
@Override
public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE);
// Check if the value has enough element to be correct
if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR);
}
double x = 0, z = 0;
boolean relativeX = false;
boolean relativeZ = false;
for (int i = 0; i < split.length; i++) {
final String element = split[i];
try {
if (element.startsWith(RELATIVE_CHAR)) {
if (i == 0) {
relativeX = true;
} else if (i == 1) {
relativeZ = true;
}
if (element.length() != RELATIVE_CHAR.length()) {
final String potentialNumber = element.substring(1);
final float number = Float.parseFloat(potentialNumber);
if (i == 0) {
x = number;
} else if (i == 1) {
z = number;
}
}
} else {
final float number = Float.parseFloat(element);
if (i == 0) {
x = number;
} else if (i == 1) {
z = number;
}
}
} catch (NumberFormatException e) {
throw new ArgumentSyntaxException("Invalid number", input, INVALID_NUMBER_ERROR);
}
}
return new RelativeVec(new Vec(x, z), relativeX, false, relativeZ);
return RelativeVec.parse(input);
}
@Override

View File

@ -22,56 +22,7 @@ public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
@NotNull
@Override
public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE);
// Check if the value has enough element to be correct
if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR);
}
double x = 0, y = 0, z = 0;
boolean relativeX = false;
boolean relativeY = false;
boolean relativeZ = false;
for (int i = 0; i < split.length; i++) {
final String element = split[i];
try {
if (element.startsWith(RELATIVE_CHAR)) {
if (i == 0) {
relativeX = true;
} else if (i == 1) {
relativeY = true;
} else if (i == 2) {
relativeZ = true;
}
if (element.length() != RELATIVE_CHAR.length()) {
final String potentialNumber = element.substring(1);
final float number = Float.parseFloat(potentialNumber);
if (i == 0) {
x = number;
} else if (i == 1) {
y = number;
} else if (i == 2) {
z = number;
}
}
} else {
final float number = Float.parseFloat(element);
if (i == 0) {
x = number;
} else if (i == 1) {
y = number;
} else if (i == 2) {
z = number;
}
}
} catch (NumberFormatException e) {
throw new ArgumentSyntaxException("Invalid number", input, INVALID_NUMBER_ERROR);
}
}
return new RelativeVec(new Vec(x, y, z), relativeX, relativeY, relativeZ);
return RelativeVec.parse(input);
}
@Override

View File

@ -1,17 +1,21 @@
package net.minestom.server.utils.location;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import static net.minestom.server.command.builder.arguments.relative.ArgumentRelative.*;
/**
* Represents a location which can have fields relative to an {@link Entity} position.
*/
@ -103,4 +107,35 @@ public final class RelativeVec {
public boolean isRelativeZ() {
return relativeZ;
}
public static RelativeVec parse(String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE);
// Check if the value has enough element to be correct
if (split.length != 3 && split.length != 2) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR);
}
double[] coordinates = new double[split.length];
boolean[] isRelative = new boolean[split.length];
for (int i = 0; i < split.length; i++) {
final String element = split[i];
try {
if (element.startsWith(RELATIVE_CHAR)) {
isRelative[i] = true;
if (element.length() != RELATIVE_CHAR.length()) {
final String potentialNumber = element.substring(1);
coordinates[i] = Float.parseFloat(potentialNumber);
}
} else {
coordinates[i] = Float.parseFloat(element);
}
} catch (NumberFormatException e) {
throw new ArgumentSyntaxException("Invalid number", input, INVALID_NUMBER_ERROR);
}
}
return new RelativeVec(split.length == 3 ? new Vec(coordinates[0], coordinates[1], coordinates[2]) : new Vec(coordinates[0], coordinates[1]),
isRelative[0], split.length == 3 && isRelative[1], isRelative[split.length == 3 ? 2 : 1]);
}
}