PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
ItemStackExtraData.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\types\inventory;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\LE;
26use function count;
27use function strlen;
28
39 public function __construct(
40 private ?CompoundTag $nbt,
41 private array $canPlaceOn,
42 private array $canDestroy
43 ){}
44
48 public function getCanPlaceOn() : array{
49 return $this->canPlaceOn;
50 }
51
55 public function getCanDestroy() : array{
56 return $this->canDestroy;
57 }
58
59 public function getNbt() : ?CompoundTag{
60 return $this->nbt;
61 }
62
63 public static function read(ByteBufferReader $in) : self{
64 $nbtLen = LE::readSignedShort($in);
65
67 $compound = null;
68 if($nbtLen === -1){
69 $nbtDataVersion = Byte::readUnsigned($in);
70 if($nbtDataVersion !== 1){
71 throw new PacketDecodeException("Unexpected NBT data version $nbtDataVersion");
72 }
73 $offset = $in->getOffset();
74 try{
75 $compound = (new LittleEndianNbtSerializer())->read($in->getData(), $offset, 512)->mustGetCompoundTag();
76 }catch(NbtDataException $e){
77 throw PacketDecodeException::wrap($e, "Failed decoding NBT root");
78 }finally{
79 $in->setOffset($offset);
80 }
81 }elseif($nbtLen !== 0){
82 throw new PacketDecodeException("Unexpected fake NBT length $nbtLen");
83 }
84
85 $canPlaceOn = [];
86 //TODO: apparently this is not correct as of 1.21.50
87 for($i = 0, $canPlaceOnCount = LE::readUnsignedInt($in); $i < $canPlaceOnCount; ++$i){
88 $canPlaceOn[] = $in->readByteArray(LE::readUnsignedShort($in));
89 }
90
91 $canDestroy = [];
92 for($i = 0, $canDestroyCount = LE::readUnsignedInt($in); $i < $canDestroyCount; ++$i){
93 $canDestroy[] = $in->readByteArray(LE::readUnsignedShort($in));
94 }
95
96 return new self($compound, $canPlaceOn, $canDestroy);
97 }
98
99 public function write(ByteBufferWriter $out) : void{
100 if($this->nbt !== null){
101 LE::writeSignedShort($out, 0xffff);
102 Byte::writeUnsigned($out, 1); //TODO: NBT data version (?)
103 $out->writeByteArray((new LittleEndianNbtSerializer())->write(new TreeRoot($this->nbt)));
104 }else{
105 LE::writeSignedShort($out, 0);
106 }
107
108 LE::writeUnsignedInt($out, count($this->canPlaceOn));
109 foreach($this->canPlaceOn as $entry){
110 LE::writeUnsignedShort($out, strlen($entry));
111 $out->writeByteArray($entry);
112 }
113 LE::writeUnsignedInt($out, count($this->canDestroy));
114 foreach($this->canDestroy as $entry){
115 LE::writeUnsignedShort($out, strlen($entry));
116 $out->writeByteArray($entry);
117 }
118 }
119}
__construct(private ?CompoundTag $nbt, private array $canPlaceOn, private array $canDestroy)