From b20de605e1560fd9c05044102e5a258e82e5d2d0 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Thu, 4 Mar 2021 11:57:58 -0500 Subject: [PATCH] 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);