Ejemplo n.º 1
0
 public function onCommand(CommandSender $sender, Command $command, $label, array $args)
 {
     if (strtolower($command->{$getName}()) == "closest") {
         if ($sender instanceof Position) {
             // if the sender is a position, for example a player
             $closest = null;
             $lastSquare = -1;
             foreach ($sender->getLevel()->getPlayers() as $p) {
                 // for every player in the sender's world
                 if ($p !== $sender) {
                     $square = $sender->distanceSquared($p);
                     // get the square of the distance between the sender and the currently checking player
                     // use distanceSquared() because it is faster than distance()
                     if ($lastSquare === -1 or $lastSquare > $square) {
                         // this is the so far closest player
                         $closest = $p;
                         $lastSquare = $square;
                     }
                 }
             }
             if ($sender instanceof Player) {
                 $sender->sendMessage("The closest player to you is" . $closest);
             }
         }
     }
 }