Search in sources :

Example 1 with EntityRegainHealthEvent

use of org.bukkit.event.entity.EntityRegainHealthEvent in project Essentials by drtshock.

the class Commandheal method healPlayer.

private void healPlayer(final User user) throws PlayerExemptException, QuietAbortException {
    final Player player = user.getBase();
    if (player.getHealth() == 0) {
        throw new PlayerExemptException(tl("healDead"));
    }
    final double amount = player.getMaxHealth() - player.getHealth();
    final EntityRegainHealthEvent erhe = new EntityRegainHealthEvent(player, amount, RegainReason.CUSTOM);
    ess.getServer().getPluginManager().callEvent(erhe);
    if (erhe.isCancelled()) {
        throw new QuietAbortException();
    }
    double newAmount = player.getHealth() + erhe.getAmount();
    if (newAmount > player.getMaxHealth()) {
        newAmount = player.getMaxHealth();
    }
    player.setHealth(newAmount);
    player.setFoodLevel(20);
    player.setFireTicks(0);
    user.sendMessage(tl("heal"));
    for (PotionEffect effect : player.getActivePotionEffects()) {
        player.removePotionEffect(effect.getType());
    }
}
Also used : Player(org.bukkit.entity.Player) EntityRegainHealthEvent(org.bukkit.event.entity.EntityRegainHealthEvent) PotionEffect(org.bukkit.potion.PotionEffect)

Example 2 with EntityRegainHealthEvent

use of org.bukkit.event.entity.EntityRegainHealthEvent in project Essentials by EssentialsX.

the class Commandheal method healPlayer.

private void healPlayer(final User user) throws PlayerExemptException, QuietAbortException {
    final Player player = user.getBase();
    if (player.getHealth() == 0) {
        throw new PlayerExemptException(tl("healDead"));
    }
    final double amount = player.getMaxHealth() - player.getHealth();
    final EntityRegainHealthEvent erhe = new EntityRegainHealthEvent(player, amount, RegainReason.CUSTOM);
    ess.getServer().getPluginManager().callEvent(erhe);
    if (erhe.isCancelled()) {
        throw new QuietAbortException();
    }
    double newAmount = player.getHealth() + erhe.getAmount();
    if (newAmount > player.getMaxHealth()) {
        newAmount = player.getMaxHealth();
    }
    player.setHealth(newAmount);
    player.setFoodLevel(20);
    player.setFireTicks(0);
    user.sendMessage(tl("heal"));
    for (PotionEffect effect : player.getActivePotionEffects()) {
        player.removePotionEffect(effect.getType());
    }
}
Also used : Player(org.bukkit.entity.Player) EntityRegainHealthEvent(org.bukkit.event.entity.EntityRegainHealthEvent) PotionEffect(org.bukkit.potion.PotionEffect)

Example 3 with EntityRegainHealthEvent

use of org.bukkit.event.entity.EntityRegainHealthEvent in project AuthMeReloaded by AuthMe.

the class EntityListenerTest method shouldNotCancelRegainedHealth.

@Test
public void shouldNotCancelRegainedHealth() {
    // given
    EntityRegainHealthEvent event = mock(EntityRegainHealthEvent.class);
    given(listenerService.shouldCancelEvent(event)).willReturn(false);
    // when
    listener.entityRegainHealthEvent(event);
    // then
    verify(listenerService).shouldCancelEvent(event);
    verifyNoInteractions(event);
}
Also used : EntityRegainHealthEvent(org.bukkit.event.entity.EntityRegainHealthEvent) Test(org.junit.Test)

Example 4 with EntityRegainHealthEvent

use of org.bukkit.event.entity.EntityRegainHealthEvent in project AuthMeReloaded by AuthMe.

the class EntityListenerTest method shouldCancelRegainHealthEvent.

@Test
public void shouldCancelRegainHealthEvent() {
    // given
    EntityRegainHealthEvent event = mock(EntityRegainHealthEvent.class);
    given(listenerService.shouldCancelEvent(event)).willReturn(true);
    // when
    listener.entityRegainHealthEvent(event);
    // then
    verify(listenerService).shouldCancelEvent(event);
    verify(event).setCancelled(true);
    verify(event).setAmount(0);
}
Also used : EntityRegainHealthEvent(org.bukkit.event.entity.EntityRegainHealthEvent) Test(org.junit.Test)

Example 5 with EntityRegainHealthEvent

use of org.bukkit.event.entity.EntityRegainHealthEvent in project MagicPlugin by elBukkit.

the class HealAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (!(entity instanceof LivingEntity)) {
        return SpellResult.NO_TARGET;
    }
    LivingEntity targetEntity = (LivingEntity) entity;
    if (targetEntity.getHealth() == targetEntity.getMaxHealth() || targetEntity.isDead()) {
        return SpellResult.NO_TARGET;
    }
    double healAmount = amount;
    if (percentage > 0) {
        healAmount = targetEntity.getMaxHealth() * percentage;
    }
    if (maxDistanceSquared > 0) {
        double distanceSquared = context.getLocation().distanceSquared(entity.getLocation());
        if (distanceSquared > maxDistanceSquared) {
            return SpellResult.NO_TARGET;
        }
        if (distanceSquared > 0) {
            healAmount = healAmount * (1 - distanceSquared / maxDistanceSquared);
        }
    }
    EntityRegainHealthEvent event = new EntityRegainHealthEvent(targetEntity, healAmount, RegainReason.CUSTOM);
    Bukkit.getServer().getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        return SpellResult.CANCELLED;
    }
    healAmount = event.getAmount();
    if (healAmount == 0) {
        return SpellResult.NO_TARGET;
    }
    context.registerModified(targetEntity);
    targetEntity.setHealth(Math.min(targetEntity.getHealth() + healAmount, targetEntity.getMaxHealth()));
    return SpellResult.CAST;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) 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