In DamageEntityEvent listener, how exactly do I get:

  1. The entity that caused the damage
  2. The entity that got damaged

It’s confusing to me and hope someone can help out. Thanks!

So the entity that was damaged is actually already in the event. You can get it by the following

Entity damaged = event.getTargetEntity();

As for the entity that caused the damage in the first place, they arnt stored in the event directly as this event can fire if a entity gets damaged by anything such as falling.

Instead you will need to find the attacker through the cause system. Easiest way to do this is like the following

public void onAttack(DamageEntityEvent event, @First Entity attacker){

This means that the event will only fire if a entity damaged another. However please note that the “attacker” maybe a arrow, in which you can easily find the shooter, however the shooter maybe a dispenser or the damaged entity (shot up in the sky and let gravity do the rest)

1 Like

A DamageSource is always at the root of the cause for DamageEntityEvent and AttackEntityEvent, it contains information about the damage. I suggest using it to find the attacker.

public void onDamageEntity(DamageEntityEvent e, @Root EntityDamageSource d) {
	Entity victim = e.getTargetEntity();
	Entity attacker = d instanceof IndirectEntityDamageSource ? ((IndirectEntityDamageSource) d).getIndirectSource() : d.getSource();
1 Like

Thanks so much!

However please note that the “attacker” maybe a arrow

In this case, why can’t I do “@First Player attacker” instead? Is this because this would create the issue of storing the DAMAGED PLAYER inside “attacker” if, say, the cause is a dispenser arrow?

To answer your question, yes it may get confused. However as @Yeregorix pointed out the DamageSource is always first, meaning that you could use @Root instead of @First which will avoid the confusion. However do note that still of the damage is a arrow shot by a player it will not fire as the damage was not directly caused by a player.