From d7382074786cbc8cc96a1063b44554c02015c80c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 6 Apr 2025 19:17:43 +0100 Subject: [PATCH] Revert "Allow blocks to respond to the contents of their containers being updated" This reverts commit 4850bd5538214642b2e96b3f1b4954b1f21c5cdd. --- src/block/Block.php | 10 ---------- src/block/BrewingStand.php | 24 ++++++++++-------------- src/block/Campfire.php | 21 ++++++++------------- src/block/ChiseledBookshelf.php | 15 --------------- src/world/World.php | 1 - 5 files changed, 18 insertions(+), 53 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index e5d01f667..36e08fc0b 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -36,7 +36,6 @@ use pocketmine\data\runtime\RuntimeDataSizeCalculator; use pocketmine\data\runtime\RuntimeDataWriter; use pocketmine\entity\Entity; use pocketmine\entity\projectile\Projectile; -use pocketmine\inventory\Inventory; use pocketmine\item\enchantment\AvailableEnchantmentRegistry; use pocketmine\item\enchantment\ItemEnchantmentTagRegistry; use pocketmine\item\enchantment\ItemEnchantmentTags; @@ -519,15 +518,6 @@ class Block{ } - /** - * Called by the World when a change is detected in a container's inventory at the block's position. - * Use this to do visual updates on the block if needed. - * Don't do any expensive logic in here. It will be called every time a slot of the inventory changes. - */ - public function onContainerUpdate(Inventory $inventory) : void{ - - } - /** * Do actions when interacted by Item. Returns if it has done anything * diff --git a/src/block/BrewingStand.php b/src/block/BrewingStand.php index c43747e40..ca143e6b1 100644 --- a/src/block/BrewingStand.php +++ b/src/block/BrewingStand.php @@ -28,7 +28,6 @@ use pocketmine\block\tile\BrewingStand as TileBrewingStand; use pocketmine\block\utils\BrewingStandSlot; use pocketmine\block\utils\SupportType; use pocketmine\data\runtime\RuntimeDataDescriber; -use pocketmine\inventory\Inventory; use pocketmine\item\Item; use pocketmine\math\Axis; use pocketmine\math\AxisAlignedBB; @@ -115,22 +114,19 @@ class BrewingStand extends Transparent{ if($brewing->onUpdate()){ $world->scheduleDelayedBlockUpdate($this->position, 1); } - } - } - public function onContainerUpdate(Inventory $inventory) : void{ - $world = $this->position->getWorld(); - $changed = false; - foreach(BrewingStandSlot::cases() as $slot){ - $occupied = !$inventory->isSlotEmpty($slot->getSlotNumber()); - if($occupied !== $this->hasSlot($slot)){ - $this->setSlot($slot, $occupied); - $changed = true; + $changed = false; + foreach(BrewingStandSlot::cases() as $slot){ + $occupied = !$brewing->getInventory()->isSlotEmpty($slot->getSlotNumber()); + if($occupied !== $this->hasSlot($slot)){ + $this->setSlot($slot, $occupied); + $changed = true; + } } - } - if($changed){ - $world->setBlock($this->position, $this); + if($changed){ + $world->setBlock($this->position, $this); + } } } } diff --git a/src/block/Campfire.php b/src/block/Campfire.php index ce4c5f107..831e0043a 100644 --- a/src/block/Campfire.php +++ b/src/block/Campfire.php @@ -37,7 +37,6 @@ use pocketmine\entity\projectile\SplashPotion; use pocketmine\event\block\CampfireCookEvent; use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageEvent; -use pocketmine\inventory\Inventory; use pocketmine\item\Durable; use pocketmine\item\enchantment\VanillaEnchantments; use pocketmine\item\Item; @@ -67,6 +66,8 @@ class Campfire extends Transparent{ LightableTrait::describeBlockOnlyState as encodeLitState; } + private const UPDATE_INTERVAL_TICKS = 10; + /** * @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. @@ -243,7 +244,7 @@ class Campfire extends Transparent{ $furnaceType = $this->getFurnaceType(); $maxCookDuration = $furnaceType->getCookDurationTicks(); foreach($items as $slot => $item){ - $this->setCookingTime($slot, min($maxCookDuration, $this->getCookingTime($slot) + 1)); + $this->setCookingTime($slot, min($maxCookDuration, $this->getCookingTime($slot) + self::UPDATE_INTERVAL_TICKS)); if($this->getCookingTime($slot) >= $maxCookDuration){ $result = ($recipe = $this->position->getWorld()->getServer()->getCraftingManager()->getFurnaceRecipeManager($furnaceType)->match($item)) instanceof FurnaceRecipe ? @@ -262,25 +263,19 @@ class Campfire extends Transparent{ $this->position->getWorld()->dropItem($this->position->add(0.5, 1, 0.5), $ev->getResult()); } } - $tile = $this->position->getWorld()->getTile($this->position); - if($tile instanceof TileCampfire){ - //TODO: we probably need to rethink how these are tracked - $tile->setCookingTimes($this->cookingTimes); - } if(count($items) > 0){ $this->position->getWorld()->setBlock($this->position, $this); } if(mt_rand(1, 6) === 1){ $this->position->getWorld()->addSound($this->position, $furnaceType->getCookSound()); } - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 1); + $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, self::UPDATE_INTERVAL_TICKS); + }else{ + //make sure the visual state is updated when items are added + $this->position->getWorld()->setBlock($this->position, $this); } } - public function onContainerUpdate(Inventory $inventory) : void{ - $this->position->getWorld()->setBlock($this->position, $this); //update visual state - } - private function extinguish() : void{ $this->position->getWorld()->addSound($this->position, new FireExtinguishSound()); $this->position->getWorld()->setBlock($this->position, $this->setLit(false)); @@ -289,6 +284,6 @@ class Campfire extends Transparent{ private function ignite() : void{ $this->position->getWorld()->addSound($this->position, new FlintSteelSound()); $this->position->getWorld()->setBlock($this->position, $this->setLit(true)); - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 1); + $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, self::UPDATE_INTERVAL_TICKS); } } diff --git a/src/block/ChiseledBookshelf.php b/src/block/ChiseledBookshelf.php index 4c91f1d94..73c4861bf 100644 --- a/src/block/ChiseledBookshelf.php +++ b/src/block/ChiseledBookshelf.php @@ -28,7 +28,6 @@ use pocketmine\block\utils\ChiseledBookshelfSlot; use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\data\runtime\RuntimeDataDescriber; -use pocketmine\inventory\Inventory; use pocketmine\item\Book; use pocketmine\item\EnchantedBook; use pocketmine\item\Item; @@ -165,20 +164,6 @@ class ChiseledBookshelf extends Opaque{ return true; } - public function onContainerUpdate(Inventory $inventory) : void{ - $changed = false; - foreach(ChiseledBookshelfSlot::cases() as $case){ - $hasItem = !$inventory->isSlotEmpty($case->value); - if($this->hasSlot($case) !== $hasItem){ - $this->setSlot($case, $hasItem); - $changed = true; - } - } - if($changed){ - $this->position->getWorld()->setBlock($this->position, $this); - } - } - public function getDropsForCompatibleTool(Item $item) : array{ return []; } diff --git a/src/world/World.php b/src/world/World.php index 18b1e06c6..c3fdbfa8a 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -2910,7 +2910,6 @@ class World implements ChunkManager, InventoryListener{ private function notifyInventoryUpdate(Inventory $inventory) : void{ $blockPosition = $this->containerToBlockPositionMap[$inventory] ?? null; if($blockPosition !== null){ - $this->getBlock($blockPosition)->onContainerUpdate($inventory); $this->scheduleDelayedBlockUpdate($blockPosition, 1); } }