43 private const TAG_LAST_INTERACTED_SLOT =
"LastInteractedSlot";
47 private ?ChiseledBookshelfSlot $lastInteractedSlot =
null;
50 parent::__construct($world, $pos);
51 $this->inventory =
new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
55 return $this->inventory;
59 return $this->inventory;
62 public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
63 return $this->lastInteractedSlot;
66 public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) :
void{
67 $this->lastInteractedSlot = $lastInteractedSlot;
70 public function readSaveData(
CompoundTag $nbt) :
void{
71 $this->loadItems($nbt);
73 $lastInteractedSlot = $nbt->getInt(self::TAG_LAST_INTERACTED_SLOT, 0);
74 if($lastInteractedSlot !== 0){
75 $this->lastInteractedSlot = ChiseledBookshelfSlot::tryFrom($lastInteractedSlot - 1);
80 $this->saveItems($nbt);
82 $nbt->
setInt(self::TAG_LAST_INTERACTED_SLOT, $this->lastInteractedSlot !==
null ?
83 $this->lastInteractedSlot->value + 1 :
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();
96 foreach($inventoryTag as $slot => $itemNBT){
98 $count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT);
102 $newContents[$slot] = Item::nbtDeserialize($itemNBT);
105 \GlobalLogger::get()->logException($e);
114 if(($lockTag = $tag->
getTag(Container::TAG_LOCK)) instanceof StringTag){
115 $this->lock = $lockTag->getValue();
119 protected function saveItems(CompoundTag $tag) : void{
121 foreach($this->getRealInventory()->getContents(
true) as $slot => $item){
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);
129 $items[$slot] = $item->nbtSerialize();
133 $tag->setTag(Container::TAG_ITEMS,
new ListTag($items, NBT::TAG_Compound));
135 if($this->lock !==
null){
136 $tag->setString(Container::TAG_LOCK, $this->lock);