From 5226300b99d8fdd60f117f6d99b70038ce2d56ef Mon Sep 17 00:00:00 2001 From: IvanCraft623 <57236932+IvanCraft623@users.noreply.github.com> Date: Fri, 27 Jan 2023 18:07:41 -0500 Subject: [PATCH] Ring bell when hit by a projectile (#5505) --- src/block/Bell.php | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/block/Bell.php b/src/block/Bell.php index f9e61e43d..85c4ecaaf 100644 --- a/src/block/Bell.php +++ b/src/block/Bell.php @@ -29,9 +29,11 @@ use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\block\utils\SupportType; use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataWriter; +use pocketmine\entity\projectile\Projectile; use pocketmine\item\Item; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; +use pocketmine\math\RayTraceResult; use pocketmine\math\Vector3; use pocketmine\player\Player; use pocketmine\world\BlockTransaction; @@ -134,14 +136,7 @@ final class Bell extends Transparent{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($player !== null){ $faceHit = Facing::opposite($player->getHorizontalFacing()); - if( - $this->attachmentType->equals(BellAttachmentType::CEILING()) || - ($this->attachmentType->equals(BellAttachmentType::FLOOR()) && Facing::axis($faceHit) === Facing::axis($this->facing)) || - ( - ($this->attachmentType->equals(BellAttachmentType::ONE_WALL()) || $this->attachmentType->equals(BellAttachmentType::TWO_WALLS())) && - ($faceHit === Facing::rotateY($this->facing, false) || $faceHit === Facing::rotateY($this->facing, true)) - ) - ){ + if($this->isValidFaceToRing($faceHit)){ $this->ring($faceHit); return true; } @@ -150,6 +145,13 @@ final class Bell extends Transparent{ return false; } + public function onProjectileHit(Projectile $projectile, RayTraceResult $hitResult) : void{ + $faceHit = Facing::opposite($projectile->getHorizontalFacing()); + if($this->isValidFaceToRing($faceHit)){ + $this->ring($faceHit); + } + } + public function ring(int $faceHit) : void{ $world = $this->position->getWorld(); $world->addSound($this->position, new BellRingSound()); @@ -158,4 +160,15 @@ final class Bell extends Transparent{ $world->broadcastPacketToViewers($this->position, $tile->createFakeUpdatePacket($faceHit)); } } + + private function isValidFaceToRing(int $faceHit) : bool{ + return ( + $this->attachmentType->equals(BellAttachmentType::CEILING()) || + ($this->attachmentType->equals(BellAttachmentType::FLOOR()) && Facing::axis($faceHit) === Facing::axis($this->facing)) || + ( + ($this->attachmentType->equals(BellAttachmentType::ONE_WALL()) || $this->attachmentType->equals(BellAttachmentType::TWO_WALLS())) && + ($faceHit === Facing::rotateY($this->facing, false) || $faceHit === Facing::rotateY($this->facing, true)) + ) + ); + } }