mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 02:08:21 +00:00
Standardised attributes encoding
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user