PocketMine-MP 5.35.1 git-d241807752ab518f2ffc7e3b71f6a72f9cdf0c30
Loading...
Searching...
No Matches
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;
39use function count;
40
41class ChiseledBookshelf extends Tile implements Container{
43
44 private const TAG_LAST_INTERACTED_SLOT = "LastInteractedSlot"; //TAG_Int
45
46 private SimpleInventory $inventory;
47
48 private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
49
50 public function __construct(World $world, Vector3 $pos){
51 parent::__construct($world, $pos);
52 $this->inventory = new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
53 }
54
55 public function getInventory() : SimpleInventory{
56 return $this->inventory;
57 }
58
59 public function getRealInventory() : SimpleInventory{
60 return $this->inventory;
61 }
62
63 public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
64 return $this->lastInteractedSlot;
65 }
66
67 public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : void{
68 $this->lastInteractedSlot = $lastInteractedSlot;
69 }
70
71 public function readSaveData(CompoundTag $nbt) : void{
72 $this->loadItems($nbt);
73
74 $lastInteractedSlot = $nbt->getInt(self::TAG_LAST_INTERACTED_SLOT, 0);
75 if($lastInteractedSlot !== 0){
76 $this->lastInteractedSlot = ChiseledBookshelfSlot::tryFrom($lastInteractedSlot - 1);
77 }
78 }
79
80 protected function writeSaveData(CompoundTag $nbt) : void{
81 $this->saveItems($nbt);
82
83 $nbt->setInt(self::TAG_LAST_INTERACTED_SLOT, $this->lastInteractedSlot !== null ?
84 $this->lastInteractedSlot->value + 1 :
85 0
86 );
87 }
88
89 protected function loadItems(CompoundTag $tag) : void{
90 try{
91 $inventoryTag = $tag->getListTag(Container::TAG_ITEMS, CompoundTag::class);
92 }catch(UnexpectedTagTypeException){
93 //preserve the old behaviour of not throwing on wrong types
94 $inventoryTag = null;
95 }
96 if($inventoryTag !== null){
97 $inventory = $this->getRealInventory();
98 $listeners = $inventory->getListeners()->toArray();
99 $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
100
101 $newContents = [];
102 foreach($inventoryTag as $slot => $itemNBT){
103 try{
104 $count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT);
105 if($count === 0){
106 continue;
107 }
108 $newContents[$slot] = Item::nbtDeserialize($itemNBT);
109 }catch(SavedDataLoadingException $e){
110 //TODO: not the best solution
111 \GlobalLogger::get()->logException($e);
112 continue;
113 }
114 }
115 $inventory->setContents($newContents);
116
117 $inventory->getListeners()->add(...$listeners);
118 }
119
120 if(($lockTag = $tag->getTag(Container::TAG_LOCK)) instanceof StringTag){
121 $this->lock = $lockTag->getValue();
122 }
123 }
124
125 protected function saveItems(CompoundTag $tag) : void{
126 $items = [];
127 foreach($this->getRealInventory()->getContents(true) as $slot => $item){
128 if($item->isNull()){
129 $items[$slot] = CompoundTag::create()
130 ->setByte(SavedItemStackData::TAG_COUNT, 0)
131 ->setShort(SavedItemData::TAG_DAMAGE, 0)
132 ->setString(SavedItemData::TAG_NAME, "")
133 ->setByte(SavedItemStackData::TAG_WAS_PICKED_UP, 0);
134 }else{
135 $items[$slot] = $item->nbtSerialize();
136 }
137 }
138
139 $tag->setTag(Container::TAG_ITEMS, new ListTag($items, NBT::TAG_Compound));
140
141 if($this->lock !== null){
142 $tag->setString(Container::TAG_LOCK, $this->lock);
143 }
144 }
145}
setInt(string $name, int $value)