22declare(strict_types=1);
43 abstract public function getRealInventory() :
Inventory;
45 protected function loadItems(
CompoundTag $tag) :
void{
46 if(($inventoryTag = $tag->
getTag(Container::TAG_ITEMS)) instanceof
ListTag && $inventoryTag->
getTagType() === NBT::TAG_Compound){
47 $inventory = $this->getRealInventory();
48 $listeners = $inventory->getListeners()->toArray();
49 $inventory->getListeners()->remove(...$listeners);
53 foreach($inventoryTag as $itemNBT){
55 $newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT);
58 \GlobalLogger::get()->logException($e);
62 $inventory->setContents($newContents);
64 $inventory->getListeners()->add(...$listeners);
67 if(($lockTag = $tag->
getTag(Container::TAG_LOCK)) instanceof
StringTag){
68 $this->lock = $lockTag->getValue();
72 protected function saveItems(
CompoundTag $tag) :
void{
74 foreach($this->getRealInventory()->getContents() as $slot => $item){
75 $items[] = $item->nbtSerialize($slot);
78 $tag->
setTag(Container::TAG_ITEMS,
new ListTag($items, NBT::TAG_Compound));
80 if($this->lock !==
null){
81 $tag->
setString(Container::TAG_LOCK, $this->lock);
89 return $this->lock === null || $this->lock === $key;
101 $inv = $this->getRealInventory();
104 $world = $pos->getWorld();
105 $dropPos = $pos->add(0.5, 0.5, 0.5);
106 foreach($inv->getContents() as $k => $item){
107 $world->dropItem($dropPos, $item);
setString(string $name, string $value)
setTag(string $name, Tag $tag)