getTag(Container::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){ $inventory = $this->getRealInventory(); $listeners = $inventory->getListeners()->toArray(); $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization $newContents = []; /** @var CompoundTag $itemNBT */ foreach($inventoryTag as $itemNBT){ try{ $newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT); }catch(SavedDataLoadingException $e){ //TODO: not the best solution \GlobalLogger::get()->logException($e); continue; } } $inventory->setContents($newContents); $inventory->getListeners()->add(...$listeners); } if(($lockTag = $tag->getTag(Container::TAG_LOCK)) instanceof StringTag){ $this->lock = $lockTag->getValue(); } } 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() */ public function canOpenWith(string $key) : bool{ return $this->lock === null || $this->lock === $key; } /** * @see Position::asPosition() */ abstract protected function getPosition() : Position; /** * @see Tile::onBlockDestroyedHook() */ protected function onBlockDestroyedHook() : void{ $inv = $this->getRealInventory(); $pos = $this->getPosition(); $world = $pos->getWorld(); $dropPos = $pos->add(0.5, 0.5, 0.5); foreach($inv->getContents() as $k => $item){ $world->dropItem($dropPos, $item); } $inv->clearAll(); } }