Most efficient way to do the equivalent of Player.getNearbyEntities() for a Location

Discussion in 'Plugin Development' started by Ronbo, Feb 22, 2014.

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

    Ronbo

    World.getEntities() can have thousands of entities, so looping through them and comparing distances is probably the most inefficient thing ever. Is there a faster way? The only thing I can think of to slightly improve the performance is to only check entities within the same chunk as the location, but that could still be pretty slow, right?

    Edit:
    Perhaps spawning an entity at the location and using getNearbyEntities() with it and then removing it would work
     
  2. Offline

    xTrollxDudex

    Ronbo
    And you know it loops through every single entity in the world from...?

    That assumption is incorrect. Internally, getNearbyEntities grows the AxisAlignedBoundingBox by the amount specified, looping through the chunks contained by the bounding box. The entities inside the bounding box and the chunk are added to the returned list of entities.
     
  3. Offline

    Ronbo

    xTrollxDudex
    Oops, that was poorly worded. I meant to say that using all the entities in Bukkit.getServer().getWorld("world").getEntities() and checking through those with distance() or something would be bad, not the nms World class's getEntities() method. I ended up just spawning an entity and using getNearbyEntities so that I could use the method with a bounding box.
     
Thread Status:
Not open for further replies.

Share This Page