From a2f828986de5aa8f1ead9a1d7e0aea007687b5df Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 20 Jun 2021 17:40:37 +0200 Subject: [PATCH] Add RelativeLocation#fromView --- .../utils/location/RelativeBlockPosition.java | 13 +++++++ .../utils/location/RelativeLocation.java | 34 ++++++++++++------- .../server/utils/location/RelativeVec.java | 13 +++++++ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java b/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java index b1b183e77..2a3ee592c 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java @@ -28,4 +28,17 @@ public class RelativeBlockPosition extends RelativeLocation { return new BlockPosition(x, y, z); } + + @Override + public BlockPosition fromView(@Nullable Position position) { + if (!relativeX && !relativeY && !relativeZ) { + return location.clone(); + } + final Position entityPosition = position != null ? position : new Position(); + + final int x = location.getX() + (relativeX ? (int) entityPosition.getYaw() : 0); + final int z = location.getZ() + (relativeZ ? (int) entityPosition.getPitch() : 0); + + return new BlockPosition(x, 0, z); + } } diff --git a/src/main/java/net/minestom/server/utils/location/RelativeLocation.java b/src/main/java/net/minestom/server/utils/location/RelativeLocation.java index fac09b12f..259406877 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeLocation.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeLocation.java @@ -1,5 +1,6 @@ package net.minestom.server.utils.location; +import com.google.common.annotations.Beta; import net.minestom.server.entity.Entity; import net.minestom.server.utils.Position; import org.jetbrains.annotations.NotNull; @@ -22,19 +23,6 @@ public abstract class RelativeLocation { this.relativeZ = relativeZ; } - /** - * Gets the location based on the relative fields and {@code entity}. - * - * @param entity the entity to get the relative position from - * @return the location - */ - public T from(@Nullable Entity entity) { - - final Position entityPosition = entity != null ? entity.getPosition() : new Position(); - - return from(entityPosition); - } - /** * Gets the location based on the relative fields and {@code position}. * @@ -43,6 +31,26 @@ public abstract class RelativeLocation { */ public abstract T from(@Nullable Position position); + @Beta + public abstract T fromView(@Nullable Position position); + + /** + * Gets the location based on the relative fields and {@code entity}. + * + * @param entity the entity to get the relative position from + * @return the location + */ + public T from(@Nullable Entity entity) { + final Position entityPosition = entity != null ? entity.getPosition() : new Position(); + return from(entityPosition); + } + + @Beta + public T fromView(@Nullable Entity entity) { + final Position entityPosition = entity != null ? entity.getPosition() : new Position(); + return fromView(entityPosition); + } + /** * Gets if the 'x' field is relative. * diff --git a/src/main/java/net/minestom/server/utils/location/RelativeVec.java b/src/main/java/net/minestom/server/utils/location/RelativeVec.java index 42d948c84..df1c02ed9 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeVec.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeVec.java @@ -28,4 +28,17 @@ public class RelativeVec extends RelativeLocation { return new Vector(x, y, z); } + + @Override + public Vector fromView(@Nullable Position position) { + if (!relativeX && !relativeY && !relativeZ) { + return location.clone(); + } + final Position entityPosition = position != null ? position : new Position(); + + final double x = location.getX() + (relativeX ? entityPosition.getYaw() : 0); + final double z = location.getZ() + (relativeZ ? entityPosition.getPitch() : 0); + + return new Vector(x, 0, z); + } }