Reduced boilerplate, RelativeVec#parse takes a parser function

This commit is contained in:
Németh Noel 2021-07-09 02:45:10 +02:00
parent 7f215ac899
commit a6b4b17279
6 changed files with 77 additions and 126 deletions

View File

@ -1,33 +0,0 @@
package net.minestom.server.command.builder.arguments.relative;
import net.minestom.server.command.builder.arguments.Argument;
import org.jetbrains.annotations.NotNull;
/**
* Common interface for all the relative location arguments.
*
* @param <T> the relative location type
*/
public abstract class ArgumentRelative<T> extends Argument<T> {
public static final String RELATIVE_CHAR = "~";
public static final int INVALID_NUMBER_COUNT_ERROR = 1;
public static final int INVALID_NUMBER_ERROR = 2;
private final int numberCount;
public ArgumentRelative(@NotNull String id, int numberCount) {
super(id, true);
this.numberCount = numberCount;
}
/**
* Gets the amount of numbers that this relative location needs.
*
* @return the amount of coordinate required
*/
public int getNumberCount() {
return numberCount;
}
}

View File

@ -8,77 +8,19 @@ import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.location.RelativeVec;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
/**
* Represents a block position with 3 integers (x;y;z) which can take relative coordinates.
* <p>
* Example: 5 ~ -3
*/
public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeVec> {
public class ArgumentRelativeBlockPosition extends ArgumentRelativeVec {
public ArgumentRelativeBlockPosition(@NotNull String id) {
super(id, 3);
}
@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);
}
int 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];
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()) {
try {
final String potentialNumber = element.substring(1);
final int number = Integer.parseInt(potentialNumber);
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);
}
}
} else {
try {
final int number = Integer.parseInt(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);
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
@ -91,4 +33,9 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeVec>
public String toString() {
return String.format("RelativeBlockPosition<%s>", getId());
}
@Override
Function<String, ? extends Number> getNumberParser() {
return Integer::parseInt;
}
}

View File

@ -0,0 +1,48 @@
package net.minestom.server.command.builder.arguments.relative;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.location.RelativeVec;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
/**
* Common interface for all the relative location arguments.
*/
public abstract class ArgumentRelativeVec extends Argument<RelativeVec> {
public static final String RELATIVE_CHAR = "~";
public static final int INVALID_NUMBER_COUNT_ERROR = 1;
public static final int INVALID_NUMBER_ERROR = 2;
private final int numberCount;
public ArgumentRelativeVec(@NotNull String id, int numberCount) {
super(id, true);
this.numberCount = numberCount;
}
abstract Function<String, ? extends Number> getNumberParser();
@NotNull
@Override
public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE);
if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR);
}
return RelativeVec.parse(split, getNumberParser());
}
/**
* Gets the amount of numbers that this relative location needs.
*
* @return the amount of coordinate required
*/
public int getNumberCount() {
return numberCount;
}
}

View File

@ -1,34 +1,23 @@
package net.minestom.server.command.builder.arguments.relative;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.location.RelativeVec;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
/**
* Represents a {@link Vec} with 2 floating numbers (x;z) which can take relative coordinates.
* <p>
* Example: -1.2 ~
*/
public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
public class ArgumentRelativeVec2 extends ArgumentRelativeVec {
public ArgumentRelativeVec2(@NotNull String id) {
super(id, 2);
}
@NotNull
@Override
public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE);
if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR);
}
return RelativeVec.parse(split);
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
@ -41,4 +30,9 @@ public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
public String toString() {
return String.format("RelativeVec2<%s>", getId());
}
@Override
Function<String, ? extends Number> getNumberParser() {
return Float::parseFloat;
}
}

View File

@ -1,34 +1,23 @@
package net.minestom.server.command.builder.arguments.relative;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.location.RelativeVec;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
/**
* Represents a {@link Vec} with 3 floating numbers (x;y;z) which can take relative coordinates.
* <p>
* Example: -1.2 ~ 5
*/
public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
public class ArgumentRelativeVec3 extends ArgumentRelativeVec {
public ArgumentRelativeVec3(@NotNull String id) {
super(id, 3);
}
@NotNull
@Override
public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE);
if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR);
}
return RelativeVec.parse(split);
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
@ -41,4 +30,9 @@ public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
public String toString() {
return String.format("RelativeVec3<%s>", getId());
}
@Override
Function<String, ? extends Number> getNumberParser() {
return Float::parseFloat;
}
}

View File

@ -13,8 +13,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Function;
import static net.minestom.server.command.builder.arguments.relative.ArgumentRelative.*;
import static net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec.*;
/**
* Represents a location which can have fields relative to an {@link Entity} position.
@ -108,7 +109,7 @@ public final class RelativeVec {
return relativeZ;
}
public static RelativeVec parse(String[] input) throws ArgumentSyntaxException {
public static RelativeVec parse(String[] input, Function<String, ? extends Number> numberParser) 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);
@ -124,10 +125,10 @@ public final class RelativeVec {
if (element.length() != RELATIVE_CHAR.length()) {
final String potentialNumber = element.substring(1);
coordinates[i] = Float.parseFloat(potentialNumber);
coordinates[i] = (Double) numberParser.apply(potentialNumber);
}
} else {
coordinates[i] = Float.parseFloat(element);
coordinates[i] = (Double) numberParser.apply(element);
}
} catch (NumberFormatException e) {
throw new ArgumentSyntaxException("Invalid number", String.join(StringUtils.SPACE, input), INVALID_NUMBER_ERROR);