Permalink
Browse files

Make getNearbyEntities not load chunks.

  • Loading branch information...
me4502 committed Feb 9, 2017
1 parent 7961798 commit 7eb9405bec8326cb0d14266f3b256c19fcb5003c
Showing with 9 additions and 7 deletions.
  1. +9 −7 src/main/java/com/sk89q/craftbook/util/LocationUtil.java
@@ -48,18 +48,20 @@ public static boolean isWithinRadius(Location l1, Location l2, Vector radius) {
}
public static Entity[] getNearbyEntities(Location l, Vector radius) {
-
int chunkRadiusX = radius.getBlockX() < 16 ? 1 : radius.getBlockX() / 16;
int chunkRadiusZ = radius.getBlockZ() < 16 ? 1 : radius.getBlockZ() / 16;
HashSet<Entity> radiusEntities = new HashSet<Entity>();
for (int chX = 0 - chunkRadiusX; chX <= chunkRadiusX; chX++) {
for (int chZ = 0 - chunkRadiusZ; chZ <= chunkRadiusZ; chZ++) {
- int x = (int) l.getX(), y = (int) l.getY(), z = (int) l.getZ();
- for (Entity e : new Location(l.getWorld(), x + chX * 16, y, z + chZ * 16).getChunk().getEntities()) {
- if(e == null || e.isDead() || !e.isValid())
- continue;
- if(isWithinRadius(l,e.getLocation(),radius))
- radiusEntities.add(e);
+ int offChunkX = l.getChunk().getX() + chX;
+ int offChunkZ = l.getChunk().getZ() + chZ;
+ if (l.getWorld().isChunkLoaded(offChunkX, offChunkZ)) {
+ for (Entity e : l.getWorld().getChunkAt(offChunkX, offChunkZ).getEntities()) {
+ if (e == null || e.isDead() || !e.isValid())
+ continue;
+ if (isWithinRadius(l, e.getLocation(), radius))
+ radiusEntities.add(e);
+ }
}
}
}

0 comments on commit 7eb9405

Please sign in to comment.