From dfee8b7fa589e2860a9743a6a2252df78b1d07af Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 4 Oct 2020 21:09:45 +0100 Subject: [PATCH] Added SignLikeRotationTrait --- src/block/FloorBanner.php | 6 +-- src/block/FloorSign.php | 7 ++-- src/block/utils/SignLikeRotationTrait.php | 46 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 src/block/utils/SignLikeRotationTrait.php diff --git a/src/block/FloorBanner.php b/src/block/FloorBanner.php index 06323009c..466bce5cb 100644 --- a/src/block/FloorBanner.php +++ b/src/block/FloorBanner.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\SignLikeRotationTrait; use pocketmine\item\Item; use pocketmine\math\Facing; use pocketmine\math\Vector3; @@ -30,8 +31,7 @@ use pocketmine\player\Player; use pocketmine\world\BlockTransaction; final class FloorBanner extends BaseBanner{ - /** @var int */ - protected $rotation = 0; + use SignLikeRotationTrait; public function readStateFromData(int $id, int $stateMeta) : void{ $this->rotation = $stateMeta; @@ -55,7 +55,7 @@ final class FloorBanner extends BaseBanner{ } if($player !== null){ - $this->rotation = ((int) floor((($player->getLocation()->getYaw() + 180) * 16 / 360) + 0.5)) & 0x0f; + $this->rotation = self::getRotationFromYaw($player->getLocation()->getYaw()); } return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } diff --git a/src/block/FloorSign.php b/src/block/FloorSign.php index 4544afeed..7ee4880dd 100644 --- a/src/block/FloorSign.php +++ b/src/block/FloorSign.php @@ -23,16 +23,15 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\SignLikeRotationTrait; use pocketmine\item\Item; use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; use pocketmine\world\BlockTransaction; -use function floor; final class FloorSign extends BaseSign{ - /** @var int */ - protected $rotation = 0; + use SignLikeRotationTrait; public function readStateFromData(int $id, int $stateMeta) : void{ $this->rotation = $stateMeta; @@ -56,7 +55,7 @@ final class FloorSign extends BaseSign{ } if($player !== null){ - $this->rotation = ((int) floor((($player->getLocation()->getYaw() + 180) * 16 / 360) + 0.5)) & 0x0f; + $this->rotation = self::getRotationFromYaw($player->getLocation()->getYaw()); } return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } diff --git a/src/block/utils/SignLikeRotationTrait.php b/src/block/utils/SignLikeRotationTrait.php new file mode 100644 index 000000000..fd09a824f --- /dev/null +++ b/src/block/utils/SignLikeRotationTrait.php @@ -0,0 +1,46 @@ +rotation; } + + /** @return $this */ + public function setRotation(int $rotation) : self{ + if($rotation < 0 || $rotation > 15){ + throw new \InvalidArgumentException("Rotation must be in range 0-15"); + } + $this->rotation = $rotation; + return $this; + } + + private static function getRotationFromYaw(float $yaw) : int{ + return ((int) floor((($yaw + 180) * 16 / 360) + 0.5)) & 0xf; + } +}