Player Locator

Discussion in 'Development' started by A354-PH, Nov 10, 2018 at 6:50 AM.

  1. A354-PH

    A354-PH Witch

    Messages:
    64
    GitHub:
    a354-ph
    Hey guys, how to make a compass that locates the nearby player?
     
  2. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,410
    GitHub:
    muqsit
    You can get the nearest player by iterating over $player->getViewers() and calculating their distances from $player.
    And this will make the compass for a player point towards $position.
    PHP:
    $player->setSpawn($position);
     
  3. korado531m7

    korado531m7 Witch Verified

    Messages:
    71
    GitHub:
    korado531m7
    PHP:
    //$player is player object
    if($player instanceof Position){
        
    $closest false;
        
    $lastSquare = -1;
        foreach(
    $player->level->getPlayers() as $p){
          
    $square $player->distanceSquared($p);
            if(
    $lastSquare === -or $lastSquare $square){
               
    $closest $p;
               
    $lastSquare $square;
            }
       }
       if(
    $closest == false){
          
    //Not found
       
    }else{
          
    //$closest is the closest player object
       
    }
    }
    Addition, if you want to set needle to the closest player, use setspawnpositionpacket
    PHP:
    $pk = new SetSpawnPositionPacket();
    $pk->spawnType SetSpawnPositionPacket::TYPE_WORLD_SPAWN;
    $pk->= (int) $closest->getX();
    $pk->= (int) $closest->getY();
    $pk->= (int) $closest->getZ();
    $pk->spawnForced false;
    $player->dataPacket($pk);
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.