namespace Minecraft.Server.FourKit.Entity;
using Minecraft.Server.FourKit.Util;
///
/// Represents a base entity in the world
///
public class Entity
{
private Location _location = new();
private Guid _uniqueId = Guid.NewGuid();
private float _fallDistance;
private int _dimensionId;
private int _entityId;
private EntityType _entityType = EntityType.UNKNOWN;
private bool _onGround;
private double _velocityX, _velocityY, _velocityZ;
///
/// Gets the entity's current position.
///
/// a new copy of containing the position of this entity
public Location getLocation() => _location;
///
/// Returns a unique id for this entity
///
/// Entity id
public virtual int getEntityId() => _entityId;
///
/// Get the type of the entity.
///
/// The of this entity.
public new virtual EntityType getType() => _entityType;
public new virtual EntityType GetType() => _entityType;
///
/// Returns a unique and persistent id for this entity. Note that this is not the standard UUID for players.
///
/// A unique to this entity.
public Guid getUniqueId() => _uniqueId;
///
/// Teleports this entity to the given location.
/// This calls into the native server to perform the actual teleport.
///
/// The destination location.
/// true if the teleport was successful.
public virtual bool teleport(Location location)
{
int targetDimId = location.LocationWorld?.getDimensionId() ?? _dimensionId;
NativeBridge.TeleportEntity?.Invoke(getEntityId(), targetDimId, location.getX(), location.getY(), location.getZ());
SetLocation(location);
return true;
}
///
/// Sets the fall distance for this entity.
///
/// The fall distance value.
public void setFallDistance(float distance)
{
_fallDistance = distance;
NativeBridge.SetFallDistance?.Invoke(getEntityId(), distance);
}
///
/// Returns the distance this entity has fallen.
///
/// The current fall distance.
public float getFallDistance() => _fallDistance;
///
/// Gets the current world this entity resides in.
///
/// World containing this entity.
public World getWorld() => FourKit.getWorld(_dimensionId);
///
/// Returns true if the entity is supported by a block. This value is a
/// state updated by the server and is not recalculated unless the entity moves.
///
/// True if entity is on ground.
public bool isOnGround() => _onGround;
///
/// Gets this entity's current velocity.
///
/// Current travelling velocity of this entity.
public Vector getVelocity() => new Vector(_velocityX, _velocityY, _velocityZ);
///
/// Sets this entity's velocity.
///
/// New velocity to travel with.
public void setVelocity(Vector velocity)
{
_velocityX = velocity.getX();
_velocityY = velocity.getY();
_velocityZ = velocity.getZ();
NativeBridge.SetVelocity?.Invoke(getEntityId(), velocity.getX(), velocity.getY(), velocity.getZ());
}
///
/// Returns whether this entity is inside a vehicle.
///
/// true if the entity is in a vehicle.
public bool isInsideVehicle()
{
return (NativeBridge.GetVehicleId?.Invoke(getEntityId()) ?? -1) >= 0;
}
///
/// Leave the current vehicle. If the entity is currently in a vehicle
/// (and is removed from it), true will be returned, otherwise
/// false will be returned.
///
/// true if the entity was in a vehicle.
public bool leaveVehicle()
{
return NativeBridge.LeaveVehicle?.Invoke(getEntityId()) != 0;
}
///
/// Get the vehicle that this entity is inside. If there is no vehicle,
/// null will be returned.
///
/// The current vehicle, or null.
public Entity? getVehicle()
{
int vehicleId = NativeBridge.GetVehicleId?.Invoke(getEntityId()) ?? -1;
if (vehicleId < 0) return null;
return FourKit.GetEntityByEntityId(vehicleId);
}
///
/// Eject any passenger.
///
/// true if there was a passenger.
public bool eject()
{
return NativeBridge.Eject?.Invoke(getEntityId()) != 0;
}
///
/// Gets the primary passenger of a vehicle. For vehicles that could
/// have multiple passengers, this will only return the primary passenger.
///
/// The passenger entity, or null.
public Entity? getPassenger()
{
int passengerId = NativeBridge.GetPassengerId?.Invoke(getEntityId()) ?? -1;
if (passengerId < 0) return null;
return FourKit.GetEntityByEntityId(passengerId);
}
///
/// Set the passenger of a vehicle.
///
/// The new passenger.
/// false if it could not be done for whatever reason.
public bool setPassenger(Entity passenger)
{
if (passenger == null || NativeBridge.SetPassenger == null) return false;
return NativeBridge.SetPassenger(getEntityId(), passenger.getEntityId()) != 0;
}
// INTERNAL
internal void SetLocation(Location location)
{
_location = location;
}
internal void SetFallDistanceInternal(float distance) => _fallDistance = distance;
internal void SetUniqueId(Guid id)
{
_uniqueId = id;
}
internal void SetDimensionInternal(int dimensionId) => _dimensionId = dimensionId;
internal void SetEntityIdInternal(int entityId) => _entityId = entityId;
internal void SetEntityTypeInternal(EntityType entityType) => _entityType = entityType;
internal void SetOnGroundInternal(bool onGround) => _onGround = onGround;
internal void SetVelocityInternal(double x, double y, double z)
{
_velocityX = x;
_velocityY = y;
_velocityZ = z;
}
}