Add RelativeLocation#fromView

This commit is contained in:
TheMode 2021-06-20 17:40:37 +02:00
parent 15cfea6f9a
commit a2f828986d
3 changed files with 47 additions and 13 deletions

View File

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

View File

@ -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<T> {
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<T> {
*/
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.
*

View File

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