diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 1890a1cc8..dcc0f5960 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1739,6 +1739,22 @@ public class Entity implements Viewable, Tickable, EventHandler, Da } } + /** + * Applies knockback to the entity + * + * @param strength the strength of the knockback, 0.4 is the vanilla value for a bare hand hit + * @param x knockback on x axle, for default knockback use the following formula
sin(attacker.yaw * (pi/180))
+ * @param z knockback on z axle, for default knockback use the following formula
-cos(attacker.yaw * (pi/180))
+ */ + public void takeKnockback(final float strength, final double x, final double z) { + if (strength > 0) { + final Vector velocityModifier = new Vector(x, 0d, z).normalize().multiply(strength); + this.velocity.setX(velocity.getX() / 2d - velocityModifier.getX()); + this.velocity.setY(onGround ? Math.min(.4d, velocity.getY() / 2d + strength) : velocity.getY()); + this.velocity.setZ(velocity.getZ() / 2d - velocityModifier.getZ()); + } + } + public enum Pose { STANDING, FALL_FLYING, diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 37fcf82c1..f1fd8642e 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -779,4 +779,18 @@ public class LivingEntity extends Entity implements EquipmentHandler { return null; } + /** + * Applies knockback + *

+ * Note: The strength is reduced based on knockback resistance + * + * @param strength the strength of the knockback, 0.4 is the vanilla value for a bare hand hit + * @param x knockback on x axle, for default knockback use the following formula

sin(attacker.yaw * (pi/180))
+ * @param z knockback on z axle, for default knockback use the following formula
-cos(attacker.yaw * (pi/180))
+ */ + @Override + public void takeKnockback(float strength, final double x, final double z) { + strength *= 1 - getAttributeValue(Attribute.KNOCKBACK_RESISTANCE); + super.takeKnockback(strength, x, z); + } }