Search in sources :

Example 6 with EntityRegainHealthEvent

use of org.bukkit.event.entity.EntityRegainHealthEvent in project Glowstone by GlowstoneMC.

the class EntityUtils method heal.

/**
 * Heals an entity by a specific amount.
 *
 * @param target the entity to heal
 * @param amount the amount of health to regain
 * @param reason the reason supplied to the {@link EntityRegainHealthEvent}
 * @return whether any health was regained this way
 * @throws IllegalArgumentException if the specified target has no {@link Attribute#GENERIC_MAX_HEALTH}
 */
public static boolean heal(@NotNull LivingEntity target, double amount, EntityRegainHealthEvent.RegainReason reason) {
    if (target.isDead() || amount <= 0) {
        return false;
    }
    final AttributeInstance attribute = target.getAttribute(Attribute.GENERIC_MAX_HEALTH);
    if (attribute == null) {
        throw new IllegalArgumentException("The specified LivingEntity has no " + Attribute.GENERIC_MAX_HEALTH + " attribute");
    }
    final double maxHealth = attribute.getValue();
    final double currentHealth = target.getHealth();
    if (currentHealth >= maxHealth) {
        // already max health
        return false;
    }
    EntityRegainHealthEvent event = new EntityRegainHealthEvent(target, amount, reason);
    EventFactory.getInstance().callEvent(event);
    if (event.isCancelled() || event.getAmount() <= 0) {
        return false;
    }
    target.setHealth(Math.min(maxHealth, currentHealth + event.getAmount()));
    return true;
}
Also used : EntityRegainHealthEvent(org.bukkit.event.entity.EntityRegainHealthEvent) AttributeInstance(org.bukkit.attribute.AttributeInstance)

Example 7 with EntityRegainHealthEvent

use of org.bukkit.event.entity.EntityRegainHealthEvent in project Towny by ElgarL.

the class HealthRegenTimerTask method incHealth.

public void incHealth(Player player) {
    // Keep saturation above zero while in town.
    float currentSat = player.getSaturation();
    if (currentSat == 0) {
        player.setSaturation(1F);
    }
    // Heal while in town.
    double currentHP = player.getHealth();
    if (currentHP < player.getMaxHealth()) {
        player.setHealth(Math.min(player.getMaxHealth(), ++currentHP));
        // Raise an event so other plugins can keep in sync.
        EntityRegainHealthEvent event = new EntityRegainHealthEvent(player, currentHP, RegainReason.REGEN);
        Bukkit.getServer().getPluginManager().callEvent(event);
    }
}
Also used : EntityRegainHealthEvent(org.bukkit.event.entity.EntityRegainHealthEvent)

Aggregations

EntityRegainHealthEvent (org.bukkit.event.entity.EntityRegainHealthEvent)7 Player (org.bukkit.entity.Player)2 PotionEffect (org.bukkit.potion.PotionEffect)2 Test (org.junit.Test)2 AttributeInstance (org.bukkit.attribute.AttributeInstance)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1