use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; class PlayerArmorDamagePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::PLAYER_ARMOR_DAMAGE_PACKET; private const FLAG_HEAD = 0; private const FLAG_CHEST = 1; private const FLAG_LEGS = 2; private const FLAG_FEET = 3; /** @var int|null */ private $headSlotDamage; /** @var int|null */ private $chestSlotDamage; /** @var int|null */ private $legsSlotDamage; /** @var int|null */ private $feetSlotDamage; public static function create(?int $headSlotDamage, ?int $chestSlotDamage, ?int $legsSlotDamage, ?int $feetSlotDamage) : self{ $result = new self; $result->headSlotDamage = $headSlotDamage; $result->chestSlotDamage = $chestSlotDamage; $result->legsSlotDamage = $legsSlotDamage; $result->feetSlotDamage = $feetSlotDamage; return $result; } public function getHeadSlotDamage() : ?int{ return $this->headSlotDamage; } public function getChestSlotDamage() : ?int{ return $this->chestSlotDamage; } public function getLegsSlotDamage() : ?int{ return $this->legsSlotDamage; } public function getFeetSlotDamage() : ?int{ return $this->feetSlotDamage; } private function maybeReadDamage(int $flags, int $flag, PacketSerializer $in) : ?int{ if(($flags & (1 << $flag)) !== 0){ return $in->getVarInt(); } return null; } protected function decodePayload(PacketSerializer $in) : void{ $flags = $in->getByte(); $this->headSlotDamage = $this->maybeReadDamage($flags, self::FLAG_HEAD, $in); $this->chestSlotDamage = $this->maybeReadDamage($flags, self::FLAG_CHEST, $in); $this->legsSlotDamage = $this->maybeReadDamage($flags, self::FLAG_LEGS, $in); $this->feetSlotDamage = $this->maybeReadDamage($flags, self::FLAG_FEET, $in); } private function composeFlag(?int $field, int $flag) : int{ return $field !== null ? (1 << $flag) : 0; } private function maybeWriteDamage(?int $field, PacketSerializer $out) : void{ if($field !== null){ $out->putVarInt($field); } } protected function encodePayload(PacketSerializer $out) : void{ $out->putByte( $this->composeFlag($this->headSlotDamage, self::FLAG_HEAD) | $this->composeFlag($this->chestSlotDamage, self::FLAG_CHEST) | $this->composeFlag($this->legsSlotDamage, self::FLAG_LEGS) | $this->composeFlag($this->feetSlotDamage, self::FLAG_FEET) ); $this->maybeWriteDamage($this->headSlotDamage, $out); $this->maybeWriteDamage($this->chestSlotDamage, $out); $this->maybeWriteDamage($this->legsSlotDamage, $out); $this->maybeWriteDamage($this->feetSlotDamage, $out); } public function handle(PacketHandlerInterface $handler) : bool{ return $handler->handlePlayerArmorDamage($this); } }