PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
ItemRegistryPacket.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\LE;
20use pmmp\encoding\VarInt;
24use function count;
25
27 public const NETWORK_ID = ProtocolInfo::ITEM_REGISTRY_PACKET;
28
33 private array $entries;
34
40 public static function create(array $entries) : self{
41 $result = new self;
42 $result->entries = $entries;
43 return $result;
44 }
45
50 public function getEntries() : array{ return $this->entries; }
51
52 protected function decodePayload(ByteBufferReader $in) : void{
53 $this->entries = [];
54 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
55 $stringId = CommonTypes::getString($in);
56 $numericId = LE::readSignedShort($in);
57 $isComponentBased = CommonTypes::getBool($in);
58 $version = VarInt::readSignedInt($in);
59 $nbt = CommonTypes::getNbtCompoundRoot($in);
60 $this->entries[] = new ItemTypeEntry($stringId, $numericId, $isComponentBased, $version, new CacheableNbt($nbt));
61 }
62 }
63
64 protected function encodePayload(ByteBufferWriter $out) : void{
65 VarInt::writeUnsignedInt($out, count($this->entries));
66 foreach($this->entries as $entry){
67 CommonTypes::putString($out, $entry->getStringId());
68 LE::writeSignedShort($out, $entry->getNumericId());
69 CommonTypes::putBool($out, $entry->isComponentBased());
70 VarInt::writeSignedInt($out, $entry->getVersion());
71 $out->writeByteArray($entry->getComponentNbt()->getEncodedNbt());
72 }
73 }
74
75 public function handle(PacketHandlerInterface $handler) : bool{
76 return $handler->handleItemRegistry($this);
77 }
78}