Get nearby entities from location

Discussion in 'Plugin Development' started by 97WaterPolo, Sep 7, 2014.

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

    97WaterPolo

    Title says it all, currently using a method from @ Assist which is
    Show Spoiler

    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.  
    5. for (int chX = 0 - chunkRadius; chX <= chunkRadius; chX++) {
    6. for (int chZ = 0 - chunkRadius; chZ <= chunkRadius; chZ++) {
    7. int x = (int) l.getX(), y = (int) l.getY(), z = (int) l.getZ();
    8. for (Entity e: new Location(l.getWorld(), x + (chX * 16), y, z + (chZ * 16)).getChunk().getEntities()) {
    9. if (e.getLocation().distance(l) <= radius && e.getLocation().getBlock() != l.getBlock())
    10. radiusEntities.add(e);
    11. }
    12. }
    13. }
    14.  
    15. return radiusEntities.toArray(new Entity[radiusEntities.size()]);
    16. }


    Only issue is I am trying to get a radius of ~ 0.5 from the block. I was wondering if there was another way to do it or would I just have to get the entities within 1 block radius, and loc1.distance(loc2) < .5 to check if they are near the block.
     
  2. Offline

    97WaterPolo

    AdamQpzm
    Aren't chunks 16x16 areas? Multiplying that by a double doesn't sound right, and if I cast it to an int, it would either round to 0 or 1?
     
  3. Offline

    teej107

    casting to integers always rounds down.
     
  4. 97WaterPolo Keep chunk radius as an int, but change the radius in method parameters to double - it probably should have been double in the first place.
     
  5. Offline

    EcMiner

    Changing it to double should just work, you might need t cast it to an int in the chunkRadius part but the rest should work
     
  6. Offline

    Anrza

Thread Status:
Not open for further replies.

Share This Page