mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-29 14:49:59 +00:00
reduce boilerplate around attribute handling
This commit is contained in:
parent
dc33b9e573
commit
296061d25d
@ -60,22 +60,22 @@ class Attribute{
|
|||||||
protected static $attributes = [];
|
protected static $attributes = [];
|
||||||
|
|
||||||
public static function init() : void{
|
public static function init() : void{
|
||||||
self::addAttribute(self::ABSORPTION, 0.00, 340282346638528859811704183484516925440.00, 0.00);
|
self::register(self::ABSORPTION, 0.00, 340282346638528859811704183484516925440.00, 0.00);
|
||||||
self::addAttribute(self::SATURATION, 0.00, 20.00, 20.00);
|
self::register(self::SATURATION, 0.00, 20.00, 20.00);
|
||||||
self::addAttribute(self::EXHAUSTION, 0.00, 5.00, 0.0, false);
|
self::register(self::EXHAUSTION, 0.00, 5.00, 0.0, false);
|
||||||
self::addAttribute(self::KNOCKBACK_RESISTANCE, 0.00, 1.00, 0.00);
|
self::register(self::KNOCKBACK_RESISTANCE, 0.00, 1.00, 0.00);
|
||||||
self::addAttribute(self::HEALTH, 0.00, 20.00, 20.00);
|
self::register(self::HEALTH, 0.00, 20.00, 20.00);
|
||||||
self::addAttribute(self::MOVEMENT_SPEED, 0.00, 340282346638528859811704183484516925440.00, 0.10);
|
self::register(self::MOVEMENT_SPEED, 0.00, 340282346638528859811704183484516925440.00, 0.10);
|
||||||
self::addAttribute(self::FOLLOW_RANGE, 0.00, 2048.00, 16.00, false);
|
self::register(self::FOLLOW_RANGE, 0.00, 2048.00, 16.00, false);
|
||||||
self::addAttribute(self::HUNGER, 0.00, 20.00, 20.00);
|
self::register(self::HUNGER, 0.00, 20.00, 20.00);
|
||||||
self::addAttribute(self::ATTACK_DAMAGE, 0.00, 340282346638528859811704183484516925440.00, 1.00, false);
|
self::register(self::ATTACK_DAMAGE, 0.00, 340282346638528859811704183484516925440.00, 1.00, false);
|
||||||
self::addAttribute(self::EXPERIENCE_LEVEL, 0.00, 24791.00, 0.00);
|
self::register(self::EXPERIENCE_LEVEL, 0.00, 24791.00, 0.00);
|
||||||
self::addAttribute(self::EXPERIENCE, 0.00, 1.00, 0.00);
|
self::register(self::EXPERIENCE, 0.00, 1.00, 0.00);
|
||||||
self::addAttribute(self::UNDERWATER_MOVEMENT, 0.0, 340282346638528859811704183484516925440.0, 0.02);
|
self::register(self::UNDERWATER_MOVEMENT, 0.0, 340282346638528859811704183484516925440.0, 0.02);
|
||||||
self::addAttribute(self::LUCK, -1024.0, 1024.0, 0.0);
|
self::register(self::LUCK, -1024.0, 1024.0, 0.0);
|
||||||
self::addAttribute(self::FALL_DAMAGE, 0.0, 340282346638528859811704183484516925440.0, 1.0);
|
self::register(self::FALL_DAMAGE, 0.0, 340282346638528859811704183484516925440.0, 1.0);
|
||||||
self::addAttribute(self::HORSE_JUMP_STRENGTH, 0.0, 2.0, 0.7);
|
self::register(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::ZOMBIE_SPAWN_REINFORCEMENTS, 0.0, 1.0, 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,7 +89,7 @@ class Attribute{
|
|||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @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){
|
if($minValue > $maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){
|
||||||
throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue");
|
throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue");
|
||||||
}
|
}
|
||||||
@ -102,7 +102,7 @@ class Attribute{
|
|||||||
*
|
*
|
||||||
* @return Attribute|null
|
* @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;
|
return isset(self::$attributes[$id]) ? clone self::$attributes[$id] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ class AttributeMap implements \ArrayAccess{
|
|||||||
/** @var Attribute[] */
|
/** @var Attribute[] */
|
||||||
private $attributes = [];
|
private $attributes = [];
|
||||||
|
|
||||||
public function addAttribute(Attribute $attribute) : void{
|
public function add(Attribute $attribute) : void{
|
||||||
$this->attributes[$attribute->getId()] = $attribute;
|
$this->attributes[$attribute->getId()] = $attribute;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ class AttributeMap implements \ArrayAccess{
|
|||||||
*
|
*
|
||||||
* @return Attribute|null
|
* @return Attribute|null
|
||||||
*/
|
*/
|
||||||
public function getAttribute(string $id) : ?Attribute{
|
public function get(string $id) : ?Attribute{
|
||||||
return $this->attributes[$id] ?? null;
|
return $this->attributes[$id] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -390,7 +390,7 @@ abstract class Entity extends Location{
|
|||||||
public function setSprinting(bool $value = true) : void{
|
public function setSprinting(bool $value = true) : void{
|
||||||
if($value !== $this->isSprinting()){
|
if($value !== $this->isSprinting()){
|
||||||
$this->sprinting = $value;
|
$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);
|
$attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3), false, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,8 @@ class ExperienceManager{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
|
private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
|
||||||
$entity->getAttributeMap()->addAttribute(Attribute::getAttribute($attributeId));
|
$entity->getAttributeMap()->add(Attribute::get($attributeId));
|
||||||
return $entity->getAttributeMap()->getAttribute($attributeId);
|
return $entity->getAttributeMap()->get($attributeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,8 +57,8 @@ class HungerManager{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
|
private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
|
||||||
$entity->getAttributeMap()->addAttribute(Attribute::getAttribute($attributeId));
|
$entity->getAttributeMap()->add(Attribute::get($attributeId));
|
||||||
return $entity->getAttributeMap()->getAttribute($attributeId);
|
return $entity->getAttributeMap()->get($attributeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFood() : float{
|
public function getFood() : float{
|
||||||
|
@ -145,37 +145,37 @@ abstract class Living extends Entity{
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function addAttributes() : void{
|
protected function addAttributes() : void{
|
||||||
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::HEALTH));
|
$this->attributeMap->add(Attribute::get(Attribute::HEALTH));
|
||||||
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::FOLLOW_RANGE));
|
$this->attributeMap->add(Attribute::get(Attribute::FOLLOW_RANGE));
|
||||||
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::KNOCKBACK_RESISTANCE));
|
$this->attributeMap->add(Attribute::get(Attribute::KNOCKBACK_RESISTANCE));
|
||||||
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::MOVEMENT_SPEED));
|
$this->attributeMap->add(Attribute::get(Attribute::MOVEMENT_SPEED));
|
||||||
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ATTACK_DAMAGE));
|
$this->attributeMap->add(Attribute::get(Attribute::ATTACK_DAMAGE));
|
||||||
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ABSORPTION));
|
$this->attributeMap->add(Attribute::get(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->getAttribute(Attribute::HEALTH)->setValue(ceil($this->getHealth()), true);
|
$this->attributeMap->get(Attribute::HEALTH)->setValue(ceil($this->getHealth()), true);
|
||||||
if($this->isAlive() and !$wasAlive){
|
if($this->isAlive() and !$wasAlive){
|
||||||
$this->broadcastEntityEvent(ActorEventPacket::RESPAWN);
|
$this->broadcastEntityEvent(ActorEventPacket::RESPAWN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMaxHealth() : int{
|
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{
|
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{
|
public function getAbsorption() : float{
|
||||||
return $this->attributeMap->getAttribute(Attribute::ABSORPTION)->getValue();
|
return $this->attributeMap->get(Attribute::ABSORPTION)->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setAbsorption(float $absorption) : void{
|
public function setAbsorption(float $absorption) : void{
|
||||||
$this->attributeMap->getAttribute(Attribute::ABSORPTION)->setValue($absorption);
|
$this->attributeMap->get(Attribute::ABSORPTION)->setValue($absorption);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function saveNBT() : CompoundTag{
|
public function saveNBT() : CompoundTag{
|
||||||
@ -476,7 +476,7 @@ abstract class Living extends Entity{
|
|||||||
if($f <= 0){
|
if($f <= 0){
|
||||||
return;
|
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;
|
$f = 1 / $f;
|
||||||
|
|
||||||
$motion = clone $this->motion;
|
$motion = clone $this->motion;
|
||||||
|
@ -29,12 +29,12 @@ use pocketmine\entity\Living;
|
|||||||
class SlownessEffect extends Effect{
|
class SlownessEffect extends Effect{
|
||||||
|
|
||||||
public function add(Living $entity, EffectInstance $instance) : void{
|
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);
|
$attr->setValue($attr->getValue() * (1 - 0.15 * $instance->getEffectLevel()), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function remove(Living $entity, EffectInstance $instance) : void{
|
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()));
|
$attr->setValue($attr->getValue() / (1 - 0.15 * $instance->getEffectLevel()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,12 +29,12 @@ use pocketmine\entity\Living;
|
|||||||
class SpeedEffect extends Effect{
|
class SpeedEffect extends Effect{
|
||||||
|
|
||||||
public function add(Living $entity, EffectInstance $instance) : void{
|
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()));
|
$attr->setValue($attr->getValue() * (1 + 0.2 * $instance->getEffectLevel()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function remove(Living $entity, EffectInstance $instance) : void{
|
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()));
|
$attr->setValue($attr->getValue() / (1 + 0.2 * $instance->getEffectLevel()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,7 +189,7 @@ class AddActorPacket extends DataPacket implements ClientboundPacket{
|
|||||||
$min = $this->getLFloat();
|
$min = $this->getLFloat();
|
||||||
$current = $this->getLFloat();
|
$current = $this->getLFloat();
|
||||||
$max = $this->getLFloat();
|
$max = $this->getLFloat();
|
||||||
$attr = Attribute::getAttribute($id);
|
$attr = Attribute::get($id);
|
||||||
|
|
||||||
if($attr !== null){
|
if($attr !== null){
|
||||||
try{
|
try{
|
||||||
|
@ -299,7 +299,7 @@ class NetworkBinaryStream extends BinaryStream{
|
|||||||
$default = $this->getLFloat();
|
$default = $this->getLFloat();
|
||||||
$id = $this->getString();
|
$id = $this->getString();
|
||||||
|
|
||||||
$attr = Attribute::getAttribute($id);
|
$attr = Attribute::get($id);
|
||||||
if($attr !== null){
|
if($attr !== null){
|
||||||
$attr->setMinValue($min);
|
$attr->setMinValue($min);
|
||||||
$attr->setMaxValue($max);
|
$attr->setMaxValue($max);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user