namespace Minecraft.Server.FourKit.Entity;
using Minecraft.Server.FourKit.Util;
///
/// Represents a base entity in the world
///
public abstract 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)
{
NativeBridge.TeleportEntity?.Invoke(getEntityId(), _dimensionId, 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());
}
// 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;
}
}