diff --git a/src/block/Anvil.php b/src/block/Anvil.php index d9c407409..4b71bd594 100644 --- a/src/block/Anvil.php +++ b/src/block/Anvil.php @@ -30,12 +30,15 @@ use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\block\utils\SupportType; use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataWriter; +use pocketmine\entity\object\FallingBlock; use pocketmine\item\Item; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; use pocketmine\world\BlockTransaction; +use function lcg_value; +use function round; class Anvil extends Transparent implements Fallable{ use FallableTrait; @@ -95,4 +98,15 @@ class Anvil extends Transparent implements Fallable{ } return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } + + public function onHitGround(FallingBlock $blockEntity) : bool{ + if(lcg_value() < 0.05 + (round($blockEntity->getFallDistance()) - 1) * 0.05){ + if($this->damage !== self::VERY_DAMAGED){ + $this->damage = $this->damage + 1; + }else{ + return false; + } + } + return true; + } } diff --git a/src/block/utils/Fallable.php b/src/block/utils/Fallable.php index 2f191f1c2..6c7973d8c 100644 --- a/src/block/utils/Fallable.php +++ b/src/block/utils/Fallable.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\block\utils; use pocketmine\block\Block; +use pocketmine\entity\object\FallingBlock; interface Fallable{ @@ -33,4 +34,10 @@ interface Fallable{ * Return null if you don't want to change the usual behaviour. */ public function tickFalling() : ?Block; + + /** + * Called when FallingBlock hits the ground. + * Returns whether the block should be placed. + */ + public function onHitGround(FallingBlock $blockEntity) : bool; } diff --git a/src/block/utils/FallableTrait.php b/src/block/utils/FallableTrait.php index 8e878b172..522ea22b1 100644 --- a/src/block/utils/FallableTrait.php +++ b/src/block/utils/FallableTrait.php @@ -58,4 +58,8 @@ trait FallableTrait{ public function tickFalling() : ?Block{ return null; } + + public function onHitGround(FallingBlock $blockEntity) : bool{ + return true; + } } diff --git a/src/entity/object/FallingBlock.php b/src/entity/object/FallingBlock.php index 6b15ed9c8..46f1df102 100644 --- a/src/entity/object/FallingBlock.php +++ b/src/entity/object/FallingBlock.php @@ -145,6 +145,13 @@ class FallingBlock extends Entity{ return $hasUpdate; } + protected function onHitGround() : ?float{ + if($this->block instanceof Fallable && !$this->block->onHitGround($this)){ + $this->flagForDespawn(); + } + return null; + } + public function getBlock() : Block{ return $this->block; }