Skip to content

Instantly share code, notes, and snippets.

@jarcode-foss
Created June 26, 2014 04:31
Show Gist options
  • Save jarcode-foss/e1fac61e20b76276d848 to your computer and use it in GitHub Desktop.
Save jarcode-foss/e1fac61e20b76276d848 to your computer and use it in GitHub Desktop.
Get entities in radius
public static List<LivingEntity> getEntitiesInRadius(Location origin, double radius, LivingEntity... ignore) {
World w = origin.getWorld();
int blocks = (int) Math.ceil(radius);
int chunkRadius = (blocks / 16) + (blocks % 16 == 0 ? 0 : 1);
int ox = origin.getChunk().getX();
int oz = origin.getChunk().getZ();
ArrayList<Chunk> chunks = new ArrayList<>();
for (int x = -chunkRadius; x <= chunkRadius; x++)
for (int z = -chunkRadius; z <= chunkRadius; z++)
chunks.add(w.getChunkAt(ox + x, oz + z));
Location loc = origin.clone().add(0.5, 0.5, 0.5);
ArrayList<LivingEntity> entities = new ArrayList<>();
for (Chunk chunk : chunks) {
for (Entity e : chunk.getEntities()) {
if (e instanceof LivingEntity && !Arrays.asList(ignore).contains(e) && (e.getLocation().distanceSquared(loc) <= (radius * radius))) {
entities.add((LivingEntity) e);
}
}
}
return entities;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment