Search in sources :

Example 1 with EntityResurrectEvent

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

the class GlowLivingEntity method tryUseTotem.

/**
 * Use "Totem of Undying" if equipped
 *
 * @return result of totem use
 */
public boolean tryUseTotem() {
    // TODO: Should return false if player die in void.
    if (!(this instanceof HumanEntity)) {
        return false;
    }
    HumanEntity human = (HumanEntity) this;
    ItemStack mainHand = human.getInventory().getItemInMainHand();
    ItemStack offHand = human.getInventory().getItemInOffHand();
    boolean hasTotem = false;
    if (!InventoryUtil.isEmpty(mainHand) && mainHand.getType() == Material.TOTEM_OF_UNDYING) {
        mainHand.setAmount(mainHand.getAmount() - 1);
        human.getInventory().setItemInMainHand(InventoryUtil.createEmptyStack());
        hasTotem = true;
    } else if (!InventoryUtil.isEmpty(offHand) && offHand.getType() == Material.TOTEM_OF_UNDYING) {
        human.getInventory().setItemInOffHand(InventoryUtil.createEmptyStack());
        hasTotem = true;
    }
    EntityResurrectEvent event = EventFactory.getInstance().callEvent(new EntityResurrectEvent(this));
    event.setCancelled(!hasTotem);
    if (event.isCancelled()) {
        return false;
    }
    this.setHealth(1.0F);
    this.clearActivePotionEffects();
    this.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 900, 1));
    this.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 900, 1));
    playEffectKnownAndSelf(EntityEffect.TOTEM_RESURRECT);
    return true;
}
Also used : GlowPotionEffect(net.glowstone.constants.GlowPotionEffect) PotionEffect(org.bukkit.potion.PotionEffect) EntityResurrectEvent(org.bukkit.event.entity.EntityResurrectEvent) HumanEntity(org.bukkit.entity.HumanEntity) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

GlowPotionEffect (net.glowstone.constants.GlowPotionEffect)1 HumanEntity (org.bukkit.entity.HumanEntity)1 EntityResurrectEvent (org.bukkit.event.entity.EntityResurrectEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 PotionEffect (org.bukkit.potion.PotionEffect)1