Get the cause of death?

i need to get the cause of the death of an entity.
I use the DestructEntityEvent.Death Event. I can check if the death was directly caused by a player with
if(_e.getCause().first(Player.class).isPresent()) but it starts to get tricky when i want to get checked if an entity got killed by an arrow.
I also have problems when the entity gets killed by fire, from a fire arrow for example.

So i need a way to check if an entity got killed by a player, either by sword/directly (works) or by an arrow/fire arrow.

Do you know a way?



From there you can tell if it was caused by a explosion or fire, if it is castable to ProjectileDamageSourse then a projectile did it, the ProjectileDamageSource will then tell you what entityprojectile did it.

I know this works in DamageEntityEvent (which has a “willCauseDeath” option. I dont see why it wont in DestructEntityEvent

So i did it like you said:

if(_e.getCause().first(DamageSource.class).get().getType().getId() == "fire") {

But how do you cast it to ProjectileSource then?

Ok it works like this. Im not sure if i miss any events though.

    if (_e.getCause().first(DamageSource.class).get().getType().getId() == "fire") {
        if (_e.getCause().getContext().get(EventContextKeys.NOTIFIER).isPresent()) {

I would recommend doing

DamageSource source = event.getCause().getFirst(DamageSource.class).get();
if(source.equals(DamageSources.FIRE_TICKS) {

As this is the more … Normal.

As for casting. This is how

if(source instanceof EntityDamageSource) {
  EntityDamageSource source2 = (EntityDamageSource) source;

Here is all the options for DamageSources casts