Getting Nearest Entity?

Hi,

How would I find the nearest entity from a player location?

Thanks
LilypadCollector

From this, you can do the following:

public Collection<Entity> getEntities(final Player player, final int radius) {
  return player.getLocation().getExtent().getEntities(new Predicate<Entity>() {
    @Override
    public boolean apply(Entity entity) {
      return entity.getLocation().getPosition().distance(player.getLocation().getPosition()) <= radius;
    }
  }
}

Or, to get the single-most closest entity:

public Entity getClosestEntity(final Player player) {
    Entity closest = null;
    double closestDistance = 0;

    for (Entity entity : player.getLocation().getExtent().getEntities()) {
        if (entity == player) {
            continue;
        }

        double distance = entity.getLocation().getPosition().distance(player.getLocation().getPosition());
        if (closest == null || distance < closestDistance) {
            closest = entity;
            closestDistance = distance;
        }
    }

    return closest;
}

A marked downfall, if you can call it that, is that in the off-chance that there are two entities which are exactly the same distance away (which, to be fair, is unlikely when you’re comparing doubles), it’ll only give you the first one to be looped over.

1 Like

This could happen if 2 players are at the /spawn location

Should this be something included in the API in case implementations have a more efficient lookup of local entities?

If a convenience method were to be added to get the nearest entity, I’d rather it not be limited by getting the single-most closest entity.

For instance, maybe a method to get the closest x entities?

public Entity getClosestEntity(final Player player) {
    return this.getClosestEntities(player, 1).get(0);
}

public List<Entity> getClosestEntities(final Player player, final int toGet) {
    return player.getLocation().getExtent().getClosestEntities(1);
}

The above could obviously be written out in a more efficient way, but I wanted to showcase both use-cases: getting the closest entity, and getting X closest entities.

Wouldn’t this always return the Player, since he is an Entity? :smiley:

Edit: Same goes to your first reply

1 Like

You’re probably right. I’ve edited my first post to reflect that check. The second I’ll leave alone, however, because the method doesn’t actually exist and it’s function, should it be added as a convenience later on, is entirely conjecture and I can’t be arsed. :stuck_out_tongue:

1 Like

Thank you so much, @FerusGrim and @gabizou!

Just remember that now we’ve moved to Java 8, use the java.util.function.Predicate and not the guava Predicates!

1 Like

You can fix that by making the method account for multiple entries with a collection, and allowing for a max entries criteria (ie: Map<Entity, Double> getClosestEntitiesFor(<entity-origin>, <range>, <int-of-max-entities>); where K=the entity, and V=the distance)

Note: The proposed pseudo method does not return the types sorted closest->farthest or vice versa.

EDIT
Well, I guess I skimmed over your follow up comment. Ignore me.