Minestom icon indicating copy to clipboard operation
Minestom copied to clipboard

[Feature Request] EntityHealthChangeEvent (Cancelable)

Open einfachBlu opened this issue 3 years ago • 4 comments

Title says mostly it. An Event that is called when the Health are tried to be changed.

This opens the possibility to cancel the event and handle own logic at that point. Probably not a big change and makes Minestom a little bit more customizeable.

Specific Use-case:

  • Whenever a player's Health will be set to 0 from any extension or other source, then there is an option to check it and cancel it
  • So you can prevent a player from getting the respawn screen (especially to prevent the "loading world" screen after clicking respawn or automatically respawning) because you can teleport the player to the same location and handle the death custom

einfachBlu avatar Dec 16 '22 06:12 einfachBlu

I think EntityDamageEvent should cover the use cases here, and if you want to prevent a player from going through the respawn screen, you could

1 - Send a packet that tells the client that doImmediateRespawn is true 2 - Add a listener to Entity Damage Event with something along the lines of:

if(event.getDamage() > event.getEntity().getHealth()) {
    event.setCancelled(true);
}

GreatWyrm avatar Dec 16 '22 06:12 GreatWyrm

Good point but while i was reading it seems that the Event will not be called if some code executes

Player player;
player.setHealth(0);

einfachBlu avatar Dec 16 '22 06:12 einfachBlu

In which case you can simply use EntityDeathEvent or equivalent and call your own EntityHealthChangeEvent

Tofaa2 avatar Dec 16 '22 15:12 Tofaa2

EntityDeathEvent is not cancelable

einfachBlu avatar Dec 16 '22 16:12 einfachBlu