From d88b32da91392c68eeca3c5b621fae3362ec9945 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 19 Oct 2018 13:43:01 +0100 Subject: [PATCH] Drop useless numeric IDs for attributes, use strings instead --- src/pocketmine/entity/Attribute.php | 85 +++++++------------ src/pocketmine/entity/AttributeMap.php | 10 +-- .../network/mcpe/NetworkBinaryStream.php | 8 +- .../network/mcpe/protocol/AddEntityPacket.php | 8 +- .../mcpe/protocol/UpdateAttributesPacket.php | 2 +- 5 files changed, 46 insertions(+), 67 deletions(-) diff --git a/src/pocketmine/entity/Attribute.php b/src/pocketmine/entity/Attribute.php index 835a2debe..c4dd3477b 100644 --- a/src/pocketmine/entity/Attribute.php +++ b/src/pocketmine/entity/Attribute.php @@ -24,26 +24,26 @@ declare(strict_types=1); namespace pocketmine\entity; class Attribute{ + public const MC_PREFIX = "minecraft:"; - public const ABSORPTION = 0; - public const SATURATION = 1; - public const EXHAUSTION = 2; - public const KNOCKBACK_RESISTANCE = 3; - public const HEALTH = 4; - public const MOVEMENT_SPEED = 5; - public const FOLLOW_RANGE = 6; - public const HUNGER = 7; - public const FOOD = 7; - public const ATTACK_DAMAGE = 8; - public const EXPERIENCE_LEVEL = 9; - public const EXPERIENCE = 10; + public const ABSORPTION = self::MC_PREFIX . "absorption"; + public const SATURATION = self::MC_PREFIX . "player.saturation"; + public const EXHAUSTION = self::MC_PREFIX . "player.exhaustion"; + public const KNOCKBACK_RESISTANCE = self::MC_PREFIX . "knockback_resistance"; + public const HEALTH = self::MC_PREFIX . "health"; + public const MOVEMENT_SPEED = self::MC_PREFIX . "movement"; + public const FOLLOW_RANGE = self::MC_PREFIX . "follow_range"; + public const HUNGER = self::MC_PREFIX . "player.hunger"; + public const FOOD = self::HUNGER; + public const ATTACK_DAMAGE = self::MC_PREFIX . "attack_damage"; + public const EXPERIENCE_LEVEL = self::MC_PREFIX . "player.level"; + public const EXPERIENCE = self::MC_PREFIX . "player.experience"; - private $id; + protected $id; protected $minValue; protected $maxValue; protected $defaultValue; protected $currentValue; - protected $name; protected $shouldSend; protected $desynchronized = true; @@ -52,24 +52,23 @@ class Attribute{ protected static $attributes = []; public static function init() : void{ - self::addAttribute(self::ABSORPTION, "minecraft:absorption", 0.00, 340282346638528859811704183484516925440.00, 0.00); - self::addAttribute(self::SATURATION, "minecraft:player.saturation", 0.00, 20.00, 20.00); - self::addAttribute(self::EXHAUSTION, "minecraft:player.exhaustion", 0.00, 5.00, 0.0); - self::addAttribute(self::KNOCKBACK_RESISTANCE, "minecraft:knockback_resistance", 0.00, 1.00, 0.00); - self::addAttribute(self::HEALTH, "minecraft:health", 0.00, 20.00, 20.00); - self::addAttribute(self::MOVEMENT_SPEED, "minecraft:movement", 0.00, 340282346638528859811704183484516925440.00, 0.10); - self::addAttribute(self::FOLLOW_RANGE, "minecraft:follow_range", 0.00, 2048.00, 16.00, false); - self::addAttribute(self::HUNGER, "minecraft:player.hunger", 0.00, 20.00, 20.00); - self::addAttribute(self::ATTACK_DAMAGE, "minecraft:attack_damage", 0.00, 340282346638528859811704183484516925440.00, 1.00, false); - self::addAttribute(self::EXPERIENCE_LEVEL, "minecraft:player.level", 0.00, 24791.00, 0.00); - self::addAttribute(self::EXPERIENCE, "minecraft:player.experience", 0.00, 1.00, 0.00); + 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); + 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); //TODO: minecraft:luck (for fishing?) //TODO: minecraft:fall_damage } /** - * @param int $id - * @param string $name + * @param string $id * @param float $minValue * @param float $maxValue * @param float $defaultValue @@ -79,41 +78,25 @@ class Attribute{ * * @throws \InvalidArgumentException */ - public static function addAttribute(int $id, string $name, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true) : Attribute{ + public static function addAttribute(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"); } - return self::$attributes[$id] = new Attribute($id, $name, $minValue, $maxValue, $defaultValue, $shouldSend); + return self::$attributes[$id] = new Attribute($id, $minValue, $maxValue, $defaultValue, $shouldSend); } /** - * @param int $id + * @param string $id * * @return Attribute|null */ - public static function getAttribute(int $id) : ?Attribute{ + public static function getAttribute(string $id) : ?Attribute{ return isset(self::$attributes[$id]) ? clone self::$attributes[$id] : null; } - /** - * @param string $name - * - * @return Attribute|null - */ - public static function getAttributeByName(string $name) : ?Attribute{ - foreach(self::$attributes as $a){ - if($a->getName() === $name){ - return clone $a; - } - } - - return null; - } - - private function __construct(int $id, string $name, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){ + private function __construct(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){ $this->id = $id; - $this->name = $name; $this->minValue = $minValue; $this->maxValue = $maxValue; $this->defaultValue = $defaultValue; @@ -203,11 +186,7 @@ class Attribute{ return $this; } - public function getName() : string{ - return $this->name; - } - - public function getId() : int{ + public function getId() : string{ return $this->id; } diff --git a/src/pocketmine/entity/AttributeMap.php b/src/pocketmine/entity/AttributeMap.php index dd07a8619..5ff7e3392 100644 --- a/src/pocketmine/entity/AttributeMap.php +++ b/src/pocketmine/entity/AttributeMap.php @@ -32,11 +32,11 @@ class AttributeMap implements \ArrayAccess{ } /** - * @param int $id + * @param string $id * * @return Attribute|null */ - public function getAttribute(int $id) : ?Attribute{ + public function getAttribute(string $id) : ?Attribute{ return $this->attributes[$id] ?? null; } @@ -61,7 +61,7 @@ class AttributeMap implements \ArrayAccess{ } /** - * @param int $offset + * @param string $offset * * @return float */ @@ -70,8 +70,8 @@ class AttributeMap implements \ArrayAccess{ } /** - * @param int $offset - * @param float $value + * @param string $offset + * @param float $value */ public function offsetSet($offset, $value) : void{ $this->attributes[$offset]->setValue($value); diff --git a/src/pocketmine/network/mcpe/NetworkBinaryStream.php b/src/pocketmine/network/mcpe/NetworkBinaryStream.php index bb6101491..75139ad27 100644 --- a/src/pocketmine/network/mcpe/NetworkBinaryStream.php +++ b/src/pocketmine/network/mcpe/NetworkBinaryStream.php @@ -236,9 +236,9 @@ class NetworkBinaryStream extends BinaryStream{ $max = $this->getLFloat(); $current = $this->getLFloat(); $default = $this->getLFloat(); - $name = $this->getString(); + $id = $this->getString(); - $attr = Attribute::getAttributeByName($name); + $attr = Attribute::getAttribute($id); if($attr !== null){ $attr->setMinValue($min); $attr->setMaxValue($max); @@ -247,7 +247,7 @@ class NetworkBinaryStream extends BinaryStream{ $list[] = $attr; }else{ - throw new \UnexpectedValueException("Unknown attribute type \"$name\""); + throw new \UnexpectedValueException("Unknown attribute type \"$id\""); } } @@ -266,7 +266,7 @@ class NetworkBinaryStream extends BinaryStream{ $this->putLFloat($attribute->getMaxValue()); $this->putLFloat($attribute->getValue()); $this->putLFloat($attribute->getDefaultValue()); - $this->putString($attribute->getName()); + $this->putString($attribute->getId()); } } diff --git a/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php b/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php index 5ea109c7f..68d8df9bc 100644 --- a/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php @@ -69,11 +69,11 @@ class AddEntityPacket extends DataPacket{ $attrCount = $this->getUnsignedVarInt(); for($i = 0; $i < $attrCount; ++$i){ - $name = $this->getString(); + $id = $this->getString(); $min = $this->getLFloat(); $current = $this->getLFloat(); $max = $this->getLFloat(); - $attr = Attribute::getAttributeByName($name); + $attr = Attribute::getAttribute($id); if($attr !== null){ $attr->setMinValue($min); @@ -81,7 +81,7 @@ class AddEntityPacket extends DataPacket{ $attr->setValue($current); $this->attributes[] = $attr; }else{ - throw new \UnexpectedValueException("Unknown attribute type \"$name\""); + throw new \UnexpectedValueException("Unknown attribute type \"$id\""); } } @@ -104,7 +104,7 @@ class AddEntityPacket extends DataPacket{ $this->putUnsignedVarInt(count($this->attributes)); foreach($this->attributes as $attribute){ - $this->putString($attribute->getName()); + $this->putString($attribute->getId()); $this->putLFloat($attribute->getMinValue()); $this->putLFloat($attribute->getValue()); $this->putLFloat($attribute->getMaxValue()); diff --git a/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php b/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php index 77e1b9718..7d3277cd9 100644 --- a/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php @@ -44,7 +44,7 @@ class UpdateAttributesPacket extends DataPacket{ protected function encodePayload() : void{ $this->putEntityRuntimeId($this->entityRuntimeId); - $this->putAttributeList(...$this->entries); + $this->putAttributeList(...array_values($this->entries)); } public function handle(SessionHandler $handler) : bool{