diff --git a/src/event/player/PlayerViewDistanceChangeEvent.php b/src/event/player/PlayerViewDistanceChangeEvent.php new file mode 100644 index 000000000..0048672ab --- /dev/null +++ b/src/event/player/PlayerViewDistanceChangeEvent.php @@ -0,0 +1,55 @@ +player = $player; + $this->oldDistance = $oldDistance; + $this->newDistance = $newDistance; + } + + /** + * Returns the new view radius, measured in chunks. + */ + public function getNewDistance() : int{ + return $this->newDistance; + } + + /** + * Returns the old view radius, measured in chunks. + * A value of -1 means that the player has just connected and did not have a view distance before this event. + */ + public function getOldDistance() : int{ + return $this->oldDistance; + } +} diff --git a/src/player/Player.php b/src/player/Player.php index b2cc1f687..7cf4388d1 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -73,6 +73,7 @@ use pocketmine\event\player\PlayerToggleSneakEvent; use pocketmine\event\player\PlayerToggleSprintEvent; use pocketmine\event\player\PlayerToggleSwimEvent; use pocketmine\event\player\PlayerTransferEvent; +use pocketmine\event\player\PlayerViewDistanceChangeEvent; use pocketmine\form\Form; use pocketmine\form\FormValidationException; use pocketmine\inventory\CallbackInventoryListener; @@ -507,7 +508,14 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ } public function setViewDistance(int $distance) : void{ - $this->viewDistance = $this->server->getAllowedViewDistance($distance); + $newViewDistance = $this->server->getAllowedViewDistance($distance); + + if($newViewDistance !== $this->viewDistance){ + $ev = new PlayerViewDistanceChangeEvent($this, $this->viewDistance, $newViewDistance); + $ev->call(); + } + + $this->viewDistance = $newViewDistance; $this->spawnThreshold = (int) (min($this->viewDistance, $this->server->getConfigGroup()->getPropertyInt("chunk-sending.spawn-radius", 4)) ** 2 * M_PI);