mirror of https://github.com/Minestom/Minestom.git
83 lines
2.3 KiB
Java
83 lines
2.3 KiB
Java
package net.minestom.server.utils.location;
|
|
|
|
import net.minestom.server.entity.Entity;
|
|
import net.minestom.server.utils.Position;
|
|
import net.minestom.server.utils.coordinate.Point;
|
|
import net.minestom.server.utils.coordinate.Pos;
|
|
import org.jetbrains.annotations.ApiStatus;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
/**
|
|
* Represents a location which can have fields relative to an {@link Entity} position.
|
|
*
|
|
* @param <T> the location type
|
|
*/
|
|
public abstract class RelativeLocation<T extends Point> {
|
|
|
|
protected T location;
|
|
protected boolean relativeX, relativeY, relativeZ;
|
|
|
|
public RelativeLocation(@NotNull T location, boolean relativeX, boolean relativeY, boolean relativeZ) {
|
|
this.location = location;
|
|
this.relativeX = relativeX;
|
|
this.relativeY = relativeY;
|
|
this.relativeZ = relativeZ;
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
|
|
@ApiStatus.Experimental
|
|
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 var entityPosition = entity != null ? entity.getPosition() : Pos.ZERO;
|
|
return from(entityPosition);
|
|
}
|
|
|
|
@ApiStatus.Experimental
|
|
public T fromView(@Nullable Entity entity) {
|
|
final var entityPosition = entity != null ? entity.getPosition() : Pos.ZERO;
|
|
return fromView(entityPosition);
|
|
}
|
|
|
|
/**
|
|
* Gets if the 'x' field is relative.
|
|
*
|
|
* @return true if the 'x' field is relative
|
|
*/
|
|
public boolean isRelativeX() {
|
|
return relativeX;
|
|
}
|
|
|
|
/**
|
|
* Gets if the 'y' field is relative.
|
|
*
|
|
* @return true if the 'y' field is relative
|
|
*/
|
|
public boolean isRelativeY() {
|
|
return relativeY;
|
|
}
|
|
|
|
/**
|
|
* Gets if the 'z' field is relative.
|
|
*
|
|
* @return true if the 'z' field is relative
|
|
*/
|
|
public boolean isRelativeZ() {
|
|
return relativeZ;
|
|
}
|
|
}
|