PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
20use pocketmine\network\mcpe\protocol\types\hud\HudElement;
21use pocketmine\network\mcpe\protocol\types\hud\HudVisibility;
22use function count;
23
24class SetHudPacket extends DataPacket implements ClientboundPacket{
25 public const NETWORK_ID = ProtocolInfo::SET_HUD_PACKET;
26
28 private array $hudElements = [];
29 private HudVisibility $visibility;
30
35 public static function create(array $hudElements, HudVisibility $visibility) : self{
36 $result = new self;
37 $result->hudElements = $hudElements;
38 $result->visibility = $visibility;
39 return $result;
40 }
41
43 public function getHudElements() : array{ return $this->hudElements; }
44
45 public function getVisibility() : HudVisibility{ return $this->visibility; }
46
47 protected function decodePayload(ByteBufferReader $in) : void{
48 $this->hudElements = [];
49 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){
50 $this->hudElements[] = HudElement::fromPacket(VarInt::readSignedInt($in));
51 }
52 $this->visibility = HudVisibility::fromPacket(VarInt::readSignedInt($in));
53 }
54
55 protected function encodePayload(ByteBufferWriter $out) : void{
56 VarInt::writeUnsignedInt($out, count($this->hudElements));
57 foreach($this->hudElements as $element){
58 VarInt::writeSignedInt($out, $element->value);
59 }
60 VarInt::writeSignedInt($out, $this->visibility->value);
61 }
62
63 public function handle(PacketHandlerInterface $handler) : bool{
64 return $handler->handleSetHud($this);
65 }
66}
static create(array $hudElements, HudVisibility $visibility)
handle(PacketHandlerInterface $handler)