From 22425551ed004e51e9cbc264f5758f10e856bc28 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 18 May 2020 18:12:04 +0100 Subject: [PATCH] separate default attribute creation from Attribute into AttributeMap --- src/entity/Attribute.php | 36 +----------------- src/entity/AttributeFactory.php | 63 ++++++++++++++++++++++++++++++++ src/entity/EntityFactory.php | 1 - src/entity/ExperienceManager.php | 2 +- src/entity/HungerManager.php | 2 +- src/entity/Living.php | 12 +++--- 6 files changed, 72 insertions(+), 44 deletions(-) create mode 100644 src/entity/AttributeFactory.php diff --git a/src/entity/Attribute.php b/src/entity/Attribute.php index 38f22547f..68c00bbc2 100644 --- a/src/entity/Attribute.php +++ b/src/entity/Attribute.php @@ -63,44 +63,10 @@ class Attribute{ /** @var bool */ protected $desynchronized = true; - /** @var Attribute[] */ - protected static $attributes = []; - - public static function init() : void{ - self::register(self::ABSORPTION, 0.00, 340282346638528859811704183484516925440.00, 0.00); - self::register(self::SATURATION, 0.00, 20.00, 20.00); - self::register(self::EXHAUSTION, 0.00, 5.00, 0.0, false); - self::register(self::KNOCKBACK_RESISTANCE, 0.00, 1.00, 0.00); - self::register(self::HEALTH, 0.00, 20.00, 20.00); - self::register(self::MOVEMENT_SPEED, 0.00, 340282346638528859811704183484516925440.00, 0.10); - self::register(self::FOLLOW_RANGE, 0.00, 2048.00, 16.00, false); - self::register(self::HUNGER, 0.00, 20.00, 20.00); - self::register(self::ATTACK_DAMAGE, 0.00, 340282346638528859811704183484516925440.00, 1.00, false); - self::register(self::EXPERIENCE_LEVEL, 0.00, 24791.00, 0.00); - self::register(self::EXPERIENCE, 0.00, 1.00, 0.00); - self::register(self::UNDERWATER_MOVEMENT, 0.0, 340282346638528859811704183484516925440.0, 0.02); - self::register(self::LUCK, -1024.0, 1024.0, 0.0); - self::register(self::FALL_DAMAGE, 0.0, 340282346638528859811704183484516925440.0, 1.0); - self::register(self::HORSE_JUMP_STRENGTH, 0.0, 2.0, 0.7); - self::register(self::ZOMBIE_SPAWN_REINFORCEMENTS, 0.0, 1.0, 0.0); - } - - /** - * @throws \InvalidArgumentException - */ - public static function register(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true) : Attribute{ + public function __construct(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){ if($minValue > $maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){ throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue"); } - - return self::$attributes[$id] = new Attribute($id, $minValue, $maxValue, $defaultValue, $shouldSend); - } - - public static function get(string $id) : ?Attribute{ - return isset(self::$attributes[$id]) ? clone self::$attributes[$id] : null; - } - - private function __construct(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){ $this->id = $id; $this->minValue = $minValue; $this->maxValue = $maxValue; diff --git a/src/entity/AttributeFactory.php b/src/entity/AttributeFactory.php new file mode 100644 index 000000000..5a9cd77e3 --- /dev/null +++ b/src/entity/AttributeFactory.php @@ -0,0 +1,63 @@ +register(Attribute::ABSORPTION, 0.00, 340282346638528859811704183484516925440.00, 0.00); + $this->register(Attribute::SATURATION, 0.00, 20.00, 20.00); + $this->register(Attribute::EXHAUSTION, 0.00, 5.00, 0.0, false); + $this->register(Attribute::KNOCKBACK_RESISTANCE, 0.00, 1.00, 0.00); + $this->register(Attribute::HEALTH, 0.00, 20.00, 20.00); + $this->register(Attribute::MOVEMENT_SPEED, 0.00, 340282346638528859811704183484516925440.00, 0.10); + $this->register(Attribute::FOLLOW_RANGE, 0.00, 2048.00, 16.00, false); + $this->register(Attribute::HUNGER, 0.00, 20.00, 20.00); + $this->register(Attribute::ATTACK_DAMAGE, 0.00, 340282346638528859811704183484516925440.00, 1.00, false); + $this->register(Attribute::EXPERIENCE_LEVEL, 0.00, 24791.00, 0.00); + $this->register(Attribute::EXPERIENCE, 0.00, 1.00, 0.00); + $this->register(Attribute::UNDERWATER_MOVEMENT, 0.0, 340282346638528859811704183484516925440.0, 0.02); + $this->register(Attribute::LUCK, -1024.0, 1024.0, 0.0); + $this->register(Attribute::FALL_DAMAGE, 0.0, 340282346638528859811704183484516925440.0, 1.0); + $this->register(Attribute::HORSE_JUMP_STRENGTH, 0.0, 2.0, 0.7); + $this->register(Attribute::ZOMBIE_SPAWN_REINFORCEMENTS, 0.0, 1.0, 0.0); + } + + public function get(string $id) : ?Attribute{ + return isset($this->attributes[$id]) ? clone $this->attributes[$id] : null; + } + + /** + * @throws \InvalidArgumentException + */ + public function register(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true) : Attribute{ + return $this->attributes[$id] = new Attribute($id, $minValue, $maxValue, $defaultValue, $shouldSend); + } +} diff --git a/src/entity/EntityFactory.php b/src/entity/EntityFactory.php index a8119ed5d..0f4a83a8d 100644 --- a/src/entity/EntityFactory.php +++ b/src/entity/EntityFactory.php @@ -103,7 +103,6 @@ final class EntityFactory{ $this->register(Human::class, ['Human']); - Attribute::init(); PaintingMotive::init(); } diff --git a/src/entity/ExperienceManager.php b/src/entity/ExperienceManager.php index 89e7d26fc..0b8fb050c 100644 --- a/src/entity/ExperienceManager.php +++ b/src/entity/ExperienceManager.php @@ -59,7 +59,7 @@ class ExperienceManager{ } private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{ - $entity->getAttributeMap()->add(Attribute::get($attributeId)); + $entity->getAttributeMap()->add(AttributeFactory::getInstance()->get($attributeId)); return $entity->getAttributeMap()->get($attributeId); } diff --git a/src/entity/HungerManager.php b/src/entity/HungerManager.php index 8f8be0abd..331f8e96c 100644 --- a/src/entity/HungerManager.php +++ b/src/entity/HungerManager.php @@ -57,7 +57,7 @@ class HungerManager{ } private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{ - $entity->getAttributeMap()->add(Attribute::get($attributeId)); + $entity->getAttributeMap()->add(AttributeFactory::getInstance()->get($attributeId)); return $entity->getAttributeMap()->get($attributeId); } diff --git a/src/entity/Living.php b/src/entity/Living.php index c5cbf8949..a8b175d55 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -153,12 +153,12 @@ abstract class Living extends Entity{ } protected function addAttributes() : void{ - $this->attributeMap->add(Attribute::get(Attribute::HEALTH)); - $this->attributeMap->add(Attribute::get(Attribute::FOLLOW_RANGE)); - $this->attributeMap->add(Attribute::get(Attribute::KNOCKBACK_RESISTANCE)); - $this->attributeMap->add(Attribute::get(Attribute::MOVEMENT_SPEED)); - $this->attributeMap->add(Attribute::get(Attribute::ATTACK_DAMAGE)); - $this->attributeMap->add(Attribute::get(Attribute::ABSORPTION)); + $this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::HEALTH)); + $this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::FOLLOW_RANGE)); + $this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::KNOCKBACK_RESISTANCE)); + $this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::MOVEMENT_SPEED)); + $this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::ATTACK_DAMAGE)); + $this->attributeMap->add(AttributeFactory::getInstance()->get(Attribute::ABSORPTION)); } public function setHealth(float $amount) : void{