diff --git a/src/block/Anvil.php b/src/block/Anvil.php index 4b71bd594..59b8d17c9 100644 --- a/src/block/Anvil.php +++ b/src/block/Anvil.php @@ -37,6 +37,8 @@ use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; use pocketmine\world\BlockTransaction; +use pocketmine\world\sound\AnvilFallSound; +use pocketmine\world\sound\Sound; use function lcg_value; use function round; @@ -109,4 +111,8 @@ class Anvil extends Transparent implements Fallable{ } return true; } + + public function getLandSound() : ?Sound{ + return new AnvilFallSound(); + } } diff --git a/src/block/utils/Fallable.php b/src/block/utils/Fallable.php index 6c7973d8c..74f96ac8b 100644 --- a/src/block/utils/Fallable.php +++ b/src/block/utils/Fallable.php @@ -25,6 +25,7 @@ namespace pocketmine\block\utils; use pocketmine\block\Block; use pocketmine\entity\object\FallingBlock; +use pocketmine\world\sound\Sound; interface Fallable{ @@ -40,4 +41,9 @@ interface Fallable{ * Returns whether the block should be placed. */ public function onHitGround(FallingBlock $blockEntity) : bool; + + /** + * Returns the sound that will be played when FallingBlock hits the ground. + */ + public function getLandSound() : ?Sound; } diff --git a/src/block/utils/FallableTrait.php b/src/block/utils/FallableTrait.php index 522ea22b1..d13a7f794 100644 --- a/src/block/utils/FallableTrait.php +++ b/src/block/utils/FallableTrait.php @@ -30,6 +30,7 @@ use pocketmine\entity\object\FallingBlock; use pocketmine\math\Facing; use pocketmine\utils\AssumptionFailedError; use pocketmine\world\Position; +use pocketmine\world\sound\Sound; /** * This trait handles falling behaviour for blocks that need them. @@ -62,4 +63,8 @@ trait FallableTrait{ public function onHitGround(FallingBlock $blockEntity) : bool{ return true; } + + public function getLandSound() : ?Sound{ + return null; + } } diff --git a/src/entity/object/FallingBlock.php b/src/entity/object/FallingBlock.php index 46f1df102..4333b8933 100644 --- a/src/entity/object/FallingBlock.php +++ b/src/entity/object/FallingBlock.php @@ -42,6 +42,7 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityIds; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\world\format\io\GlobalBlockStateHandlers; +use pocketmine\world\sound\BlockBreakSound; use function abs; class FallingBlock extends Entity{ @@ -128,14 +129,20 @@ class FallingBlock extends Entity{ if($this->onGround || $blockTarget !== null){ $this->flagForDespawn(); + $blockResult = $blockTarget ?? $this->block; $block = $world->getBlock($pos); if(!$block->canBeReplaced() || !$world->isInWorld($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ()) || ($this->onGround && abs($this->location->y - $this->location->getFloorY()) > 0.001)){ $world->dropItem($this->location, $this->block->asItem()); + $world->addSound($pos->add(0.5, 0.5, 0.5), new BlockBreakSound($blockResult)); }else{ - $ev = new EntityBlockChangeEvent($this, $block, $blockTarget ?? $this->block); + $ev = new EntityBlockChangeEvent($this, $block, $blockResult); $ev->call(); if(!$ev->isCancelled()){ - $world->setBlock($pos, $ev->getTo()); + $b = $ev->getTo(); + $world->setBlock($pos, $b); + if($this->onGround && $b instanceof Fallable && ($sound = $b->getLandSound()) !== null){ + $world->addSound($pos->add(0.5, 0.5, 0.5), $sound); + } } } $hasUpdate = true;