separate default attribute creation from Attribute into AttributeMap

This commit is contained in:
Dylan K. Taylor 2020-05-18 18:12:04 +01:00
parent 07f979fbde
commit 22425551ed
6 changed files with 72 additions and 44 deletions

View File

@ -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;

View File

@ -0,0 +1,63 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\entity;
use pocketmine\utils\SingletonTrait;
final class AttributeFactory{
use SingletonTrait;
/** @var Attribute[] */
private $attributes = [];
public function __construct(){
$this->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);
}
}

View File

@ -103,7 +103,6 @@ final class EntityFactory{
$this->register(Human::class, ['Human']);
Attribute::init();
PaintingMotive::init();
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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{