Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Last active November 28, 2018 22:24
Show Gist options
  • Save Cryptite/df184ae684e1db5015a817866bc1a37e to your computer and use it in GitHub Desktop.
Save Cryptite/df184ae684e1db5015a817866bc1a37e to your computer and use it in GitHub Desktop.
Get Entities in a Cuboid Region Async with PaperLib
public static void getEntitiesWithinRegion(Location p1, Location p2, Consumer<List<Entity>> entitiesConsumer) {
int minX = Math.min(p1.getBlockX(), p2.getBlockX()) >> 4;
int maxX = Math.max(p1.getBlockX(), p2.getBlockX()) >> 4;
int minZ = Math.min(p1.getBlockZ(), p2.getBlockZ()) >> 4;
int maxZ = Math.max(p1.getBlockZ(), p2.getBlockZ()) >> 4;
List<CompletableFuture> futures = new ArrayList<>();
List<Entity> entities = new ArrayList<>();
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {
futures.add(PaperLib.getChunkAtAsync(p1.getWorld(), x, z)
.thenAccept(chunk -> entities.addAll(Arrays.asList(chunk.getEntities()))));
}
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[0]))
.thenAccept(aVoid -> entitiesConsumer.accept(entities));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment