From c51c8ae700e82722d617e6d3c820ffd765051c10 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 10 May 2017 10:48:53 +0100 Subject: [PATCH] Standardised attributes encoding --- .../network/mcpe/protocol/AddEntityPacket.php | 8 +-- .../network/mcpe/protocol/DataPacket.php | 49 +++++++++++++++++++ .../mcpe/protocol/UpdateAttributesPacket.php | 10 +--- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php b/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php index 09c47f7ba..b3e0c63c0 100644 --- a/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php @@ -57,13 +57,7 @@ class AddEntityPacket extends DataPacket{ $this->putVector3f($this->speedX, $this->speedY, $this->speedZ); $this->putLFloat($this->pitch); $this->putLFloat($this->yaw); - $this->putUnsignedVarInt(count($this->attributes)); - foreach($this->attributes as $entry){ - $this->putString($entry->getName()); - $this->putLFloat($entry->getMinValue()); - $this->putLFloat($entry->getValue()); - $this->putLFloat($entry->getMaxValue()); - } + $this->putAttributeList(...$this->attributes); $this->putEntityMetadata($this->metadata); $this->putUnsignedVarInt(count($this->links)); foreach($this->links as $link){ diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index f530eb179..a41a83aaf 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -23,6 +23,7 @@ namespace pocketmine\network\mcpe\protocol; #include +use pocketmine\entity\Attribute; use pocketmine\entity\Entity; use pocketmine\item\Item; use pocketmine\network\mcpe\NetworkSession; @@ -201,6 +202,54 @@ abstract class DataPacket extends BinaryStream{ } } + /** + * Reads a list of Attributes from the stream. + * @return Attribute[] + * + * @throws \UnexpectedValueException if reading an attribute with an unrecognized name + */ + public function getAttributeList() : array{ + $list = []; + $count = $this->getUnsignedVarInt(); + + for($i = 0; $i < $count; ++$i){ + $min = $this->getLFloat(); + $max = $this->getLFloat(); + $current = $this->getLFloat(); + $default = $this->getLFloat(); + $name = $this->getString(); + + $attr = Attribute::getAttributeByName($name); + if($attr !== null){ + $attr->setMinValue($min); + $attr->setMaxValue($max); + $attr->setValue($current); + $attr->setDefaultValue($default); + + $list[] = $attr; + }else{ + throw new \UnexpectedValueException("Unknown attribute type \"$name\""); + } + } + + return $list; + } + + /** + * Writes a list of Attributes to the packet buffer using the standard format. + * @param Attribute[] ...$attributes + */ + public function putAttributeList(Attribute ...$attributes){ + $this->putUnsignedVarInt(count($attributes)); + foreach($attributes as $attribute){ + $this->putLFloat($attribute->getMinValue()); + $this->putLFloat($attribute->getMaxValue()); + $this->putLFloat($attribute->getValue()); + $this->putLFloat($attribute->getDefaultValue()); + $this->putString($attribute->getName()); + } + } + /** * Reads and returns an EntityUniqueID * @return int|string diff --git a/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php b/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php index 9f369579f..68cd90e72 100644 --- a/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php @@ -30,7 +30,6 @@ use pocketmine\network\mcpe\NetworkSession; class UpdateAttributesPacket extends DataPacket{ const NETWORK_ID = ProtocolInfo::UPDATE_ATTRIBUTES_PACKET; - public $entityId; /** @var Attribute[] */ public $entries = []; @@ -42,14 +41,7 @@ class UpdateAttributesPacket extends DataPacket{ public function encode(){ $this->reset(); $this->putEntityRuntimeId($this->entityId); - $this->putUnsignedVarInt(count($this->entries)); - foreach($this->entries as $entry){ - $this->putLFloat($entry->getMinValue()); - $this->putLFloat($entry->getMaxValue()); - $this->putLFloat($entry->getValue()); - $this->putLFloat($entry->getDefaultValue()); - $this->putString($entry->getName()); - } + $this->putAttributeList(...$this->entries); } public function handle(NetworkSession $session) : bool{