Bell: clean up code

This commit is contained in:
Dylan K. Taylor 2023-07-21 15:29:33 +01:00
parent 5899f2fc1d
commit 2779f92828
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -35,6 +35,7 @@ use pocketmine\math\Facing;
use pocketmine\math\RayTraceResult; use pocketmine\math\RayTraceResult;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\BlockTransaction; use pocketmine\world\BlockTransaction;
use pocketmine\world\sound\BellRingSound; use pocketmine\world\sound\BellRingSound;
@ -92,41 +93,39 @@ final class Bell extends Transparent{
} }
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($face === Facing::UP){ if(!$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){
if(!$this->canBeSupportedAt($blockReplace, Facing::DOWN)){
return false; return false;
} }
if($face === Facing::UP){
if($player !== null){ if($player !== null){
$this->setFacing(Facing::opposite($player->getHorizontalFacing())); $this->setFacing(Facing::opposite($player->getHorizontalFacing()));
} }
$this->setAttachmentType(BellAttachmentType::FLOOR()); $this->setAttachmentType(BellAttachmentType::FLOOR());
}elseif($face === Facing::DOWN){ }elseif($face === Facing::DOWN){
if(!$this->canBeSupportedAt($blockReplace, Facing::UP)){
return false;
}
$this->setAttachmentType(BellAttachmentType::CEILING()); $this->setAttachmentType(BellAttachmentType::CEILING());
}else{ }else{
$this->setFacing($face); $this->setFacing($face);
if($this->canBeSupportedAt($blockReplace, Facing::opposite($face))){ $this->setAttachmentType(
$this->setAttachmentType(BellAttachmentType::ONE_WALL()); $this->canBeSupportedAt($blockReplace, $face) ?
}else{ BellAttachmentType::TWO_WALLS() :
return false; BellAttachmentType::ONE_WALL()
} );
if($this->canBeSupportedAt($blockReplace, $face)){
$this->setAttachmentType(BellAttachmentType::TWO_WALLS());
}
} }
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
} }
public function onNearbyBlockChange() : void{ public function onNearbyBlockChange() : void{
if( foreach(match($this->attachmentType){
($this->attachmentType->equals(BellAttachmentType::CEILING()) && !$this->canBeSupportedAt($this, Facing::UP)) || BellAttachmentType::CEILING() => [Facing::UP],
($this->attachmentType->equals(BellAttachmentType::FLOOR()) && !$this->canBeSupportedAt($this, Facing::DOWN)) || BellAttachmentType::FLOOR() => [Facing::DOWN],
($this->attachmentType->equals(BellAttachmentType::ONE_WALL()) && !$this->canBeSupportedAt($this, Facing::opposite($this->facing))) || BellAttachmentType::ONE_WALL() => [Facing::opposite($this->facing)],
($this->attachmentType->equals(BellAttachmentType::TWO_WALLS()) && (!$this->canBeSupportedAt($this, $this->facing) || !$this->canBeSupportedAt($this, Facing::opposite($this->facing)))) BellAttachmentType::TWO_WALLS() => [$this->facing, Facing::opposite($this->facing)],
){ default => throw new AssumptionFailedError("All cases of BellAttachmentType must be handled")
} as $supportBlockDirection){
if(!$this->canBeSupportedAt($this, $supportBlockDirection)){
$this->position->getWorld()->useBreakOn($this->position); $this->position->getWorld()->useBreakOn($this->position);
break;
}
} }
} }
@ -159,13 +158,11 @@ final class Bell extends Transparent{
} }
private function isValidFaceToRing(int $faceHit) : bool{ private function isValidFaceToRing(int $faceHit) : bool{
return ( return match($this->attachmentType){
$this->attachmentType->equals(BellAttachmentType::CEILING()) || BellAttachmentType::CEILING() => true,
($this->attachmentType->equals(BellAttachmentType::FLOOR()) && Facing::axis($faceHit) === Facing::axis($this->facing)) || BellAttachmentType::FLOOR() => Facing::axis($faceHit) === Facing::axis($this->facing),
( BellAttachmentType::ONE_WALL(), BellAttachmentType::TWO_WALLS() => $faceHit === Facing::rotateY($this->facing, false) || $faceHit === Facing::rotateY($this->facing, true),
($this->attachmentType->equals(BellAttachmentType::ONE_WALL()) || $this->attachmentType->equals(BellAttachmentType::TWO_WALLS())) && default => throw new AssumptionFailedError("All cases of BellAttachmentType must be handled")
($faceHit === Facing::rotateY($this->facing, false) || $faceHit === Facing::rotateY($this->facing, true)) };
)
);
} }
} }