Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Get nearest player?

Comments in 'Plugin Development' started by Legoboy0215, Jun 30, 2015.

Thread Status:
Not open for further replies.
  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    In terms of coding, how do I find the nearest player from the command executor??
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Did you mean command sender/issuer?
    PHP:
    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 === -or $lastSquare $square){ // this is the so far closest player
            
    $closest $p;
            
    $lastSquare $square;
          }
        }
      }
      
    // do something with $closest
      // $closest may be null if there are no players in the world except $sender
    }
    Last edited by a moderator: Jun 30, 2015
    Falk, aliuly and TonyDroide like this.
  3. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    PHP:
    foreach($p->getLevel()->getNearbyEntities(new AxisAlignedBB($pos->getX()-10$pos->getY()-10$pos->getZ()-10$pos->getX()+10$pos->getY()+10$pos->getZ()+10), $p) as $entity)
    is this function bad? @PEMapModder?
    Falk likes this.
  4. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I want to teleport the sender to the closest player. I think I am only missing the teleportation. What value should I use?
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Read the code I gave you and try to understand it. You are welcome to ask any questions about the code itself, but before you do, don't try to add it into your code.
    Legoboy0215 and CrazedMiner like this.
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I never even tried. :)
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You are asking a question that you shouldn't be asking if you understand it. And asking this question shouldn't help you understanding it.
    Anyway it is $closest
Thread Status:
Not open for further replies.

Share This Page

Advertisement