22declare(strict_types=1);
43 abstract public function getRealInventory() :
Inventory;
45 protected function loadItems(
CompoundTag $tag) :
void{
47 $inventoryTag = $tag->
getListTag(ContainerTile::TAG_ITEMS, CompoundTag::class);
52 if($inventoryTag !==
null){
53 $inventory = $this->getRealInventory();
54 $listeners = $inventory->getListeners()->toArray();
55 $inventory->getListeners()->remove(...$listeners);
58 $errorLogContext =
"Container (" . $this->
getPosition() .
")";
59 foreach($inventoryTag as $itemNBT){
60 $slotId = $itemNBT->getByte(SavedItemStackData::TAG_SLOT);
61 $newContents[$slotId] = Item::safeNbtDeserialize($itemNBT,
"$errorLogContext slot $slotId");
63 $inventory->setContents($newContents);
65 $inventory->getListeners()->add(...$listeners);
68 if(($lockTag = $tag->
getTag(ContainerTile::TAG_LOCK)) instanceof
StringTag){
69 $this->lock = $lockTag->getValue();
73 protected function saveItems(
CompoundTag $tag) :
void{
75 foreach($this->getRealInventory()->getContents() as $slot => $item){
76 $items[] = $item->nbtSerialize($slot);
79 $tag->
setTag(ContainerTile::TAG_ITEMS,
new ListTag($items, NBT::TAG_Compound));
81 if($this->lock !==
null){
82 $tag->
setString(ContainerTile::TAG_LOCK, $this->lock);
90 return $this->lock === null || $this->lock === $key;
102 $inv = $this->getRealInventory();
105 $world = $pos->getWorld();
106 $dropPos = $pos->add(0.5, 0.5, 0.5);
107 foreach($inv->getContents() as $k => $item){
108 $world->dropItem($dropPos, $item);
setString(string $name, string $value)
setTag(string $name, Tag $tag)
getListTag(string $name, string $tagClass=Tag::class)