PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
tile/ChiseledBookshelf.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block\tile;
25
26use pocketmine\block\utils\ChiseledBookshelfSlot;
38use function count;
39
40class ChiseledBookshelf extends Tile implements Container{
42
43 private SimpleInventory $inventory;
44
45 public function __construct(World $world, Vector3 $pos){
46 parent::__construct($world, $pos);
47 $this->inventory = new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
48 }
49
50 public function getInventory() : SimpleInventory{
51 return $this->inventory;
52 }
53
54 public function getRealInventory() : SimpleInventory{
55 return $this->inventory;
56 }
57
58 public function readSaveData(CompoundTag $nbt) : void{
59 $this->loadItems($nbt);
60 }
61
62 public function writeSaveData(CompoundTag $nbt) : void{
63 $this->saveItems($nbt);
64 }
65
66 protected function loadItems(CompoundTag $tag) : void{
67 if(($inventoryTag = $tag->getTag(Container::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){
68 $inventory = $this->getRealInventory();
69 $listeners = $inventory->getListeners()->toArray();
70 $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
71
72 $newContents = [];
74 foreach($inventoryTag as $slot => $itemNBT){
75 try{
76 $count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT);
77 if($count === 0){
78 continue;
79 }
80 $newContents[$slot] = Item::nbtDeserialize($itemNBT);
82 //TODO: not the best solution
83 \GlobalLogger::get()->logException($e);
84 continue;
85 }
86 }
87 $inventory->setContents($newContents);
88
89 $inventory->getListeners()->add(...$listeners);
90 }
91
92 if(($lockTag = $tag->getTag(Container::TAG_LOCK)) instanceof StringTag){
93 $this->lock = $lockTag->getValue();
94 }
95 }
96
97 protected function saveItems(CompoundTag $tag) : void{
98 $items = [];
99 foreach($this->getRealInventory()->getContents(true) as $slot => $item){
100 if($item->isNull()){
101 $items[$slot] = CompoundTag::create()
102 ->setByte(SavedItemStackData::TAG_COUNT, 0)
103 ->setShort(SavedItemData::TAG_DAMAGE, 0)
104 ->setString(SavedItemData::TAG_NAME, "")
105 ->setByte(SavedItemStackData::TAG_WAS_PICKED_UP, 0);
106 }else{
107 $items[$slot] = $item->nbtSerialize();
108 }
109 }
110
111 $tag->setTag(Container::TAG_ITEMS, new ListTag($items, NBT::TAG_Compound));
112
113 if($this->lock !== null){
114 $tag->setString(Container::TAG_LOCK, $this->lock);
115 }
116 }
117}
static nbtDeserialize(CompoundTag $tag)
Definition: Item.php:740