Noob question - how do I get an attaking entity?

public void Hit(DamageEntityEvent e){
	EntityDamageSource eds = (EntityDamageSource) e.getCause().all().get(0);
	Entity entity = eds.getSource();


it occurs error sometimes.
I want to know how to get an attaking entity that occurred damage to targetentity

public void onHit(DamageEntityEvent event, @Root EntityDamageSource entityDamageSource) {
  Entity entity = entityDamageSource.getSource();
  // do stuff

You might want to look at event filters instead of ever calling .all(). There’s other methods like root() and first() in Cause that aim to avoid you having to iterate over the entire list of objects every time.

Secondly, you’re never checking the cast, so there can be many cases where a DamageSource is simply not an EntityDamageSource (like when an entity is on fire), in which case, yes, you’d get ClassCastException.

1 Like

thank you so much!