22declare(strict_types=1);
44 abstract public function getRealInventory() :
Inventory;
46 protected function loadItems(
CompoundTag $tag) :
void{
48 $inventoryTag = $tag->
getListTag(Container::TAG_ITEMS, CompoundTag::class);
53 if($inventoryTag !==
null){
54 $inventory = $this->getRealInventory();
55 $listeners = $inventory->getListeners()->toArray();
56 $inventory->getListeners()->remove(...$listeners);
59 foreach($inventoryTag as $itemNBT){
61 $newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT);
64 \GlobalLogger::get()->logException($e);
68 $inventory->setContents($newContents);
70 $inventory->getListeners()->add(...$listeners);
73 if(($lockTag = $tag->
getTag(Container::TAG_LOCK)) instanceof
StringTag){
74 $this->lock = $lockTag->getValue();
78 protected function saveItems(
CompoundTag $tag) :
void{
80 foreach($this->getRealInventory()->getContents() as $slot => $item){
81 $items[] = $item->nbtSerialize($slot);
84 $tag->
setTag(Container::TAG_ITEMS,
new ListTag($items, NBT::TAG_Compound));
86 if($this->lock !==
null){
87 $tag->
setString(Container::TAG_LOCK, $this->lock);
95 return $this->lock === null || $this->lock === $key;
107 $inv = $this->getRealInventory();
110 $world = $pos->getWorld();
111 $dropPos = $pos->add(0.5, 0.5, 0.5);
112 foreach($inv->getContents() as $k => $item){
113 $world->dropItem($dropPos, $item);
setString(string $name, string $value)
setTag(string $name, Tag $tag)
getListTag(string $name, string $tagClass=Tag::class)