From 6cc7ae5b233bf4fb28ca383ff8bc9e4bfccc968b Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 29 Jun 2021 04:11:02 +0200 Subject: [PATCH] Add unary operators --- .../minestom/server/utils/incubator/Vec.java | 55 ++++++++++++------- .../server/utils/incubator/VecImpl.java | 4 +- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/incubator/Vec.java b/src/main/java/net/minestom/server/utils/incubator/Vec.java index 6da49c931..67ce19dcb 100644 --- a/src/main/java/net/minestom/server/utils/incubator/Vec.java +++ b/src/main/java/net/minestom/server/utils/incubator/Vec.java @@ -3,11 +3,12 @@ package net.minestom.server.utils.incubator; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import java.util.function.DoubleUnaryOperator; import java.util.function.UnaryOperator; public interface Vec { - - Vec ZERO = new VecImpl(); + Vec ZERO = new VecImpl(0); + Vec ONE = new VecImpl(1); @Contract(pure = true) static @NotNull Vec vec(double x, double y, double z) { @@ -15,28 +16,43 @@ public interface Vec { } @Contract(pure = true) - static @NotNull Vec x(double x) { - return vec(x, 0, 0); - } - - @Contract(pure = true) - static @NotNull Vec y(double y) { - return vec(0, y, 0); - } - - @Contract(pure = true) - static @NotNull Vec z(double z) { - return vec(0, 0, z); - } - - @Contract(pure = true) - static @NotNull Vec xz(double x, double z) { - return vec(x, 0, z); + static @NotNull Vec vec(double value) { + return new VecImpl(value); } @Contract(pure = true) @NotNull Vec with(double x, double y, double z); + @Contract(pure = true) + default @NotNull Vec withX(@NotNull DoubleUnaryOperator operator) { + return with(operator.applyAsDouble(x()), y(), z()); + } + + @Contract(pure = true) + default @NotNull Vec withX(double x) { + return with(x, y(), z()); + } + + @Contract(pure = true) + default @NotNull Vec withY(@NotNull DoubleUnaryOperator operator) { + return with(x(), operator.applyAsDouble(y()), z()); + } + + @Contract(pure = true) + default @NotNull Vec withY(double y) { + return with(x(), y, z()); + } + + @Contract(pure = true) + default @NotNull Vec withZ(@NotNull DoubleUnaryOperator operator) { + return with(x(), y(), operator.applyAsDouble(z())); + } + + @Contract(pure = true) + default @NotNull Vec withZ(double z) { + return with(x(), y(), z); + } + @Contract(pure = true) default @NotNull Vec add(@NotNull Vec vec) { return with(x() + vec.x(), y() + vec.y(), z() + vec.z()); @@ -87,6 +103,7 @@ public interface Vec { return new Pos(x(), y(), z()); } + @Contract(pure = true) default @NotNull Vec asBlockPosition() { final int castedY = (int) y(); return with((int) Math.floor(x()), diff --git a/src/main/java/net/minestom/server/utils/incubator/VecImpl.java b/src/main/java/net/minestom/server/utils/incubator/VecImpl.java index 2cf8762bf..912869433 100644 --- a/src/main/java/net/minestom/server/utils/incubator/VecImpl.java +++ b/src/main/java/net/minestom/server/utils/incubator/VecImpl.java @@ -11,8 +11,8 @@ class VecImpl implements Vec { this.z = z; } - public VecImpl() { - this(0, 0, 0); + public VecImpl(double value) { + this(value, value, value); } @Override