PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
23use function count;
24use function strlen;
25
36 public function __construct(
37 private ?CompoundTag $nbt,
38 private array $canPlaceOn,
39 private array $canDestroy
40 ){}
41
45 public function getCanPlaceOn() : array{
46 return $this->canPlaceOn;
47 }
48
52 public function getCanDestroy() : array{
53 return $this->canDestroy;
54 }
55
56 public function getNbt() : ?CompoundTag{
57 return $this->nbt;
58 }
59
60 public static function read(PacketSerializer $in) : self{
61 $nbtLen = $in->getLShort();
62
64 $compound = null;
65 if($nbtLen === 0xffff){
66 $nbtDataVersion = $in->getByte();
67 if($nbtDataVersion !== 1){
68 throw new PacketDecodeException("Unexpected NBT data version $nbtDataVersion");
69 }
70 $offset = $in->getOffset();
71 try{
72 $compound = (new LittleEndianNbtSerializer())->read($in->getBuffer(), $offset, 512)->mustGetCompoundTag();
73 }catch(NbtDataException $e){
74 throw PacketDecodeException::wrap($e, "Failed decoding NBT root");
75 }finally{
76 $in->setOffset($offset);
77 }
78 }elseif($nbtLen !== 0){
79 throw new PacketDecodeException("Unexpected fake NBT length $nbtLen");
80 }
81
82 $canPlaceOn = [];
83 for($i = 0, $canPlaceOnCount = $in->getLInt(); $i < $canPlaceOnCount; ++$i){
84 $canPlaceOn[] = $in->get($in->getLShort());
85 }
86
87 $canDestroy = [];
88 for($i = 0, $canDestroyCount = $in->getLInt(); $i < $canDestroyCount; ++$i){
89 $canDestroy[] = $in->get($in->getLShort());
90 }
91
92 return new self($compound, $canPlaceOn, $canDestroy);
93 }
94
95 public function write(PacketSerializer $out) : void{
96 if($this->nbt !== null){
97 $out->putLShort(0xffff);
98 $out->putByte(1); //TODO: NBT data version (?)
99 $out->put((new LittleEndianNbtSerializer())->write(new TreeRoot($this->nbt)));
100 }else{
101 $out->putLShort(0);
102 }
103
104 $out->putLInt(count($this->canPlaceOn));
105 foreach($this->canPlaceOn as $entry){
106 $out->putLShort(strlen($entry));
107 $out->put($entry);
108 }
109 $out->putLInt(count($this->canDestroy));
110 foreach($this->canDestroy as $entry){
111 $out->putLShort(strlen($entry));
112 $out->put($entry);
113 }
114 }
115}
__construct(private ?CompoundTag $nbt, private array $canPlaceOn, private array $canDestroy)