From 283469e02545593f64cfa5f4ba561649efca666f Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Thu, 4 Mar 2021 11:54:29 -0500 Subject: [PATCH 1/3] Prefer position over entity --- .../minestom/server/utils/location/RelativeBlockPosition.java | 4 ++-- .../net/minestom/server/utils/location/RelativeLocation.java | 3 ++- .../java/net/minestom/server/utils/location/RelativeVec.java | 4 ++-- 3 files changed, 6 insertions(+), 5 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 0d1eec53f..372597b95 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java @@ -17,11 +17,11 @@ public class RelativeBlockPosition extends RelativeLocation { } @Override - public BlockPosition from(@Nullable Entity entity) { + public BlockPosition from(@Nullable Position position) { if (!relativeX && !relativeY && !relativeZ) { return location.clone(); } - final Position entityPosition = entity != null ? entity.getPosition() : new Position(); + final Position entityPosition = position != null ? position : new Position(); final int x = location.getX() + (relativeX ? (int) entityPosition.getX() : 0); final int y = location.getY() + (relativeY ? (int) entityPosition.getY() : 0); 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 7a2cb3ec4..e800bc0bb 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeLocation.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeLocation.java @@ -1,6 +1,7 @@ package net.minestom.server.utils.location; import net.minestom.server.entity.Entity; +import net.minestom.server.utils.Position; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -27,7 +28,7 @@ public abstract class RelativeLocation { * @param entity the entity to get the relative position from * @return the location */ - public abstract T from(@Nullable Entity entity); + public abstract T from(@Nullable Position entity); /** * 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 606e9d62d..ad3e838a9 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeVec.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeVec.java @@ -17,11 +17,11 @@ public class RelativeVec extends RelativeLocation { } @Override - public Vector from(@Nullable Entity entity) { + public Vector from(@Nullable Position position) { if (!relativeX && !relativeY && !relativeZ) { return location.clone(); } - final Position entityPosition = entity != null ? entity.getPosition() : new Position(); + final Position entityPosition = position != null ? position : new Position(); final double x = location.getX() + (relativeX ? entityPosition.getX() : 0); final double y = location.getY() + (relativeY ? entityPosition.getY() : 0); From e3c47e60baf8e5ee67440e8881ad72b53c5d34bc Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Thu, 4 Mar 2021 11:55:14 -0500 Subject: [PATCH 2/3] Fix RelativeVector from test --- src/test/java/demo/commands/TeleportCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/demo/commands/TeleportCommand.java b/src/test/java/demo/commands/TeleportCommand.java index 562efa677..2956747a9 100644 --- a/src/test/java/demo/commands/TeleportCommand.java +++ b/src/test/java/demo/commands/TeleportCommand.java @@ -38,7 +38,7 @@ public class TeleportCommand extends Command { final Player player = sender.asPlayer(); final RelativeVec relativeVec = args.getRelativeVector("pos"); - final Position position = relativeVec.from(player).toPosition(); + final Position position = relativeVec.from(player.getPosition()).toPosition(); player.teleport(position); player.sendMessage("You have been teleported to " + position); From b20de605e1560fd9c05044102e5a258e82e5d2d0 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Thu, 4 Mar 2021 11:57:58 -0500 Subject: [PATCH 3/3] Add default method and fix tests --- .../server/utils/location/RelativeLocation.java | 15 ++++++++++++++- src/test/java/demo/commands/TeleportCommand.java | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) 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 e800bc0bb..fac09b12f 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeLocation.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeLocation.java @@ -28,7 +28,20 @@ public abstract class RelativeLocation { * @param entity the entity to get the relative position from * @return the location */ - public abstract T from(@Nullable Position entity); + 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}. + * + * @param position the relative position + * @return the location + */ + public abstract T from(@Nullable Position position); /** * Gets if the 'x' field is relative. diff --git a/src/test/java/demo/commands/TeleportCommand.java b/src/test/java/demo/commands/TeleportCommand.java index 2956747a9..562efa677 100644 --- a/src/test/java/demo/commands/TeleportCommand.java +++ b/src/test/java/demo/commands/TeleportCommand.java @@ -38,7 +38,7 @@ public class TeleportCommand extends Command { final Player player = sender.asPlayer(); final RelativeVec relativeVec = args.getRelativeVector("pos"); - final Position position = relativeVec.from(player.getPosition()).toPosition(); + final Position position = relativeVec.from(player).toPosition(); player.teleport(position); player.sendMessage("You have been teleported to " + position);