*/ private array $cookingTimes = []; public function __construct(World $world, Vector3 $pos){ parent::__construct($world, $pos); $this->inventory = new CampfireInventory($this->position); $this->inventory->getListeners()->add(CallbackInventoryListener::onAnyChange( static function(Inventory $unused) use ($world, $pos) : void{ $block = $world->getBlock($pos); if($block instanceof BlockCampfire){ $world->setBlock($pos, $block); } }) ); } public function getInventory() : CampfireInventory{ return $this->inventory; } public function getRealInventory() : CampfireInventory{ return $this->inventory; } /** * @return int[] * @phpstan-return array */ public function getCookingTimes() : array{ return $this->cookingTimes; } /** * @param int[] $cookingTimes * @phpstan-param array $cookingTimes */ public function setCookingTimes(array $cookingTimes) : void{ $this->cookingTimes = $cookingTimes; } public function readSaveData(CompoundTag $nbt) : void{ $items = []; $listeners = $this->inventory->getListeners()->toArray(); $this->inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization foreach([ [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME], [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME], [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME], [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME], ] as [$slot, $itemTag, $cookingTimeTag]){ if(($tag = $nbt->getTag($itemTag)) instanceof CompoundTag){ $items[$slot] = Item::nbtDeserialize($tag); } if(($tag = $nbt->getTag($cookingTimeTag)) instanceof IntTag){ $this->cookingTimes[$slot] = $tag->getValue(); } } $this->inventory->setContents($items); $this->inventory->getListeners()->add(...$listeners); } protected function writeSaveData(CompoundTag $nbt) : void{ foreach([ [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME], [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME], [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME], [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME], ] as [$slot, $itemTag, $cookingTimeTag]){ $item = $this->inventory->getItem($slot); if(!$item->isNull()){ $nbt->setTag($itemTag, $item->nbtSerialize()); if(isset($this->cookingTimes[$slot])){ $nbt->setInt($cookingTimeTag, $this->cookingTimes[$slot]); } } } } protected function addAdditionalSpawnData(CompoundTag $nbt) : void{ foreach([ 0 => self::TAG_FIRST_INPUT_ITEM, 1 => self::TAG_SECOND_INPUT_ITEM, 2 => self::TAG_THIRD_INPUT_ITEM, 3 => self::TAG_FOURTH_INPUT_ITEM ] as $slot => $tag){ $item = $this->inventory->getItem($slot); if(!$item->isNull()){ $nbt->setTag($tag, TypeConverter::getInstance()->getItemTranslator()->toNetworkNbt($item)); } } } }