namespace Minecraft.Server.FourKit.Entity;
///
/// Represents an that can take damage and has health.
///
public class Damageable : Entity
{
private double _health = 20.0;
private double _maxHealth = 20.0;
private readonly double _originalMaxHealth = 20.0;
///
/// Deals the given amount of damage to this entity.
/// This calls into the native server to apply real damage.
///
/// Amount of damage to deal.
public void damage(double amount)
{
NativeBridge.DamagePlayer?.Invoke(getEntityId(), (float)amount);
}
///
/// Gets the entity's health from 0 to , where 0 is dead.
///
/// The current health.
public double getHealth() => _health;
///
/// Gets the maximum health this entity has.
///
/// The maximum health.
public double getMaxHealth() => _maxHealth;
///
/// Resets the max health to the original amount.
///
public void resetMaxHealth()
{
_maxHealth = _originalMaxHealth;
if (_health > _maxHealth)
_health = _maxHealth;
}
///
/// Sets the entity's health from 0 to , where 0 is dead.
/// This calls into the native server to apply the health change.
///
/// New health value.
public void setHealth(double health)
{
NativeBridge.SetPlayerHealth?.Invoke(getEntityId(), (float)Math.Clamp(health, 0.0, _maxHealth));
}
///
/// Sets the maximum health this entity can have.
/// If the entity's current health exceeds the new maximum, it is clamped.
///
/// New maximum health value.
public void setMaxHealth(double health)
{
_maxHealth = health;
if (_health > _maxHealth)
_health = _maxHealth;
}
// --- Internal setter used by the bridge ---
///
/// Updates health directly. Called internally by the bridge.
///
/// The new health value.
internal void SetHealthInternal(double health) => _health = health;
///
/// Updates max health directly. Called internally by the bridge.
///
/// The new max health value.
internal void SetMaxHealthInternal(double maxHealth) => _maxHealth = maxHealth;
}