diff --git a/src/event/block/BlockTeleportEvent.php b/src/event/block/BlockTeleportEvent.php index 3dcd041ae..d20de90e1 100644 --- a/src/event/block/BlockTeleportEvent.php +++ b/src/event/block/BlockTeleportEvent.php @@ -27,6 +27,7 @@ use pocketmine\block\Block; use pocketmine\event\Cancellable; use pocketmine\event\CancellableTrait; use pocketmine\math\Vector3; +use pocketmine\utils\Utils; class BlockTeleportEvent extends BlockEvent implements Cancellable{ use CancellableTrait; @@ -44,6 +45,7 @@ class BlockTeleportEvent extends BlockEvent implements Cancellable{ } public function setTo(Vector3 $to) : void{ + Utils::checkVector3NotInfOrNaN($to); $this->to = $to; } } diff --git a/src/event/entity/EntityTeleportEvent.php b/src/event/entity/EntityTeleportEvent.php index 33f1d8de6..a513b4686 100644 --- a/src/event/entity/EntityTeleportEvent.php +++ b/src/event/entity/EntityTeleportEvent.php @@ -26,6 +26,7 @@ namespace pocketmine\event\entity; use pocketmine\entity\Entity; use pocketmine\event\Cancellable; use pocketmine\event\CancellableTrait; +use pocketmine\utils\Utils; use pocketmine\world\Position; /** @@ -54,6 +55,7 @@ class EntityTeleportEvent extends EntityEvent implements Cancellable{ } public function setTo(Position $to) : void{ + Utils::checkVector3NotInfOrNaN($to); $this->to = $to; } } diff --git a/src/event/player/PlayerMoveEvent.php b/src/event/player/PlayerMoveEvent.php index d9f258a3c..62b8a2843 100644 --- a/src/event/player/PlayerMoveEvent.php +++ b/src/event/player/PlayerMoveEvent.php @@ -27,6 +27,7 @@ use pocketmine\entity\Location; use pocketmine\event\Cancellable; use pocketmine\event\CancellableTrait; use pocketmine\player\Player; +use pocketmine\utils\Utils; class PlayerMoveEvent extends PlayerEvent implements Cancellable{ use CancellableTrait; @@ -51,6 +52,7 @@ class PlayerMoveEvent extends PlayerEvent implements Cancellable{ } public function setTo(Location $to) : void{ + Utils::checkLocationNotInfOrNaN($to); $this->to = $to; } } diff --git a/src/event/player/PlayerRespawnEvent.php b/src/event/player/PlayerRespawnEvent.php index 5f1f291ac..1bdc2446c 100644 --- a/src/event/player/PlayerRespawnEvent.php +++ b/src/event/player/PlayerRespawnEvent.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\event\player; use pocketmine\player\Player; +use pocketmine\utils\Utils; use pocketmine\world\Position; /** @@ -46,6 +47,7 @@ class PlayerRespawnEvent extends PlayerEvent{ if(!$position->isValid()){ throw new \InvalidArgumentException("Spawn position must reference a valid and loaded World"); } + Utils::checkVector3NotInfOrNaN($position); $this->position = $position; } } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 0b772ae64..adb3187f5 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -28,7 +28,9 @@ declare(strict_types=1); namespace pocketmine\utils; use DaveRandom\CallbackValidator\CallbackType; +use pocketmine\entity\Location; use pocketmine\errorhandler\ErrorTypeToStringMap; +use pocketmine\math\Vector3; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; use function array_combine; @@ -57,7 +59,9 @@ use function interface_exists; use function is_a; use function is_array; use function is_bool; +use function is_infinite; use function is_int; +use function is_nan; use function is_object; use function is_string; use function mb_check_encoding; @@ -602,4 +606,27 @@ final class Utils{ } return $value; } + + public static function checkFloatNotInfOrNaN(string $name, float $float) : void{ + if(is_nan($float)){ + throw new \InvalidArgumentException("$name cannot be NaN"); + } + if(is_infinite($float)){ + throw new \InvalidArgumentException("$name cannot be infinite"); + } + } + + public static function checkVector3NotInfOrNaN(Vector3 $vector3) : void{ + if($vector3 instanceof Location){ //location could be masquerading as vector3 + self::checkFloatNotInfOrNaN("yaw", $vector3->yaw); + self::checkFloatNotInfOrNaN("pitch", $vector3->pitch); + } + self::checkFloatNotInfOrNaN("x", $vector3->x); + self::checkFloatNotInfOrNaN("y", $vector3->y); + self::checkFloatNotInfOrNaN("z", $vector3->z); + } + + public static function checkLocationNotInfOrNaN(Location $location) : void{ + self::checkVector3NotInfOrNaN($location); + } }