Finding Closest Player

Discussion in 'Plugin Development' started by Dr_MadMan, Apr 9, 2012.

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


    I am working on a plugin in which one of the aspects is that the compass will point at the closest player. I know how to get compasses to point at players, but what is stumping me is how to find the closest player. Essentially, I a need a way to find the closest point to Loc A from a list (Loc D, Loc, B, and Loc, C). Can anyone help me with this puzzle?
  2. Offline


    List<Location> locations = (This is your list of locations!)
    Location myLocation = (Player location)
    Location closest = locations.get(0);
    double closestDist = closest.distance(myLocation);
    for (Location loc : locations) {
        if (loc.distance(myLocation) < closestDist) {
            closestDist = loc.distance(myLocation);
            closest = loc;
    //Now you got the closest location in closest. 
    This probably isn't the most efficient way, though.
  3. Offline


    u can do something like..
    2. Player p = //whatever;
    3. boolean found = false;
    4. for (int i = 0; i < 200; i++) {
    5. List<Entity> entities = p.getNearbyEntities(i,64,i);
    6. for (entity e : entities) {
    7. if (e.getType().equals(EntityType.PLAYER)) {
    8. //do compas thing for that player, also post that code, i would be interested in it..
    9. found = true;
    10. break;
    11. }
    12. }
    13. if (found) break;
    14. }

    btw, still would be nice if u cud post compas code :).
  4. Offline


    You should use .distanceSquared(); .distance() uses the sqrt() method which is pretty intensive.
    zhuowei likes this.
  5. Offline


    It's just
    1. player.setCompassTarget(location);
Thread Status:
Not open for further replies.

Share This Page