1.16.5 Cancel EntityDeathEvent

Discussion in 'Spigot Plugin Development' started by Jorngamernl, Jul 12, 2021.

  1. How to cancel the EntityDeathEvent?
     
  2. Listen to it, then cancel it :)
     
  3. Sorry I meant EntityDeathEvent
     
  4. Hello,

    You can not cancel that.
    You'll have to listen to the EntityDamageEvent, check if the entity is a player (if you want that for a player) and check if the EntityDamageEvent#getFinalDamage() is >= Player#getHealth().
     
    • Like Like x 1
    • Agree Agree x 1
  5. You can use something like this:
    Code (Java):
    @EventHandler
    public void onDie(EntityDamageEvent e) {
        if(!(e.getEntity() instanceof Player)) return;
        Player p = (Player)e.getEntity();
        if(p.getHealth() - e.getFinalDamage() <= 0.0D) {
            //Do your stuff
        }
    }
     
    • Like Like x 1
  6. Cant
     
  7. There also is a PlayerDeathEvent :p
     
  8. Which doesn't help, as that isn't cancellable either of course.
     
  9. However, be careful if using this code as if other plugins interfere into damage on your server the result may be inaccurate depending on the priority of the event.
     
    #9 Sxlver_, Jul 13, 2021
    Last edited: Jul 13, 2021
    • Agree Agree x 1
  10. Because EntityDamageEvent is cancellable, meaning that if you cancel after those checks you effectively cancel the death.
     
    • Funny Funny x 1

  11. the stuff
    Code (Java):
    p.spigot().respawn();
     
  12. No. If you want to respawn them immediately, just use PlayerDeathEvent. The question is about cancelling the death (truly), which means the stuff is:
    Code (Java):
    e.setCancelled(true);
    for the EntityDamageEvent in that example.
     
    #12 3ricL, Jul 13, 2021
    Last edited: Jul 13, 2021
  13. mb, I sometimes let my read disability shine through :p
     
    • Friendly Friendly x 1