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