Created
June 26, 2014 04:31
-
-
Save jarcode-foss/e1fac61e20b76276d848 to your computer and use it in GitHub Desktop.
Get entities in radius
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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