From 406895a8bcceffea95f59ea2a14e5ef698a2c9d0 Mon Sep 17 00:00:00 2001 From: Stephen <138153996+stephendotgg@users.noreply.github.com> Date: Sat, 4 Nov 2023 04:44:07 +0000 Subject: [PATCH] BoundingBox.kt extensions --- build.gradle.kts | 2 +- .../flyte/twilight/extension/BoundingBox.kt | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/gg/flyte/twilight/extension/BoundingBox.kt diff --git a/build.gradle.kts b/build.gradle.kts index bdae0ad..86d8863 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "gg.flyte" -version = "1.0.18" +version = "1.0.19" repositories { mavenCentral() diff --git a/src/main/kotlin/gg/flyte/twilight/extension/BoundingBox.kt b/src/main/kotlin/gg/flyte/twilight/extension/BoundingBox.kt new file mode 100644 index 0000000..563f935 --- /dev/null +++ b/src/main/kotlin/gg/flyte/twilight/extension/BoundingBox.kt @@ -0,0 +1,74 @@ +package gg.flyte.twilight.extension + +import org.bukkit.Location +import org.bukkit.World +import org.bukkit.block.Block +import org.bukkit.util.BoundingBox + +/** + * Retrieves all the blocks within the bounding box in the specified world. + * + * This extension function for BoundingBox iterates through all x, y, and z + * coordinates that the bounding box encompasses and collects the corresponding + * blocks from the world. + * + * Note: This function can be performance-intensive for large bounding boxes, + * as it iterates over every single block within the bounding box. + * + * @param world The world from which to retrieve the blocks. + * @return A list of Block objects within the bounding box in the given world. + */ +fun BoundingBox.getBlocks(world: World): List { + val blocks = mutableListOf() + + val minX = minX.toInt() + val minY = minY.toInt() + val minZ = minZ.toInt() + val maxX = maxX.toInt() + val maxY = maxY.toInt() + val maxZ = maxZ.toInt() + + for (x in minX..maxX) { + for (y in minY..maxY) { + for (z in minZ..maxZ) { + blocks.add(world.getBlockAt(x, y, z)) + } + } + } + + return blocks +} + +/** + * Retrieves all the locations within the bounding box in the specified world. + * + * This extension function for BoundingBox iterates through all x, y, and z + * coordinates that the bounding box encompasses and collects the corresponding + * locations from the world. + * + * Note: This function can be performance-intensive for large bounding boxes, + * as it iterates over every single location within the bounding box. + * + * @param world The world in which the bounding box is defined. + * @return A list of Location objects representing each block position within the bounding box. + */ +fun BoundingBox.getLocations(world: World): List { + val locations = mutableListOf() + + val minX = minX.toInt() + val minY = minY.toInt() + val minZ = minZ.toInt() + val maxX = maxX.toInt() + val maxY = maxY.toInt() + val maxZ = maxZ.toInt() + + for (x in minX..maxX) { + for (y in minY..maxY) { + for (z in minZ..maxZ) { + locations.add(Location(world, x.toDouble(), y.toDouble(), z.toDouble())) + } + } + } + + return locations +} \ No newline at end of file