@Listener
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.