From b93f1a57cd959dcb79807df6dfb35125fb1cc9d8 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 30 Jun 2021 01:53:01 +0200 Subject: [PATCH] Create more Vec implementations --- .../minestom/server/utils/incubator/Vec.java | 13 ++- .../server/utils/incubator/VecImpl.java | 106 ++++++++++++++---- 2 files changed, 92 insertions(+), 27 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 9d54f7315..2c87b6a71 100644 --- a/src/main/java/net/minestom/server/utils/incubator/Vec.java +++ b/src/main/java/net/minestom/server/utils/incubator/Vec.java @@ -8,17 +8,22 @@ import java.util.function.DoubleUnaryOperator; import java.util.function.UnaryOperator; public interface Vec { - Vec ZERO = new VecImpl(0); - Vec ONE = new VecImpl(1); + Vec ZERO = vec(0); + Vec ONE = vec(1); @Contract(pure = true) static @NotNull Vec vec(double x, double y, double z) { - return new VecImpl(x, y, z); + return new VecImpl.Vec3(x, y, z); + } + + @Contract(pure = true) + static @NotNull Vec vec(double x, double z) { + return new VecImpl.Tuple(x, z); } @Contract(pure = true) static @NotNull Vec vec(double value) { - return new VecImpl(value); + return new VecImpl.Single(value); } @Contract(pure = true) 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 912869433..b4ce16c55 100644 --- a/src/main/java/net/minestom/server/utils/incubator/VecImpl.java +++ b/src/main/java/net/minestom/server/utils/incubator/VecImpl.java @@ -2,36 +2,96 @@ package net.minestom.server.utils.incubator; import org.jetbrains.annotations.NotNull; -class VecImpl implements Vec { - private final double x, y, z; +class VecImpl { - public VecImpl(double x, double y, double z) { - this.x = x; - this.y = y; - this.z = z; + static class Vec3 implements Vec { + private final double x, y, z; + + Vec3(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public Vec3(double value) { + this(value, value, value); + } + + @Override + public @NotNull Vec with(double x, double y, double z) { + return new Vec3(x, y, z); + } + + @Override + public double x() { + return x; + } + + @Override + public double y() { + return y; + } + + @Override + public double z() { + return z; + } } - public VecImpl(double value) { - this(value, value, value); + static class Tuple implements Vec { + private final double x, z; + + Tuple(double x, double z) { + this.x = x; + this.z = z; + } + + @Override + public @NotNull Vec with(double x, double y, double z) { + return new Vec3(x, y, z); + } + + @Override + public double x() { + return x; + } + + @Override + public double y() { + return 0; + } + + @Override + public double z() { + return z; + } } - @Override - public @NotNull Vec with(double x, double y, double z) { - return new VecImpl(x, y, z); - } + static class Single implements Vec { + private final double value; - @Override - public double x() { - return x; - } + Single(double value) { + this.value = value; + } - @Override - public double y() { - return y; - } + @Override + public @NotNull Vec with(double x, double y, double z) { + return new Vec3(x, y, z); + } - @Override - public double z() { - return z; + @Override + public double x() { + return value; + } + + @Override + public double y() { + return value; + } + + @Override + public double z() { + return value; + } } }