The Nearest of Nearby Players?

Discussion in 'Plugin Development' started by chasechocolate, Dec 23, 2012.

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

    chasechocolate

    In my plugin, I want to let players interact with their compass to get the nearest player, and their compass will point to them. I can do player.getNearbyEntities(30, 30, 30) and check if the entity is a player, but if there are multiple players within a 30 block radius of their location, it will send the player multiple messages, and point their compass to multiple locations. How can I get the nearest of nearby players?
     
  2. Offline

    NoLiver92

    start with size 0 then work up in the radius till you find a player
     
  3. Offline

    fireblast709

    Or loop through all and keep checking the distance. Keep the nearest everytime.
     
  4. Offline

    nitrousspark

    how would you do that?

    would this work for that?

    Code:
                for (Player player1 : Distance.playersDistance(event.getPlayer()
                        .getLocation(), 32)) {
                    if (Distance.exactDistanceFromLocation(player.getWorld(),
                            player.getLocation(), player1.getLocation()) < Distance
                            .exactDistanceFromLocation(player.getWorld(),
                                    player.getLocation(), player1.getLocation())) {
                        player.setCompassTarget(player1.getLocation());
                    }
                }
    Distance class here

    Code:
       
        public static double exactDistanceFromLocation(World world, Location loc, Location loc1){
           
            ChunkCoordinates cords = new ChunkCoordinates();
           
            cords.b((int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
           
            double distance = (double) Math.max(Math.abs(loc1.getX() - cords.x), Math.abs(loc1.getZ() - cords.z));
           
            return distance;
           
        }
    Code:
       
        public static ArrayList<Player> playersDistance(Location loc, double distance){
           
            ArrayList<Player> players = new ArrayList<Player>();
           
            double i1 = loc.getX() + distance;
            double j1 = loc.getY() + distance;
            double k1 = loc.getZ() + distance;
           
            double i2 = loc.getX() - distance;
            double j2 = loc.getY() - distance;
            double k2 = loc.getZ() - distance;
           
            double i3 = 0;
            double j3 = 0;
            double k3 = 0;
           
            for(Player player : Bukkit.getOnlinePlayers()){
               
                if(player.getWorld().equals(loc.getWorld())){       
               
                    i3 = player.getLocation().getX();
                    j3 = player.getLocation().getY();
                    k3 = player.getLocation().getZ();
                   
                    if(i3 <= i1 && i3 >= i2 && j3 <= j1 && j3 >= j2 && k3 <= k1 && k3 >= k2){
                       
                        players.add(player);
                       
                    }
                   
                }
               
            }
           
            return players;
           
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  5. Offline

    fireblast709

    entity.getLocation().distanceSquared(player.getLocation());
    Use distanceSquared because squaring another number before comparing takes up less resources than taking a squareroot (like getDistance() would do)
     
  6. Offline

    nitrousspark

    would it work without doing that?
    because i just tried it and it didnt work
     
  7. Offline

    fireblast709

    Well its a method for comparing :p. You would just loop over the entities, and keep overriding the LivingEntity if its closer
     
  8. Offline

    nitrousspark

    thats what i thought i was doing ._.

    Code:
    for (Player player1 : Distance.playersDistance(event.getPlayer()
                        .getLocation(), 32)) {
                    if (Distance.exactDistanceFromLocation(player.getWorld(),
                            player.getLocation(), player1.getLocation()) < Distance
                            .exactDistanceFromLocation(player.getWorld(),
                                    player.getLocation(), player1.getLocation())) {
                        player.setCompassTarget(player1.getLocation());
                    }
                }
     
  9. Offline

    fireblast709

    Code:java
    1. public Player getClosestPlayer(Player player, double radius)
    2. {
    3. double minimalDistance = Math.pow(radius, 2);
    4. double curDist;
    5. Player closest = null;
    6. for(Player p = Bukkit.getOnlinePlayers())
    7. {
    8. if(!p.equals(player))
    9. {
    10. curDist = player.getLocation().distanceSquared(p.getLocation());
    11. if( curDist < minimalDistance)
    12. {
    13. minimalDistance = curDist;
    14. closest = p;
    15. }
    16. }
    17. }
    18. return closest;
    19. }

    Seems legit to me
     
    RastaLulz likes this.
Thread Status:
Not open for further replies.

Share This Page