From d43fb7310bd9d69653970f76a738ee64e0b2d018 Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Sat, 3 Jun 2023 10:32:26 -0500 Subject: [PATCH 1/5] Always eject disc when jukebox is destroyed --- src/block/Block.php | 13 ++++++++++--- src/block/Jukebox.php | 6 +++--- src/world/Explosion.php | 4 +--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index f2268aed931..024a664016a 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -427,12 +427,19 @@ public function getBreakInfo() : BlockBreakInfo{ * @param Item[] &$returnedItems Items to be added to the target's inventory (or dropped, if full) */ public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []) : bool{ + $this->onDestroy(); + $world->setBlock($this->position, VanillaBlocks::AIR()); + return true; + } + + /** + * Called when this block is destroyed either when a player breaks it or is hit by an explosion. + */ + public function onDestroy() : void{ $world = $this->position->getWorld(); if(($t = $world->getTile($this->position)) !== null){ - $t->onBlockDestroyed(); + $t->onBlockDestroyed(); //needed to create drops for inventories } - $world->setBlock($this->position, VanillaBlocks::AIR()); - return true; } /** diff --git a/src/block/Jukebox.php b/src/block/Jukebox.php index 20c3cab6131..50c238ec864 100644 --- a/src/block/Jukebox.php +++ b/src/block/Jukebox.php @@ -84,9 +84,9 @@ public function stopSound() : void{ $this->getPosition()->getWorld()->addSound($this->getPosition(), new RecordStopSound()); } - public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []) : bool{ - $this->stopSound(); - return parent::onBreak($item, $player, $returnedItems); + public function onDestroy() : void{ + $this->ejectRecord(); + parent::onDestroy(); } public function getDropsForCompatibleTool(Item $item) : array{ diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 601f9109e27..117f4a6a53b 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -209,9 +209,7 @@ public function explodeB() : bool{ $this->world->dropItem($pos->add(0.5, 0.5, 0.5), $drop); } } - if(($t = $this->world->getTileAt($pos->x, $pos->y, $pos->z)) !== null){ - $t->onBlockDestroyed(); //needed to create drops for inventories - } + $block->onDestroy(); $this->world->setBlockAt($pos->x, $pos->y, $pos->z, $airBlock); } } From d581c5810dda52de68b0ba4c3d95f6ffe7454ff9 Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Sun, 4 Jun 2023 15:18:57 -0500 Subject: [PATCH 2/5] Revert dylan's solution --- src/block/tile/Jukebox.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/block/tile/Jukebox.php b/src/block/tile/Jukebox.php index 54acd60eeb8..a94072bb191 100644 --- a/src/block/tile/Jukebox.php +++ b/src/block/tile/Jukebox.php @@ -27,7 +27,6 @@ use pocketmine\item\Record; use pocketmine\nbt\tag\CompoundTag; use pocketmine\network\mcpe\convert\TypeConverter; -use pocketmine\world\sound\RecordStopSound; class Jukebox extends Spawnable{ private const TAG_RECORD = "RecordItem"; //Item CompoundTag @@ -63,8 +62,4 @@ protected function addAdditionalSpawnData(CompoundTag $nbt) : void{ $nbt->setTag(self::TAG_RECORD, TypeConverter::getInstance()->getItemTranslator()->toNetworkNbt($this->record)); } } - - protected function onBlockDestroyedHook() : void{ - $this->position->getWorld()->addSound($this->position, new RecordStopSound()); - } } From a5882251042ce4fbd862a68bd77e148bad7d637c Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Sun, 4 Jun 2023 15:24:53 -0500 Subject: [PATCH 3/5] Oops --- src/block/Block.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index 024a664016a..3e694e4645e 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -428,7 +428,7 @@ public function getBreakInfo() : BlockBreakInfo{ */ public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []) : bool{ $this->onDestroy(); - $world->setBlock($this->position, VanillaBlocks::AIR()); + $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR()); return true; } @@ -436,8 +436,7 @@ public function onBreak(Item $item, ?Player $player = null, array &$returnedItem * Called when this block is destroyed either when a player breaks it or is hit by an explosion. */ public function onDestroy() : void{ - $world = $this->position->getWorld(); - if(($t = $world->getTile($this->position)) !== null){ + if(($t = $this->position->getWorld()->getTile($this->position)) !== null){ $t->onBlockDestroyed(); //needed to create drops for inventories } } From b4e17fa9606c3aa955d4d282f42909f13b0f323c Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Tue, 6 Jun 2023 21:09:18 -0500 Subject: [PATCH 4/5] Move `Block::onDestroy()` call to `World::destroyBlockInternal()` --- src/block/Block.php | 1 - src/world/World.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/block/Block.php b/src/block/Block.php index 3e694e4645e..e1b1dc6e1d3 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -427,7 +427,6 @@ public function getBreakInfo() : BlockBreakInfo{ * @param Item[] &$returnedItems Items to be added to the target's inventory (or dropped, if full) */ public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []) : bool{ - $this->onDestroy(); $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR()); return true; } diff --git a/src/world/World.php b/src/world/World.php index b2b6dfac2a1..87e89b4fec4 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -2012,6 +2012,7 @@ private function destroyBlockInternal(Block $target, Item $item, ?Player $player } $target->onBreak($item, $player, $returnedItems); + $target->onDestroy(); $tile = $this->getTile($target->getPosition()); if($tile !== null){ From a573060d48584b903478452e852e45d80e82bb5a Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Tue, 6 Jun 2023 21:14:37 -0500 Subject: [PATCH 5/5] Remove unnecessary call to `Tile::onBlockDestroyed()` This is already being called in `Block::onDestroy()` --- src/world/World.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/world/World.php b/src/world/World.php index 87e89b4fec4..c8ced25d066 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -2013,11 +2013,6 @@ private function destroyBlockInternal(Block $target, Item $item, ?Player $player $target->onBreak($item, $player, $returnedItems); $target->onDestroy(); - - $tile = $this->getTile($target->getPosition()); - if($tile !== null){ - $tile->onBlockDestroyed(); - } } /**