Merge pull request #160 from LeoDog896/relative-cleanup

Prefer position over entity
This commit is contained in:
TheMode 2021-03-04 19:00:37 +01:00 committed by GitHub
commit 061dabbe4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View File

@ -17,11 +17,11 @@ public class RelativeBlockPosition extends RelativeLocation<BlockPosition> {
}
@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);

View File

@ -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,20 @@ public abstract class RelativeLocation<T> {
* @param entity the entity to get the relative position from
* @return the location
*/
public abstract T from(@Nullable Entity 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.

View File

@ -17,11 +17,11 @@ public class RelativeVec extends RelativeLocation<Vector> {
}
@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);