Add unary operators

This commit is contained in:
TheMode 2021-06-29 04:11:02 +02:00
parent 43ac1c53e3
commit 6cc7ae5b23
2 changed files with 38 additions and 21 deletions

View File

@ -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()),

View File

@ -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