Question How to get knockback from damage, e.g., EntityDamageEvent

Noy

New member
Jan 8, 2023
2
0
1
(PlayerVelocityEvent is not a choice since it couldn't tell the cause and only responds to player)
I checked the mojang remapped just now. I found that the EntityDamageEvent is called in method damageEntity0(final DamageSource damagesource, float f)
, which is invoke in method hurt(DamageSource damagesource, float f). After damageEntity0 was called with a true return, it will calculate and perform the knockback.
Java:
public boolean hurt(DamageSource damagesource, float f) {
    //details omitted
    if (!this.damageEntity0(damagesource, f - this.lastHurt)) {
        return false;
    }
    //...
    this.hurtDir = (float)(Mth.atan2(d1, d0) * 57.2957763671875 - (double)this.getYRot());
    this.knockback(0.4000000059604645, d0, d1);
    //...
}
Java:
protected boolean damageEntity0(final DamageSource damagesource, float f) {
    //...
    EntityDamageEvent event = CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, (double)originalDamage, (double)hardHatModifier, (double)blockingModifier, (double)armorModifier, (double)resistanceModifier, (double)magicModifier, (double)absorptionModifier, hardHat, blocking, armor, resistance, magic, absorption);
    if (damagesource.getEntity() instanceof Player) {
        ((Player)damagesource.getEntity()).resetAttackStrengthTicker();
    }

    if (event.isCancelled()) {
        return false;
    } else {
        //...
        return true;
    }
}
It seems that there's no way to get or change the knockback in a damage event, and the only way is to cancel this event and then calculate and perform the knockback by myself. But I still want to know if there's a better solution.
 
Last edited: