Standardised attributes encoding

This commit is contained in:
Dylan K. Taylor 2017-05-10 10:48:53 +01:00
parent 713f3facf9
commit c51c8ae700
3 changed files with 51 additions and 16 deletions

View File

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

View File

@ -23,6 +23,7 @@ namespace pocketmine\network\mcpe\protocol;
#include <rules/DataPacket.h>
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

View File

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