44 private const TAG_LAST_INTERACTED_SLOT =
"LastInteractedSlot";
48 private ?ChiseledBookshelfSlot $lastInteractedSlot =
null;
51 parent::__construct($world, $pos);
52 $this->inventory =
new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
56 return $this->inventory;
60 return $this->inventory;
63 public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
64 return $this->lastInteractedSlot;
67 public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) :
void{
68 $this->lastInteractedSlot = $lastInteractedSlot;
71 public function readSaveData(
CompoundTag $nbt) :
void{
72 $this->loadItems($nbt);
74 $lastInteractedSlot = $nbt->getInt(self::TAG_LAST_INTERACTED_SLOT, 0);
75 if($lastInteractedSlot !== 0){
76 $this->lastInteractedSlot = ChiseledBookshelfSlot::tryFrom($lastInteractedSlot - 1);
81 $this->saveItems($nbt);
83 $nbt->
setInt(self::TAG_LAST_INTERACTED_SLOT, $this->lastInteractedSlot !==
null ?
84 $this->lastInteractedSlot->value + 1 :
89 protected function loadItems(
CompoundTag $tag) : void{
92 }
catch(UnexpectedTagTypeException){
96 if($inventoryTag !==
null){
97 $inventory = $this->getRealInventory();
102 foreach($inventoryTag as $slot => $itemNBT){
104 $count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT);
108 $newContents[$slot] = Item::nbtDeserialize($itemNBT);
109 }
catch(SavedDataLoadingException $e){
111 \GlobalLogger::get()->logException($e);
120 if(($lockTag = $tag->getTag(Container::TAG_LOCK)) instanceof StringTag){
121 $this->lock = $lockTag->getValue();
125 protected function saveItems(CompoundTag $tag) : void{
127 foreach($this->getRealInventory()->getContents(
true) as $slot => $item){
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);
135 $items[$slot] = $item->nbtSerialize();
139 $tag->setTag(Container::TAG_ITEMS,
new ListTag($items, NBT::TAG_Compound));
141 if($this->lock !==
null){
142 $tag->setString(Container::TAG_LOCK, $this->lock);