hasTag(Container::TAG_ITEMS, ListTag::class)){ $inventoryTag = $tag->getListTag(Container::TAG_ITEMS); $inventory = $this->getRealInventory(); $listeners = $inventory->getChangeListeners(); $inventory->removeChangeListeners(...$listeners); //prevent any events being fired by initialization $inventory->clearAll(); /** @var CompoundTag $itemNBT */ foreach($inventoryTag as $itemNBT){ $inventory->setItem($itemNBT->getByte("Slot"), Item::nbtDeserialize($itemNBT)); } $inventory->addChangeListeners(...$listeners); } if($tag->hasTag(Container::TAG_LOCK, StringTag::class)){ $this->lock = $tag->getString(Container::TAG_LOCK); } } protected function saveItems(CompoundTag $tag) : void{ $items = []; foreach($this->getRealInventory()->getContents() as $slot => $item){ $items[] = $item->nbtSerialize($slot); } $tag->setTag(Container::TAG_ITEMS, new ListTag($items, NBT::TAG_Compound)); if($this->lock !== null){ $tag->setString(Container::TAG_LOCK, $this->lock); } } /** * @see Container::canOpenWith() * * @param string $key * * @return bool */ public function canOpenWith(string $key) : bool{ return $this->lock === null or $this->lock === $key; } /** * @see Position::asPosition() * @return Position */ abstract protected function getPos() : Position; /** * @see Tile::onBlockDestroyedHook() */ protected function onBlockDestroyedHook() : void{ $inv = $this->getRealInventory(); $pos = $this->getPos(); foreach($inv->getContents() as $k => $item){ $pos->world->dropItem($pos->add(0.5, 0.5, 0.5), $item); } $inv->clearAll(false); } }