Get nearby entities

Discussion in 'Plugin Development' started by Mother__, Oct 14, 2013.

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

    Mother__

    I know that you can get nearby entities from an explosion, but is there a way to do the same thing, given a location? Thanks!

    Like this

    world.getNearbyEntities(x, y, z);
     
  2. Offline

    bobbob1870

    You could get a location and do .getChunk().getEntities() which will return an array of Entities
     
  3. Offline

    xTrollxDudex

    Mother__
    Spawn an invisible bat at the location and check the entities. Then remove the bat.
     
  4. ^^That's not the best solution.
    Here is a method i'm using that returns an arraylist of entites:
    Code:java
    1. public static Entity[] getNearbyEntities(Location l, int radius){
    2. int chunkRadius = radius < 16 ? 1 : (radius - (radius % 16))/16;
    3. HashSet<Entity> radiusEntities = new HashSet<Entity>();
    4. for (int chX = 0 -chunkRadius; chX <= chunkRadius; chX ++){
    5. for (int chZ = 0 -chunkRadius; chZ <= chunkRadius; chZ++){
    6. int x=(int) l.getX(),y=(int) l.getY(),z=(int) l.getZ();
    7. for (Entity e : new Location(l.getWorld(),x+(chX*16),y,z+(chZ*16)).getChunk().getEntities()){
    8. if (e.getLocation().distance(l) <= radius && e.getLocation().getBlock() != l.getBlock()) radiusEntities.add(e);
    9. }
    10. }
    11. }
    12. return radiusEntities.toArray(new Entity[radiusEntities.size()]);
    13. }
     
  5. Offline

    thecrystalflame

    you could just itorate through the servers entitys and check "location.distance(entlocation) < distance"
     
Thread Status:
Not open for further replies.

Share This Page