From b76db739fdc3855d3f355f682ff471c8ca969a8b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 7 Dec 2024 14:45:57 +0000 Subject: [PATCH] Campfire block's inventory is now null if it hasn't been set in the world having this created by the block was unreliable anyway. If items were set into the block's created inventory before setting the block in the world, the campfire contents would get overridden when the block was next run through readStateFromWorld() anyway. There needs to be a deeper exploration of how to handle blocks with inventories without requiring plugins to interact with tiles. For now, this isn't the worst solution, but it's not the best either. --- src/block/Campfire.php | 35 ++++++++++------------------------- src/block/tile/Campfire.php | 4 +++- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/block/Campfire.php b/src/block/Campfire.php index 89661d8a7..ebde5ba3d 100644 --- a/src/block/Campfire.php +++ b/src/block/Campfire.php @@ -72,24 +72,7 @@ class Campfire extends Transparent{ * @deprecated This was added by mistake. It can't be relied on as the inventory won't be initialized if this block * has never been set in the world. */ - protected Inventory $inventory; - - public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo){ - parent::__construct($idInfo, $name, $typeInfo); - //TODO: this should never have been in the block - it creates problems for setting blocks in different positions - //as inventories aren't designed to be cloned - $this->inventory = self::createInventory(); - } - - /** - * @internal - * TODO: we need to explore why this is being created in multiple places - */ - public static function createInventory() : Inventory{ - $result = new SimpleInventory(4); - $result->setMaxStackSize(1); - return $result; - } + protected ?Inventory $inventory = null; /** * @var int[] slot => ticks @@ -109,7 +92,8 @@ class Campfire extends Transparent{ $this->inventory = $tile->getInventory(); $this->cookingTimes = $tile->getCookingTimes(); }else{ - $this->inventory = self::createInventory(); + $this->inventory = null; + $this->cookingTimes = []; } return $this; @@ -153,7 +137,7 @@ class Campfire extends Transparent{ * @deprecated This was added by mistake. It can't be relied on as the inventory won't be initialized if this block * has never been set in the world. */ - public function getInventory() : Inventory{ + public function getInventory() : ?Inventory{ return $this->inventory; } @@ -216,10 +200,11 @@ class Campfire extends Transparent{ return true; } - if($this->position->getWorld()->getServer()->getCraftingManager()->getFurnaceRecipeManager($this->getFurnaceType())->match($item) !== null){ + $inventory = $this->inventory; + if($inventory !== null && $this->position->getWorld()->getServer()->getCraftingManager()->getFurnaceRecipeManager($this->getFurnaceType())->match($item) !== null){ $ingredient = clone $item; $ingredient->setCount(1); - if(count($this->inventory->addItem($ingredient)) === 0){ + if(count($inventory->addItem($ingredient)) === 0){ $item->pop(); $this->position->getWorld()->addSound($this->position, new ItemFrameAddItemSound()); return true; @@ -254,8 +239,8 @@ class Campfire extends Transparent{ } public function onScheduledUpdate() : void{ - if($this->lit){ - $items = $this->inventory->getContents(); + if($this->lit && ($inventory = $this->inventory) !== null){ + $items = $inventory->getContents(); $furnaceType = $this->getFurnaceType(); $maxCookDuration = $furnaceType->getCookDurationTicks(); foreach($items as $slot => $item){ @@ -273,7 +258,7 @@ class Campfire extends Transparent{ continue; } - $this->inventory->setItem($slot, VanillaItems::AIR()); + $inventory->setItem($slot, VanillaItems::AIR()); $this->setCookingTime($slot, 0); $this->position->getWorld()->dropItem($this->position->add(0.5, 1, 0.5), $ev->getResult()); } diff --git a/src/block/tile/Campfire.php b/src/block/tile/Campfire.php index a40cf9eaf..d0e43569a 100644 --- a/src/block/tile/Campfire.php +++ b/src/block/tile/Campfire.php @@ -25,6 +25,7 @@ namespace pocketmine\block\tile; use pocketmine\block\Campfire as BlockCampfire; use pocketmine\inventory\Inventory; +use pocketmine\inventory\SimpleInventory; use pocketmine\item\Item; use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; @@ -51,7 +52,8 @@ class Campfire extends Spawnable implements ContainerTile{ public function __construct(World $world, Vector3 $pos){ parent::__construct($world, $pos); - $this->inventory = BlockCampfire::createInventory(); + $this->inventory = new SimpleInventory(4); + $this->inventory->setMaxStackSize(1); } public function getInventory() : Inventory{