PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
ItemComponentPacket.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
20use function count;
21
23 public const NETWORK_ID = ProtocolInfo::ITEM_COMPONENT_PACKET;
24
29 private array $entries;
30
36 public static function create(array $entries) : self{
37 $result = new self;
38 $result->entries = $entries;
39 return $result;
40 }
41
46 public function getEntries() : array{ return $this->entries; }
47
48 protected function decodePayload(PacketSerializer $in) : void{
49 $this->entries = [];
50 for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
51 $name = $in->getString();
52 $nbt = $in->getNbtCompoundRoot();
53 $this->entries[] = new ItemComponentPacketEntry($name, new CacheableNbt($nbt));
54 }
55 }
56
57 protected function encodePayload(PacketSerializer $out) : void{
58 $out->putUnsignedVarInt(count($this->entries));
59 foreach($this->entries as $entry){
60 $out->putString($entry->getName());
61 $out->put($entry->getComponentNbt()->getEncodedNbt());
62 }
63 }
64
65 public function handle(PacketHandlerInterface $handler) : bool{
66 return $handler->handleItemComponent($this);
67 }
68}