Implement anvil damage on fall (#5345)

This commit is contained in:
IvanCraft623 2022-12-15 14:12:18 -05:00 committed by GitHub
parent 8e600b4a78
commit 84f9136b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -58,4 +58,8 @@ trait FallableTrait{
public function tickFalling() : ?Block{
return null;
}
public function onHitGround(FallingBlock $blockEntity) : bool{
return true;
}
}

View File

@ -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;
}