24package com.dreamfirestudios.dreamcore.DreamBlock;
26import org.bukkit.Location;
27import org.bukkit.Material;
28import org.bukkit.World;
29import org.bukkit.block.Block;
32import java.util.concurrent.CompletableFuture;
33import java.util.function.Predicate;
75 final RegionShape shape,
final Predicate<Block> condition) {
76 List<Block> result =
new ArrayList<>();
77 final World world = location.getWorld();
78 if (world ==
null || step <= 0)
return result;
80 final int cx = location.getBlockX();
81 final int cy = location.getBlockY();
82 final int cz = location.getBlockZ();
84 final int r2 = radius * radius;
85 for (
int x = cx - radius; x < cx + radius; x += step) {
86 for (
int y = cy - radius; y < cy + radius; y += step) {
87 for (
int z = cz - radius; z < cz + radius; z += step) {
89 int dx = x - cx, dy = y - cy, dz = z - cz;
90 if (dx * dx + dy * dy + dz * dz > r2)
continue;
92 Block block = world.getBlockAt(x, y, z);
93 if (condition.test(block)) result.add(block);
111 final RegionShape shape,
final Material... materials) {
112 List<Block> result =
new ArrayList<>();
113 final World world = location.getWorld();
114 if (world ==
null || step <= 0)
return result;
116 final int cx = location.getBlockX();
117 final int cy = location.getBlockY();
118 final int cz = location.getBlockZ();
120 final int r2 = radius * radius;
121 final boolean filterByMaterial = materials !=
null && materials.length > 0;
122 final Set<Material> materialSet = filterByMaterial ?
new HashSet<>(Arrays.asList(materials)) :
null;
124 for (
int x = cx - radius; x < cx + radius; x += step) {
125 for (
int y = cy - radius; y < cy + radius; y += step) {
126 for (
int z = cz - radius; z < cz + radius; z += step) {
128 int dx = x - cx, dy = y - cy, dz = z - cz;
129 if (dx * dx + dy * dy + dz * dz > r2)
continue;
131 Block block = world.getBlockAt(x, y, z);
132 if (!filterByMaterial || materialSet.contains(block.getType())) result.add(block);
155 final RegionShape shape,
final Material[] targetMaterials,
156 final Material replacementMaterial) {
157 final World world = location.getWorld();
158 if (world ==
null || step <= 0)
return 0;
161 final int cx = location.getBlockX();
162 final int cy = location.getBlockY();
163 final int cz = location.getBlockZ();
165 final int r2 = radius * radius;
166 final Set<Material> targets =
new HashSet<>(Arrays.asList(targetMaterials));
168 for (
int x = cx - radius; x < cx + radius; x += step) {
169 for (
int y = cy - radius; y < cy + radius; y += step) {
170 for (
int z = cz - radius; z < cz + radius; z += step) {
172 int dx = x - cx, dy = y - cy, dz = z - cz;
173 if (dx * dx + dy * dy + dz * dz > r2)
continue;
175 Block block = world.getBlockAt(x, y, z);
176 if (targets.contains(block.getType())) {
177 block.setType(replacementMaterial,
false);
197 final RegionShape shape,
final Material... materials) {
198 final World world = location.getWorld();
199 if (world ==
null || step <= 0)
return 0;
202 final int cx = location.getBlockX();
203 final int cy = location.getBlockY();
204 final int cz = location.getBlockZ();
206 final int r2 = radius * radius;
207 final boolean filterByMaterial = materials !=
null && materials.length > 0;
208 final Set<Material> materialSet = filterByMaterial ?
new HashSet<>(Arrays.asList(materials)) :
null;
210 for (
int x = cx - radius; x < cx + radius; x += step) {
211 for (
int y = cy - radius; y < cy + radius; y += step) {
212 for (
int z = cz - radius; z < cz + radius; z += step) {
214 int dx = x - cx, dy = y - cy, dz = z - cz;
215 if (dx * dx + dy * dy + dz * dz > r2)
continue;
217 Block block = world.getBlockAt(x, y, z);
218 if (!filterByMaterial || materialSet.contains(block.getType())) count++;
234 final RegionShape shape,
final Material material) {
235 final World world = location.getWorld();
236 if (world ==
null)
return null;
238 Block closest =
null;
239 double bestDist = Double.MAX_VALUE;
241 final int cx = location.getBlockX();
242 final int cy = location.getBlockY();
243 final int cz = location.getBlockZ();
244 final int r2 = radius * radius;
246 for (
int x = cx - radius; x < cx + radius; x++) {
247 for (
int y = cy - radius; y < cy + radius; y++) {
248 for (
int z = cz - radius; z < cz + radius; z++) {
250 int dx = x - cx, dy = y - cy, dz = z - cz;
251 if (dx * dx + dy * dy + dz * dz > r2)
continue;
253 Block block = world.getBlockAt(x, y, z);
254 if (block.getType() == material) {
255 double dist = location.distanceSquared(block.getLocation());
256 if (dist < bestDist) {
282 final RegionShape shape,
final Material... materials) {
283 final World world = location.getWorld();
284 if (world ==
null || step <= 0)
return 0;
287 final int cx = location.getBlockX();
288 final int cy = location.getBlockY();
289 final int cz = location.getBlockZ();
291 final int r2 = radius * radius;
292 final boolean filterByMaterial = materials !=
null && materials.length > 0;
293 final Set<Material> materialSet = filterByMaterial ?
new HashSet<>(Arrays.asList(materials)) :
null;
295 for (
int x = cx - radius; x < cx + radius; x += step) {
296 for (
int y = cy - radius; y < cy + radius; y += step) {
297 for (
int z = cz - radius; z < cz + radius; z += step) {
299 int dx = x - cx, dy = y - cy, dz = z - cz;
300 if (dx * dx + dy * dy + dz * dz > r2)
continue;
302 Block block = world.getBlockAt(x, y, z);
303 if (!filterByMaterial || materialSet.contains(block.getType())) {
304 block.setType(Material.AIR,
false);
328 final Predicate<Block> condition) {
329 return CompletableFuture.supplyAsync(() ->
filterBlocksInRadius(location, radius, step, shape, condition));
343 final Material... materials) {
358 final Material... materials) {
359 return CompletableFuture.supplyAsync(() ->
countBlocksInRadius(location, radius, step, shape, materials));
371 final RegionShape shape,
final Material material) {
372 return CompletableFuture.supplyAsync(() ->
findClosestBlock(location, radius, shape, material));
Utilities for synchronous and asynchronous (read-only) block queries within a region (cube or sphere)...
static int replaceBlocksInRadius(final Location location, final int radius, final int step, final RegionShape shape, final Material[] targetMaterials, final Material replacementMaterial)
Replaces all blocks whose material is in targetMaterials within the region with replacementMaterial ...
static List< Block > returnAllBlocksInRadius(final Location location, final int radius, final int step, final RegionShape shape, final Material... materials)
Returns all blocks within a region centered at location , optionally filtered by materials .
static CompletableFuture< Block > findClosestBlockAsync(final Location location, final int radius, final RegionShape shape, final Material material)
Asynchronously finds the closest block of the specified material within a region.
static CompletableFuture< Integer > countBlocksInRadiusAsync(final Location location, final int radius, final int step, final RegionShape shape, final Material... materials)
Asynchronously counts blocks with the specified materials within a region.
static CompletableFuture< List< Block > > returnAllBlocksInRadiusAsync(final Location location, final int radius, final int step, final RegionShape shape, final Material... materials)
Asynchronously returns all blocks within a region.
static List< Block > filterBlocksInRadius(final Location location, final int radius, final int step, final RegionShape shape, final Predicate< Block > condition)
Filters blocks within a region centered at location that match condition .
static int countBlocksInRadius(final Location location, final int radius, final int step, final RegionShape shape, final Material... materials)
Counts blocks within the region.
static Block findClosestBlock(final Location location, final int radius, final RegionShape shape, final Material material)
Finds the closest block of material within the region, or null if none.
static int clearBlocksInRadius(final Location location, final int radius, final int step, final RegionShape shape, final Material... materials)
Clears (sets to Material::AIR) all blocks within the region.
static CompletableFuture< List< Block > > filterBlocksInRadiusAsync(final Location location, final int radius, final int step, final RegionShape shape, final Predicate< Block > condition)
Asynchronously filters blocks within a region.
Enumeration of geometric region shapes used in block operations.
SPHERE
A spherical region defined by all positions within a given radius from the center point,...