Store attributes as local vars, reduce usage of AttributeMap

ideally we want attribute map to only be used for properties that need synchronization.
This commit is contained in:
Dylan K. Taylor 2020-05-21 19:18:00 +01:00
parent f93bc0739c
commit 74e1f6320a
5 changed files with 33 additions and 51 deletions

View File

@ -54,6 +54,14 @@ final class AttributeFactory{
return isset($this->attributes[$id]) ? clone $this->attributes[$id] : null; return isset($this->attributes[$id]) ? clone $this->attributes[$id] : null;
} }
public function mustGet(string $id) : Attribute{
$result = $this->get($id);
if($result === null){
throw new \InvalidArgumentException("Attribute $id is not registered");
}
return $result;
}
/** /**
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */

View File

@ -59,8 +59,9 @@ class ExperienceManager{
} }
private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{ private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
$entity->getAttributeMap()->add(AttributeFactory::getInstance()->get($attributeId)); $attribute = AttributeFactory::getInstance()->mustGet($attributeId);
return $entity->getAttributeMap()->get($attributeId); $entity->getAttributeMap()->add($attribute);
return $attribute;
} }
/** /**

View File

@ -57,8 +57,9 @@ class HungerManager{
} }
private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{ private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
$entity->getAttributeMap()->add(AttributeFactory::getInstance()->get($attributeId)); $attribute = AttributeFactory::getInstance()->get($attributeId);
return $entity->getAttributeMap()->get($attributeId); $entity->getAttributeMap()->add($attribute);
return $attribute;
} }
public function getFood() : float{ public function getFood() : float{

View File

@ -101,6 +101,13 @@ abstract class Living extends Entity{
/** @var int */ /** @var int */
protected $maxBreathTicks = self::DEFAULT_BREATH_TICKS; protected $maxBreathTicks = self::DEFAULT_BREATH_TICKS;
/** @var Attribute */
protected $healthAttr;
/** @var Attribute */
protected $absorptionAttr;
/** @var Attribute */
protected $knockbackResistanceAttr;
abstract public function getName() : string; abstract public function getName() : string;
protected function initEntity(CompoundTag $nbt) : void{ protected function initEntity(CompoundTag $nbt) : void{
@ -153,37 +160,37 @@ abstract class Living extends Entity{
} }
protected function addAttributes() : void{ protected function addAttributes() : void{
$this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::HEALTH)); $this->attributeMap->add($this->healthAttr = AttributeFactory::getInstance()->mustGet(Attribute::HEALTH));
$this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::FOLLOW_RANGE)); $this->attributeMap->add(AttributeFactory::getInstance()->mustGet(Attribute::FOLLOW_RANGE));
$this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::KNOCKBACK_RESISTANCE)); $this->attributeMap->add($this->knockbackResistanceAttr = AttributeFactory::getInstance()->mustGet(Attribute::KNOCKBACK_RESISTANCE));
$this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::MOVEMENT_SPEED)); $this->attributeMap->add(AttributeFactory::getInstance()->mustGet(Attribute::MOVEMENT_SPEED));
$this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::ATTACK_DAMAGE)); $this->attributeMap->add(AttributeFactory::getInstance()->mustGet(Attribute::ATTACK_DAMAGE));
$this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::ABSORPTION)); $this->attributeMap->add($this->absorptionAttr = AttributeFactory::getInstance()->mustGet(Attribute::ABSORPTION));
} }
public function setHealth(float $amount) : void{ public function setHealth(float $amount) : void{
$wasAlive = $this->isAlive(); $wasAlive = $this->isAlive();
parent::setHealth($amount); parent::setHealth($amount);
$this->attributeMap->get(Attribute::HEALTH)->setValue(ceil($this->getHealth()), true); $this->healthAttr->setValue(ceil($this->getHealth()), true);
if($this->isAlive() and !$wasAlive){ if($this->isAlive() and !$wasAlive){
$this->broadcastAnimation(new RespawnAnimation($this)); $this->broadcastAnimation(new RespawnAnimation($this));
} }
} }
public function getMaxHealth() : int{ public function getMaxHealth() : int{
return (int) $this->attributeMap->get(Attribute::HEALTH)->getMaxValue(); return (int) $this->healthAttr->getMaxValue();
} }
public function setMaxHealth(int $amount) : void{ public function setMaxHealth(int $amount) : void{
$this->attributeMap->get(Attribute::HEALTH)->setMaxValue($amount)->setDefaultValue($amount); $this->healthAttr->setMaxValue($amount)->setDefaultValue($amount);
} }
public function getAbsorption() : float{ public function getAbsorption() : float{
return $this->attributeMap->get(Attribute::ABSORPTION)->getValue(); return $this->absorptionAttr->getValue();
} }
public function setAbsorption(float $absorption) : void{ public function setAbsorption(float $absorption) : void{
$this->attributeMap->get(Attribute::ABSORPTION)->setValue($absorption); $this->absorptionAttr->setValue($absorption);
} }
public function saveNBT() : CompoundTag{ public function saveNBT() : CompoundTag{
@ -453,7 +460,7 @@ abstract class Living extends Entity{
if($f <= 0){ if($f <= 0){
return; return;
} }
if(mt_rand() / mt_getrandmax() > $this->getAttributeMap()->get(Attribute::KNOCKBACK_RESISTANCE)->getValue()){ if(mt_rand() / mt_getrandmax() > $this->knockbackResistanceAttr->getValue()){
$f = 1 / $f; $f = 1 / $f;
$motion = clone $this->motion; $motion = clone $this->motion;

View File

@ -185,16 +185,6 @@ parameters:
count: 1 count: 1
path: ../../../src/entity/Entity.php path: ../../../src/entity/Entity.php
-
message: "#^Parameter \\#1 \\$attribute of method pocketmine\\\\entity\\\\AttributeMap\\:\\:add\\(\\) expects pocketmine\\\\entity\\\\Attribute, pocketmine\\\\entity\\\\Attribute\\|null given\\.$#"
count: 1
path: ../../../src/entity/ExperienceManager.php
-
message: "#^Method pocketmine\\\\entity\\\\ExperienceManager\\:\\:fetchAttribute\\(\\) should return pocketmine\\\\entity\\\\Attribute but returns pocketmine\\\\entity\\\\Attribute\\|null\\.$#"
count: 1
path: ../../../src/entity/ExperienceManager.php
- -
message: "#^Parameter \\#1 \\$attribute of method pocketmine\\\\entity\\\\AttributeMap\\:\\:add\\(\\) expects pocketmine\\\\entity\\\\Attribute, pocketmine\\\\entity\\\\Attribute\\|null given\\.$#" message: "#^Parameter \\#1 \\$attribute of method pocketmine\\\\entity\\\\AttributeMap\\:\\:add\\(\\) expects pocketmine\\\\entity\\\\Attribute, pocketmine\\\\entity\\\\Attribute\\|null given\\.$#"
count: 1 count: 1
@ -205,31 +195,6 @@ parameters:
count: 1 count: 1
path: ../../../src/entity/HungerManager.php path: ../../../src/entity/HungerManager.php
-
message: "#^Parameter \\#1 \\$attribute of method pocketmine\\\\entity\\\\AttributeMap\\:\\:add\\(\\) expects pocketmine\\\\entity\\\\Attribute, pocketmine\\\\entity\\\\Attribute\\|null given\\.$#"
count: 6
path: ../../../src/entity/Living.php
-
message: "#^Cannot call method setValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#"
count: 2
path: ../../../src/entity/Living.php
-
message: "#^Cannot call method getMaxValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#"
count: 1
path: ../../../src/entity/Living.php
-
message: "#^Cannot call method setMaxValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#"
count: 1
path: ../../../src/entity/Living.php
-
message: "#^Cannot call method getValue\\(\\) on pocketmine\\\\entity\\\\Attribute\\|null\\.$#"
count: 2
path: ../../../src/entity/Living.php
- -
message: "#^Cannot call method getEffectLevel\\(\\) on pocketmine\\\\entity\\\\effect\\\\EffectInstance\\|null\\.$#" message: "#^Cannot call method getEffectLevel\\(\\) on pocketmine\\\\entity\\\\effect\\\\EffectInstance\\|null\\.$#"
count: 3 count: 3