Checking if Cause of Damage was an Arrow

I have an entity. Whenever the entity gets damaged, I want to know if the cause was by an arrow. Am I doing this right?

@Listener
public void onPlayerHit(InteractEntityEvent.Primary event, @First Player player) {
    if (event.getCause().any(Arrow.class)) {
        Projectile arrow = event.getCause().first(Arrow.class).get(); // This is the arrow that caused the damage
    }
}
@Listener
public void onDamageEntityEvent(DamageEntityEvent event, @First EntityDamageSource src) {
    if(src instanceof Arrow) {
        Arrow = (Arrow) src;
    }
}

You mean

if (src.getSource() instanceof Arrow) {
1 Like

Oops. Yeah that’s what I get for not checking my work

1 Like

There’s quite the explanation for DamageSources in general: https://github.com/SpongePowered/SpongeAPI/blob/master/src/main/java/org/spongepowered/api/event/entity/DamageEntityEvent.java#L61-L140

Also, technically, you want to look at whether the first element is an IndirectEntityDamageSource.

1 Like