From 1aa92bd6a801544211b9d698f905ee3b29e62840 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 21 May 2020 20:13:24 +0100 Subject: [PATCH] Living: Do not rely on attribute map for moveSpeed attribute access --- src/entity/Living.php | 17 ++++++++++++--- src/entity/effect/SlownessEffect.php | 7 ++---- src/entity/effect/SpeedEffect.php | 7 ++---- tests/phpstan/configs/l8-baseline.neon | 30 -------------------------- 4 files changed, 18 insertions(+), 43 deletions(-) diff --git a/src/entity/Living.php b/src/entity/Living.php index 604dfd325..2097bf0d4 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -107,6 +107,8 @@ abstract class Living extends Entity{ protected $absorptionAttr; /** @var Attribute */ protected $knockbackResistanceAttr; + /** @var Attribute */ + protected $moveSpeedAttr; /** @var bool */ protected $sprinting = false; @@ -168,7 +170,7 @@ abstract class Living extends Entity{ $this->attributeMap->add($this->healthAttr = AttributeFactory::getInstance()->mustGet(Attribute::HEALTH)); $this->attributeMap->add(AttributeFactory::getInstance()->mustGet(Attribute::FOLLOW_RANGE)); $this->attributeMap->add($this->knockbackResistanceAttr = AttributeFactory::getInstance()->mustGet(Attribute::KNOCKBACK_RESISTANCE)); - $this->attributeMap->add(AttributeFactory::getInstance()->mustGet(Attribute::MOVEMENT_SPEED)); + $this->attributeMap->add($this->moveSpeedAttr = AttributeFactory::getInstance()->mustGet(Attribute::MOVEMENT_SPEED)); $this->attributeMap->add(AttributeFactory::getInstance()->mustGet(Attribute::ATTACK_DAMAGE)); $this->attributeMap->add($this->absorptionAttr = AttributeFactory::getInstance()->mustGet(Attribute::ABSORPTION)); } @@ -213,11 +215,20 @@ abstract class Living extends Entity{ public function setSprinting(bool $value = true) : void{ if($value !== $this->isSprinting()){ $this->sprinting = $value; - $attr = $this->attributeMap->get(Attribute::MOVEMENT_SPEED); - $attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3), false, true); + $moveSpeed = $this->getMovementSpeed(); + $this->setMovementSpeed($value ? ($moveSpeed * 1.3) : ($moveSpeed / 1.3)); + $this->moveSpeedAttr->markSynchronized(false); //TODO: reevaluate this hack } } + public function getMovementSpeed() : float{ + return $this->moveSpeedAttr->getValue(); + } + + public function setMovementSpeed(float $v, bool $fit = false) : void{ + $this->moveSpeedAttr->setValue($v, $fit); + } + public function saveNBT() : CompoundTag{ $nbt = parent::saveNBT(); $nbt->setFloat("Health", $this->getHealth()); diff --git a/src/entity/effect/SlownessEffect.php b/src/entity/effect/SlownessEffect.php index a2604a1ba..6ab11bdbe 100644 --- a/src/entity/effect/SlownessEffect.php +++ b/src/entity/effect/SlownessEffect.php @@ -23,18 +23,15 @@ declare(strict_types=1); namespace pocketmine\entity\effect; -use pocketmine\entity\Attribute; use pocketmine\entity\Living; class SlownessEffect extends Effect{ public function add(Living $entity, EffectInstance $instance) : void{ - $attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED); - $attr->setValue($attr->getValue() * (1 - 0.15 * $instance->getEffectLevel()), true); + $entity->setMovementSpeed($entity->getMovementSpeed() * (1 - 0.15 * $instance->getEffectLevel()), true); } public function remove(Living $entity, EffectInstance $instance) : void{ - $attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED); - $attr->setValue($attr->getValue() / (1 - 0.15 * $instance->getEffectLevel())); + $entity->setMovementSpeed($entity->getMovementSpeed() / (1 - 0.15 * $instance->getEffectLevel())); } } diff --git a/src/entity/effect/SpeedEffect.php b/src/entity/effect/SpeedEffect.php index caafe0b74..d84d3a6d6 100644 --- a/src/entity/effect/SpeedEffect.php +++ b/src/entity/effect/SpeedEffect.php @@ -23,18 +23,15 @@ declare(strict_types=1); namespace pocketmine\entity\effect; -use pocketmine\entity\Attribute; use pocketmine\entity\Living; class SpeedEffect extends Effect{ public function add(Living $entity, EffectInstance $instance) : void{ - $attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED); - $attr->setValue($attr->getValue() * (1 + 0.2 * $instance->getEffectLevel())); + $entity->setMovementSpeed($entity->getMovementSpeed() * (1 + 0.2 * $instance->getEffectLevel())); } public function remove(Living $entity, EffectInstance $instance) : void{ - $attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED); - $attr->setValue($attr->getValue() / (1 + 0.2 * $instance->getEffectLevel())); + $entity->setMovementSpeed($entity->getMovementSpeed() / (1 + 0.2 * $instance->getEffectLevel())); } } diff --git a/tests/phpstan/configs/l8-baseline.neon b/tests/phpstan/configs/l8-baseline.neon index 860fc02c1..fe34314b6 100644 --- a/tests/phpstan/configs/l8-baseline.neon +++ b/tests/phpstan/configs/l8-baseline.neon @@ -185,16 +185,6 @@ parameters: count: 1 path: ../../../src/entity/HungerManager.php - - - message: "#^Cannot call method getValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#" - count: 2 - path: ../../../src/entity/Living.php - - - - message: "#^Cannot call method setValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#" - count: 1 - path: ../../../src/entity/Living.php - - message: "#^Cannot call method getEffectLevel\\(\\) on pocketmine\\\\entity\\\\effect\\\\EffectInstance\\|null\\.$#" count: 3 @@ -210,26 +200,6 @@ parameters: count: 1 path: ../../../src/entity/Living.php - - - message: "#^Cannot call method getValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#" - count: 2 - path: ../../../src/entity/effect/SlownessEffect.php - - - - message: "#^Cannot call method setValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#" - count: 2 - path: ../../../src/entity/effect/SlownessEffect.php - - - - message: "#^Cannot call method getValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#" - count: 2 - path: ../../../src/entity/effect/SpeedEffect.php - - - - message: "#^Cannot call method setValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#" - count: 2 - path: ../../../src/entity/effect/SpeedEffect.php - - message: "#^Method pocketmine\\\\entity\\\\object\\\\Painting\\:\\:getMotive\\(\\) should return pocketmine\\\\entity\\\\object\\\\PaintingMotive but returns pocketmine\\\\entity\\\\object\\\\PaintingMotive\\|null\\.$#" count: 1