reduce boilerplate around attribute handling

This commit is contained in:
Dylan K. Taylor 2019-07-31 16:41:09 +01:00
parent dc33b9e573
commit 296061d25d
10 changed files with 43 additions and 43 deletions

View File

@ -60,22 +60,22 @@ class Attribute{
protected static $attributes = [];
public static function init() : void{
self::addAttribute(self::ABSORPTION, 0.00, 340282346638528859811704183484516925440.00, 0.00);
self::addAttribute(self::SATURATION, 0.00, 20.00, 20.00);
self::addAttribute(self::EXHAUSTION, 0.00, 5.00, 0.0, false);
self::addAttribute(self::KNOCKBACK_RESISTANCE, 0.00, 1.00, 0.00);
self::addAttribute(self::HEALTH, 0.00, 20.00, 20.00);
self::addAttribute(self::MOVEMENT_SPEED, 0.00, 340282346638528859811704183484516925440.00, 0.10);
self::addAttribute(self::FOLLOW_RANGE, 0.00, 2048.00, 16.00, false);
self::addAttribute(self::HUNGER, 0.00, 20.00, 20.00);
self::addAttribute(self::ATTACK_DAMAGE, 0.00, 340282346638528859811704183484516925440.00, 1.00, false);
self::addAttribute(self::EXPERIENCE_LEVEL, 0.00, 24791.00, 0.00);
self::addAttribute(self::EXPERIENCE, 0.00, 1.00, 0.00);
self::addAttribute(self::UNDERWATER_MOVEMENT, 0.0, 340282346638528859811704183484516925440.0, 0.02);
self::addAttribute(self::LUCK, -1024.0, 1024.0, 0.0);
self::addAttribute(self::FALL_DAMAGE, 0.0, 340282346638528859811704183484516925440.0, 1.0);
self::addAttribute(self::HORSE_JUMP_STRENGTH, 0.0, 2.0, 0.7);
self::addAttribute(self::ZOMBIE_SPAWN_REINFORCEMENTS, 0.0, 1.0, 0.0);
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);
}
/**
@ -89,7 +89,7 @@ class Attribute{
*
* @throws \InvalidArgumentException
*/
public static function addAttribute(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true) : Attribute{
public static function register(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true) : Attribute{
if($minValue > $maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){
throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue");
}
@ -102,7 +102,7 @@ class Attribute{
*
* @return Attribute|null
*/
public static function getAttribute(string $id) : ?Attribute{
public static function get(string $id) : ?Attribute{
return isset(self::$attributes[$id]) ? clone self::$attributes[$id] : null;
}

View File

@ -29,7 +29,7 @@ class AttributeMap implements \ArrayAccess{
/** @var Attribute[] */
private $attributes = [];
public function addAttribute(Attribute $attribute) : void{
public function add(Attribute $attribute) : void{
$this->attributes[$attribute->getId()] = $attribute;
}
@ -38,7 +38,7 @@ class AttributeMap implements \ArrayAccess{
*
* @return Attribute|null
*/
public function getAttribute(string $id) : ?Attribute{
public function get(string $id) : ?Attribute{
return $this->attributes[$id] ?? null;
}

View File

@ -390,7 +390,7 @@ abstract class Entity extends Location{
public function setSprinting(bool $value = true) : void{
if($value !== $this->isSprinting()){
$this->sprinting = $value;
$attr = $this->attributeMap->getAttribute(Attribute::MOVEMENT_SPEED);
$attr = $this->attributeMap->get(Attribute::MOVEMENT_SPEED);
$attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3), false, true);
}
}

View File

@ -58,8 +58,8 @@ class ExperienceManager{
}
private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
$entity->getAttributeMap()->addAttribute(Attribute::getAttribute($attributeId));
return $entity->getAttributeMap()->getAttribute($attributeId);
$entity->getAttributeMap()->add(Attribute::get($attributeId));
return $entity->getAttributeMap()->get($attributeId);
}
/**

View File

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

View File

@ -145,37 +145,37 @@ abstract class Living extends Entity{
}
protected function addAttributes() : void{
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::HEALTH));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::FOLLOW_RANGE));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::KNOCKBACK_RESISTANCE));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::MOVEMENT_SPEED));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ATTACK_DAMAGE));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ABSORPTION));
$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));
}
public function setHealth(float $amount) : void{
$wasAlive = $this->isAlive();
parent::setHealth($amount);
$this->attributeMap->getAttribute(Attribute::HEALTH)->setValue(ceil($this->getHealth()), true);
$this->attributeMap->get(Attribute::HEALTH)->setValue(ceil($this->getHealth()), true);
if($this->isAlive() and !$wasAlive){
$this->broadcastEntityEvent(ActorEventPacket::RESPAWN);
}
}
public function getMaxHealth() : int{
return (int) $this->attributeMap->getAttribute(Attribute::HEALTH)->getMaxValue();
return (int) $this->attributeMap->get(Attribute::HEALTH)->getMaxValue();
}
public function setMaxHealth(int $amount) : void{
$this->attributeMap->getAttribute(Attribute::HEALTH)->setMaxValue($amount)->setDefaultValue($amount);
$this->attributeMap->get(Attribute::HEALTH)->setMaxValue($amount)->setDefaultValue($amount);
}
public function getAbsorption() : float{
return $this->attributeMap->getAttribute(Attribute::ABSORPTION)->getValue();
return $this->attributeMap->get(Attribute::ABSORPTION)->getValue();
}
public function setAbsorption(float $absorption) : void{
$this->attributeMap->getAttribute(Attribute::ABSORPTION)->setValue($absorption);
$this->attributeMap->get(Attribute::ABSORPTION)->setValue($absorption);
}
public function saveNBT() : CompoundTag{
@ -476,7 +476,7 @@ abstract class Living extends Entity{
if($f <= 0){
return;
}
if(mt_rand() / mt_getrandmax() > $this->getAttributeMap()->getAttribute(Attribute::KNOCKBACK_RESISTANCE)->getValue()){
if(mt_rand() / mt_getrandmax() > $this->getAttributeMap()->get(Attribute::KNOCKBACK_RESISTANCE)->getValue()){
$f = 1 / $f;
$motion = clone $this->motion;

View File

@ -29,12 +29,12 @@ use pocketmine\entity\Living;
class SlownessEffect extends Effect{
public function add(Living $entity, EffectInstance $instance) : void{
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
$attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED);
$attr->setValue($attr->getValue() * (1 - 0.15 * $instance->getEffectLevel()), true);
}
public function remove(Living $entity, EffectInstance $instance) : void{
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
$attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED);
$attr->setValue($attr->getValue() / (1 - 0.15 * $instance->getEffectLevel()));
}
}

View File

@ -29,12 +29,12 @@ use pocketmine\entity\Living;
class SpeedEffect extends Effect{
public function add(Living $entity, EffectInstance $instance) : void{
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
$attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED);
$attr->setValue($attr->getValue() * (1 + 0.2 * $instance->getEffectLevel()));
}
public function remove(Living $entity, EffectInstance $instance) : void{
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
$attr = $entity->getAttributeMap()->get(Attribute::MOVEMENT_SPEED);
$attr->setValue($attr->getValue() / (1 + 0.2 * $instance->getEffectLevel()));
}
}

View File

@ -189,7 +189,7 @@ class AddActorPacket extends DataPacket implements ClientboundPacket{
$min = $this->getLFloat();
$current = $this->getLFloat();
$max = $this->getLFloat();
$attr = Attribute::getAttribute($id);
$attr = Attribute::get($id);
if($attr !== null){
try{

View File

@ -299,7 +299,7 @@ class NetworkBinaryStream extends BinaryStream{
$default = $this->getLFloat();
$id = $this->getString();
$attr = Attribute::getAttribute($id);
$attr = Attribute::get($id);
if($attr !== null){
$attr->setMinValue($min);
$attr->setMaxValue($max);