From a6b4b17279d1a0f2e24597a55fd96438d8794102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Fri, 9 Jul 2021 02:45:10 +0200 Subject: [PATCH] Reduced boilerplate, RelativeVec#parse takes a parser function --- .../arguments/relative/ArgumentRelative.java | 33 --------- .../ArgumentRelativeBlockPosition.java | 69 +++---------------- .../relative/ArgumentRelativeVec.java | 48 +++++++++++++ .../relative/ArgumentRelativeVec2.java | 22 +++--- .../relative/ArgumentRelativeVec3.java | 22 +++--- .../server/utils/location/RelativeVec.java | 9 +-- 6 files changed, 77 insertions(+), 126 deletions(-) delete mode 100644 src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelative.java create mode 100644 src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec.java diff --git a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelative.java b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelative.java deleted file mode 100644 index 2ab9471e0..000000000 --- a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelative.java +++ /dev/null @@ -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 the relative location type - */ -public abstract class ArgumentRelative extends Argument { - - 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; - } -} 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 f7c5e90d5..04ff60147 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 @@ -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. *

* Example: 5 ~ -3 */ -public class ArgumentRelativeBlockPosition extends ArgumentRelative { +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 public String toString() { return String.format("RelativeBlockPosition<%s>", getId()); } + + @Override + Function getNumberParser() { + 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 new file mode 100644 index 000000000..6b68cbff8 --- /dev/null +++ b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec.java @@ -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 { + + 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 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; + } +} 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 851bc976b..5677f1a13 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 @@ -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. *

* Example: -1.2 ~ */ -public class ArgumentRelativeVec2 extends ArgumentRelative { +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 { public String toString() { return String.format("RelativeVec2<%s>", getId()); } + + @Override + Function getNumberParser() { + return Float::parseFloat; + } } \ No newline at end of file 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 a5333614c..2750202dc 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 @@ -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. *

* Example: -1.2 ~ 5 */ -public class ArgumentRelativeVec3 extends ArgumentRelative { +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 { public String toString() { return String.format("RelativeVec3<%s>", getId()); } + + @Override + Function getNumberParser() { + return Float::parseFloat; + } } 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 152a89da4..911602501 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeVec.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeVec.java @@ -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 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);