Add unary operators

This commit is contained in:
TheMode 2021-06-29 19:59:38 +02:00
parent f8d1f8e067
commit dab17f44d5
2 changed files with 12 additions and 1 deletions

View File

@ -23,6 +23,11 @@ public interface Vec {
@Contract(pure = true)
@NotNull Vec with(double x, double y, double z);
@Contract(pure = true)
default @NotNull Vec with(Operator operator) {
return operator.apply(x(), y(), z());
}
@Contract(pure = true)
default @NotNull Vec withX(@NotNull DoubleUnaryOperator operator) {
return with(operator.applyAsDouble(x()), y(), z());
@ -119,4 +124,9 @@ public interface Vec {
@Contract(pure = true)
double z();
@FunctionalInterface
interface Operator {
@NotNull Vec apply(double x, double y, double z);
}
}

View File

@ -164,8 +164,9 @@ public class PlayerInit {
.mul(Vec.vec(5, 5, 5))
.mul(Vec.vec(5))
.asBlockPosition();
UnaryOperator<Vec> blockTransform2 = vec -> Vec.vec(vec.x()*2, 20, vec.z()+5);
UnaryOperator<Vec> blockTransform2 = vec -> Vec.vec(vec.x() * 2, 20, vec.z() + 5);
var vec = Vec.ZERO.apply(blockTransform).add(Vec.vec(5));
var vec2 = Vec.ZERO.with((x, y, z) -> Vec.vec(x + 5, y / 5, z * 5));
var eventHandler = MinecraftServer.getGlobalEventHandler();
eventHandler.addChild(DEMO_NODE);
var children = eventHandler.findChildren("demo", Event.class);