PocketMine-MP 5.21.1 git-2ff647079265e7c600203af4fd902b15e99d49a4
SetHudPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
18use pocketmine\network\mcpe\protocol\types\hud\HudElement;
19use pocketmine\network\mcpe\protocol\types\hud\HudVisibility;
20use function count;
21
22class SetHudPacket extends DataPacket implements ClientboundPacket{
23 public const NETWORK_ID = ProtocolInfo::SET_HUD_PACKET;
24
26 private array $hudElements = [];
27 private HudVisibility $visibility;
28
33 public static function create(array $hudElements, HudVisibility $visibility) : self{
34 $result = new self;
35 $result->hudElements = $hudElements;
36 $result->visibility = $visibility;
37 return $result;
38 }
39
41 public function getHudElements() : array{ return $this->hudElements; }
42
43 public function getVisibility() : HudVisibility{ return $this->visibility; }
44
45 protected function decodePayload(PacketSerializer $in) : void{
46 $this->hudElements = [];
47 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){
48 $this->hudElements[] = HudElement::fromPacket($in->getByte());
49 }
50 $this->visibility = HudVisibility::fromPacket($in->getByte());
51 }
52
53 protected function encodePayload(PacketSerializer $out) : void{
54 $out->putUnsignedVarInt(count($this->hudElements));
55 foreach($this->hudElements as $element){
56 $out->putByte($element->value);
57 }
58 $out->putByte($this->visibility->value);
59 }
60
61 public function handle(PacketHandlerInterface $handler) : bool{
62 return $handler->handleSetHud($this);
63 }
64}
static create(array $hudElements, HudVisibility $visibility)
handle(PacketHandlerInterface $handler)