Skip to content

Commit

Permalink
BoundingBox.kt extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen committed Nov 4, 2023
1 parent 9639249 commit 406895a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "gg.flyte"
version = "1.0.18"
version = "1.0.19"

repositories {
mavenCentral()
Expand Down
74 changes: 74 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/extension/BoundingBox.kt
Original file line number Diff line number Diff line change
@@ -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<Block> {
val blocks = mutableListOf<Block>()

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<Location> {
val locations = mutableListOf<Location>()

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
}

0 comments on commit 406895a

Please sign in to comment.