From 4414baf89b146d99cf25a6504bacdc8a99ac35e8 Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 5 Jul 2021 08:01:43 +0200 Subject: [PATCH] Move classes & some methods --- .../server/utils/coordinate/Point.java | 83 +++++++++++++++++++ .../utils/{incubator => coordinate}/Pos.java | 2 +- .../utils/{incubator => coordinate}/Vec.java | 32 +------ .../server/utils/incubator/Point.java | 52 ------------ src/test/java/demo/PlayerInit.java | 2 +- 5 files changed, 86 insertions(+), 85 deletions(-) create mode 100644 src/main/java/net/minestom/server/utils/coordinate/Point.java rename src/main/java/net/minestom/server/utils/{incubator => coordinate}/Pos.java (98%) rename src/main/java/net/minestom/server/utils/{incubator => coordinate}/Vec.java (90%) delete mode 100644 src/main/java/net/minestom/server/utils/incubator/Point.java diff --git a/src/main/java/net/minestom/server/utils/coordinate/Point.java b/src/main/java/net/minestom/server/utils/coordinate/Point.java new file mode 100644 index 000000000..2a23b5bc6 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/coordinate/Point.java @@ -0,0 +1,83 @@ +package net.minestom.server.utils.coordinate; + +import net.minestom.server.utils.MathUtils; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +/** + * Represents a 3D point. + *

+ * Can either be a {@link Pos} or {@link Vec}. + * Interface will become {@code sealed} in the future. + */ +@ApiStatus.NonExtendable +public interface Point { + + /** + * Gets the X coordinate. + * + * @return the X coordinate + */ + @Contract(pure = true) + double x(); + + /** + * Gets the Y coordinate. + * + * @return the Y coordinate + */ + @Contract(pure = true) + double y(); + + /** + * Gets the Z coordinate. + * + * @return the Z coordinate + */ + @Contract(pure = true) + double z(); + + /** + * Gets the distance between this point and another. The value of this + * method is not cached and uses a costly square-root function, so do not + * repeatedly call this method to get the vector's magnitude. NaN will be + * returned if the inner result of the sqrt() function overflows, which + * will be caused if the distance is too long. + * + * @param point the other point + * @return the distance + */ + @Contract(pure = true) + default double distance(@NotNull Point point) { + return Math.sqrt(MathUtils.square(x() - point.x()) + + MathUtils.square(y() - point.y()) + + MathUtils.square(z() - point.z())); + } + + /** + * Gets the squared distance between this point and another. + * + * @param point the other point + * @return the squared distance + */ + @Contract(pure = true) + default double distanceSquared(@NotNull Point point) { + return MathUtils.square(x() - point.x()) + + MathUtils.square(y() - point.y()) + + MathUtils.square(z() - point.z()); + } + + /** + * Converts all coordinates to integers. + * + * @return a new point representing a block position + */ + @Contract(pure = true) + default @NotNull Point asBlockPosition() { + final int castedY = (int) y(); + return new Vec((int) Math.floor(x()), + (y() == castedY) ? castedY : castedY + 1, + (int) Math.floor(z())); + } +} diff --git a/src/main/java/net/minestom/server/utils/incubator/Pos.java b/src/main/java/net/minestom/server/utils/coordinate/Pos.java similarity index 98% rename from src/main/java/net/minestom/server/utils/incubator/Pos.java rename to src/main/java/net/minestom/server/utils/coordinate/Pos.java index 4f2113dd3..f673ab806 100644 --- a/src/main/java/net/minestom/server/utils/incubator/Pos.java +++ b/src/main/java/net/minestom/server/utils/coordinate/Pos.java @@ -1,4 +1,4 @@ -package net.minestom.server.utils.incubator; +package net.minestom.server.utils.coordinate; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/net/minestom/server/utils/incubator/Vec.java b/src/main/java/net/minestom/server/utils/coordinate/Vec.java similarity index 90% rename from src/main/java/net/minestom/server/utils/incubator/Vec.java rename to src/main/java/net/minestom/server/utils/coordinate/Vec.java index 521b45e43..c7e1f4c74 100644 --- a/src/main/java/net/minestom/server/utils/incubator/Vec.java +++ b/src/main/java/net/minestom/server/utils/coordinate/Vec.java @@ -1,4 +1,4 @@ -package net.minestom.server.utils.incubator; +package net.minestom.server.utils.coordinate; import net.minestom.server.utils.MathUtils; import org.jetbrains.annotations.Contract; @@ -207,36 +207,6 @@ public final class Vec implements Point { return new Vec(x / length, y / length, z / length); } - /** - * Gets the distance between this vector and another. The value of this - * method is not cached and uses a costly square-root function, so do not - * repeatedly call this method to get the vector's magnitude. NaN will be - * returned if the inner result of the sqrt() function overflows, which - * will be caused if the distance is too long. - * - * @param vec the other vector - * @return the distance - */ - @Contract(pure = true) - public double distance(@NotNull Vec vec) { - return Math.sqrt(MathUtils.square(x - vec.x) + - MathUtils.square(y - vec.y) + - MathUtils.square(z - vec.z)); - } - - /** - * Gets the squared distance between this vector and another. - * - * @param vec the other vector - * @return the squared distance - */ - @Contract(pure = true) - public double distanceSquared(@NotNull Vec vec) { - return MathUtils.square(x - vec.x) + - MathUtils.square(y - vec.y) + - MathUtils.square(z - vec.z); - } - /** * Gets the angle between this vector and another in radians. * diff --git a/src/main/java/net/minestom/server/utils/incubator/Point.java b/src/main/java/net/minestom/server/utils/incubator/Point.java deleted file mode 100644 index 7d106570d..000000000 --- a/src/main/java/net/minestom/server/utils/incubator/Point.java +++ /dev/null @@ -1,52 +0,0 @@ -package net.minestom.server.utils.incubator; - -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -/** - * Represents a 3D point. - *

- * Can either be a {@link Pos} or {@link Vec}. - * Interface will become {@code sealed} in the future. - */ -@ApiStatus.NonExtendable -public interface Point { - - /** - * Gets the X coordinate. - * - * @return the X coordinate - */ - @Contract(pure = true) - double x(); - - /** - * Gets the Y coordinate. - * - * @return the Y coordinate - */ - @Contract(pure = true) - double y(); - - /** - * Gets the Z coordinate. - * - * @return the Z coordinate - */ - @Contract(pure = true) - double z(); - - /** - * Converts all coordinates to integers. - * - * @return a new point representing a block position - */ - @Contract(pure = true) - default @NotNull Point asBlockPosition() { - final int castedY = (int) y(); - return new Vec((int) Math.floor(x()), - (y() == castedY) ? castedY : castedY + 1, - (int) Math.floor(z())); - } -} diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 615139f85..794efab26 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -36,7 +36,7 @@ import net.minestom.server.monitoring.TickMonitor; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; -import net.minestom.server.utils.incubator.Vec; +import net.minestom.server.utils.coordinate.Vec; import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.world.DimensionType;