From 5ec3f4655fe30a63827570877875b0f50c42901c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 28 Jul 2023 12:52:15 +0100 Subject: [PATCH] EntityDamageByEntityEvent: added APIs to get and set vertical knockback limits this was requested and PR'd as far back as 2020 (see #3782). Since no issue was filed about this, it became forgotten until #5946. However, #5946 overcomplicates the solution to the problem, and breaks BC without an obvious reason. --- src/entity/Living.php | 4 +-- .../entity/EntityDamageByEntityEvent.php | 28 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/entity/Living.php b/src/entity/Living.php index 1b5b582da..4d5e10cb3 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -557,14 +557,14 @@ abstract class Living extends Entity{ $e = $source->getChild(); if($e !== null){ $motion = $e->getMotion(); - $this->knockBack($motion->x, $motion->z, $source->getKnockBack()); + $this->knockBack($motion->x, $motion->z, $source->getKnockBack(), $source->getVerticalKnockBackLimit()); } }elseif($source instanceof EntityDamageByEntityEvent){ $e = $source->getDamager(); if($e !== null){ $deltaX = $this->location->x - $e->location->x; $deltaZ = $this->location->z - $e->location->z; - $this->knockBack($deltaX, $deltaZ, $source->getKnockBack()); + $this->knockBack($deltaX, $deltaZ, $source->getKnockBack(), $source->getVerticalKnockBackLimit()); } } diff --git a/src/event/entity/EntityDamageByEntityEvent.php b/src/event/entity/EntityDamageByEntityEvent.php index b8eb95def..5ef6c4b8e 100644 --- a/src/event/entity/EntityDamageByEntityEvent.php +++ b/src/event/entity/EntityDamageByEntityEvent.php @@ -36,7 +36,15 @@ class EntityDamageByEntityEvent extends EntityDamageEvent{ /** * @param float[] $modifiers */ - public function __construct(Entity $damager, Entity $entity, int $cause, float $damage, array $modifiers = [], private float $knockBack = Living::DEFAULT_KNOCKBACK_FORCE){ + public function __construct( + Entity $damager, + Entity $entity, + int $cause, + float $damage, + array $modifiers = [], + private float $knockBack = Living::DEFAULT_KNOCKBACK_FORCE, + private float $verticalKnockBackLimit = Living::DEFAULT_KNOCKBACK_VERTICAL_LIMIT + ){ $this->damagerEntityId = $damager->getId(); parent::__construct($entity, $cause, $damage, $modifiers); $this->addAttackerModifiers($damager); @@ -79,4 +87,22 @@ class EntityDamageByEntityEvent extends EntityDamageEvent{ public function setKnockBack(float $knockBack) : void{ $this->knockBack = $knockBack; } + + /** + * Returns the maximum upwards velocity the victim may have after being knocked back. + * This ensures that the victim doesn't fly up into the sky when high levels of knockback are applied. + * + * @see Living::DEFAULT_KNOCKBACK_VERTICAL_LIMIT + */ + public function getVerticalKnockBackLimit() : float{ + return $this->verticalKnockBackLimit; + } + + /** + * Sets the maximum upwards velocity the victim may have after being knocked back. + * Larger values will allow the victim to fly higher if the knockback force is also large. + */ + public function setVerticalKnockBackLimit(float $verticalKnockBackLimit) : void{ + $this->verticalKnockBackLimit = $verticalKnockBackLimit; + } }