mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Reduced boilerplate, RelativeVec#parse takes a parser function
This commit is contained in:
parent
7f215ac899
commit
a6b4b17279
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,77 +8,19 @@ import net.minestom.server.utils.StringUtils;
|
|||||||
import net.minestom.server.utils.location.RelativeVec;
|
import net.minestom.server.utils.location.RelativeVec;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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.
|
* Represents a block position with 3 integers (x;y;z) which can take relative coordinates.
|
||||||
* <p>
|
* <p>
|
||||||
* Example: 5 ~ -3
|
* Example: 5 ~ -3
|
||||||
*/
|
*/
|
||||||
public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeVec> {
|
public class ArgumentRelativeBlockPosition extends ArgumentRelativeVec {
|
||||||
|
|
||||||
public ArgumentRelativeBlockPosition(@NotNull String id) {
|
public ArgumentRelativeBlockPosition(@NotNull String id) {
|
||||||
super(id, 3);
|
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
|
@Override
|
||||||
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
||||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
|
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
|
||||||
@ -91,4 +33,9 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeVec>
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("RelativeBlockPosition<%s>", getId());
|
return String.format("RelativeBlockPosition<%s>", getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Function<String, ? extends Number> getNumberParser() {
|
||||||
|
return Integer::parseInt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,34 +1,23 @@
|
|||||||
package net.minestom.server.command.builder.arguments.relative;
|
package net.minestom.server.command.builder.arguments.relative;
|
||||||
|
|
||||||
import net.minestom.server.command.builder.NodeMaker;
|
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.coordinate.Vec;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a {@link Vec} with 2 floating numbers (x;z) which can take relative coordinates.
|
* Represents a {@link Vec} with 2 floating numbers (x;z) which can take relative coordinates.
|
||||||
* <p>
|
* <p>
|
||||||
* Example: -1.2 ~
|
* Example: -1.2 ~
|
||||||
*/
|
*/
|
||||||
public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
|
public class ArgumentRelativeVec2 extends ArgumentRelativeVec {
|
||||||
|
|
||||||
public ArgumentRelativeVec2(@NotNull String id) {
|
public ArgumentRelativeVec2(@NotNull String id) {
|
||||||
super(id, 2);
|
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
|
@Override
|
||||||
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
||||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
|
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
|
||||||
@ -41,4 +30,9 @@ public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("RelativeVec2<%s>", getId());
|
return String.format("RelativeVec2<%s>", getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Function<String, ? extends Number> getNumberParser() {
|
||||||
|
return Float::parseFloat;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,34 +1,23 @@
|
|||||||
package net.minestom.server.command.builder.arguments.relative;
|
package net.minestom.server.command.builder.arguments.relative;
|
||||||
|
|
||||||
import net.minestom.server.command.builder.NodeMaker;
|
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.coordinate.Vec;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
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 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.
|
* Represents a {@link Vec} with 3 floating numbers (x;y;z) which can take relative coordinates.
|
||||||
* <p>
|
* <p>
|
||||||
* Example: -1.2 ~ 5
|
* Example: -1.2 ~ 5
|
||||||
*/
|
*/
|
||||||
public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
|
public class ArgumentRelativeVec3 extends ArgumentRelativeVec {
|
||||||
|
|
||||||
public ArgumentRelativeVec3(@NotNull String id) {
|
public ArgumentRelativeVec3(@NotNull String id) {
|
||||||
super(id, 3);
|
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
|
@Override
|
||||||
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
||||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
|
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
|
||||||
@ -41,4 +30,9 @@ public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("RelativeVec3<%s>", getId());
|
return String.format("RelativeVec3<%s>", getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Function<String, ? extends Number> getNumberParser() {
|
||||||
|
return Float::parseFloat;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,9 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Objects;
|
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.
|
* Represents a location which can have fields relative to an {@link Entity} position.
|
||||||
@ -108,7 +109,7 @@ public final class RelativeVec {
|
|||||||
return relativeZ;
|
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
|
// Check if the value has enough element to be correct
|
||||||
if (input.length != 3 && input.length != 2) {
|
if (input.length != 3 && input.length != 2) {
|
||||||
throw new ArgumentSyntaxException("Invalid number of values", String.join(StringUtils.SPACE, input), INVALID_NUMBER_COUNT_ERROR);
|
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()) {
|
if (element.length() != RELATIVE_CHAR.length()) {
|
||||||
final String potentialNumber = element.substring(1);
|
final String potentialNumber = element.substring(1);
|
||||||
coordinates[i] = Float.parseFloat(potentialNumber);
|
coordinates[i] = (Double) numberParser.apply(potentialNumber);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
coordinates[i] = Float.parseFloat(element);
|
coordinates[i] = (Double) numberParser.apply(element);
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new ArgumentSyntaxException("Invalid number", String.join(StringUtils.SPACE, input), INVALID_NUMBER_ERROR);
|
throw new ArgumentSyntaxException("Invalid number", String.join(StringUtils.SPACE, input), INVALID_NUMBER_ERROR);
|
||||||
|
Loading…
Reference in New Issue
Block a user