Search for nearst(180 blocks) player

Discussion in 'Plugin Development' started by Licio123, Mar 25, 2014.

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


    Make compass search for the nearst player in a distace of 180 blocks
    I need help with the code
  2. Offline


    1. Loop through the players (or entities)
    2. Find which one is closest, and if it is less than 180 blocks away.
    3. Set the compass target to the player.
  3. Offline


    Can u send me the code?
  4. Offline


    But then you wouldn't learn anything, would you.
    AoH_Ruthless likes this.
  5. Offline


    Player getNearestPlayer(Player p, double radius) {
    Player closest = null;
    double distance = radius;
    for (Player pl : getServer().getOnlinePlayers()) {
    if (pl!=p&&pl.getLocation().distance(p.getLocation()) <= radius) {
    if (pl.getLocation().distance(p.getLocation()) < distance) {
    closest = pl;
    distance = pl.getLocation().distance(p.getLocation());
    return closest;

    Written in the reply box, not IDE
  6. Offline


    What Barinade said would only work in a class that extends JavaPlugin, so if you are putting this in a different class, use "Bukkit.getOnlinePlayers()" instead of "getServer()"
  7. Offline


    I am reading the code to learn
Thread Status:
Not open for further replies.

Share This Page