Cancel DeathEvent from Damage Fall cause

Discussion in 'Plugin Development' started by Suprem20, Jan 6, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Suprem20

    Hey,

    I want to cancel the damage event when a player falls to the ground holding a feather. Sadly, it isn't working, could someone help? Here's my code:

    Code:
        public void onEntityDamageEvent(EntityDamageEvent ev) {
     
            if (!(ev.getEntity() instanceof Player)) {
    
                return;
    
            }
    
            if ((ev.equals(EntityDamageEvent.DamageCause.FALL))) {
    
                Player player = (Player) ev.getEntity();
    
                if (player.getItemInHand().equals(Material.FEATHER)) {
    
                    ev.setCancelled(true);
                }
    
            }
        }
     
  2. Offline

    tips48

    change
    Code:java
    1.  
    2. if ((ev.equals(EntityDamageEvent.DamageCause.FALL))) {
    3.  

    to
    Code:java
    1.  
    2. if (ev.getCause().equals(EntityDamageEvent.DamageCause.FALL))
    3.  

    :)
     
    r3Fuze likes this.
  3. Offline

    Suprem20

    thanks
     
  4. Offline

    Bone008

    Same on your other equals comparison:
    Code:
                if (player.getItemInHand().equals(Material.FEATHER)) {
    You are comparing ItemStack to Material, that won't work. You're missing getType()

    Also, there's no need to use equals() on enums - you maybe just did it because your IDE told you "==" doesn't work, which it doesn't because you actually can't compare an ItemStack to a Material.

    Use == for enums.
    Code:
    ev.getCause() == DamageCause.FALL
    and
    Code:
    player.getItemInHand().getType() == Material.FEATHER
     
Thread Status:
Not open for further replies.

Share This Page