Skip to content

Instantly share code, notes, and snippets.

@fourohfour
Created January 3, 2014 18:38
Show Gist options
  • Save fourohfour/8243657 to your computer and use it in GitHub Desktop.
Save fourohfour/8243657 to your computer and use it in GitHub Desktop.
Get nearest player Bukkit.
double closest = Double.MAX_VALUE;
Player closestp = null;
for(Player i : Bukkit.getOnlinePlayers()){
double dist = i.getLocation().distance(event.getPlayer().getLocation());
if (closest == Double.MAX_VALUE || dist < closest){
closest = dist;
closestp = i;
}
}
if (closestp == null){
//No players found
}
else{
//the closest player is closestp
}
@werterhalimi
Copy link

hey thank you for the code but the closest player is the player interact how i can do ? sorry for my english : P
@fourohfour

@zitreF
Copy link

zitreF commented Feb 6, 2021

    loc.getWorld().getNearbyEntities(loc, distance, distance, distance).stream()
            .filter(e -> e instanceof Player)
            .findFirst()
            .orElse(null);
           just do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment