Skip to content

Instantly share code, notes, and snippets.

@ShiftSad
Created September 30, 2023 00:21
Show Gist options
  • Save ShiftSad/d96587bd1c156c4da42a59021595ef60 to your computer and use it in GitHub Desktop.
Save ShiftSad/d96587bd1c156c4da42a59021595ef60 to your computer and use it in GitHub Desktop.
Min3CraftDud3's Cuboid implementation in Kotlin.
@file:Suppress("unused")
package tech.shiftmc.townywars.api
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.block.Block
import kotlin.math.min
data class Cuboid(
val worldName: String,
val x1: Int, val y1: Int, val z1: Int,
val x2: Int, val y2: Int, val z2: Int
): Cloneable, Iterable<Block> {
val world = Bukkit.getWorld(worldName) ?: throw IllegalArgumentException("World $worldName is not loaded.")
constructor(location: Location) : this(
location.world.name,
location.blockX,
location.blockY,
location.blockZ,
location.blockX,
location.blockY,
location.blockZ
)
constructor(location: Location, location2: Location) : this(
location.world.name,
min(location.blockX, location2.blockX),
min(location.blockY, location2.blockY),
min(location.blockZ, location2.blockZ),
min(location.blockX, location2.blockX),
min(location.blockY, location2.blockY),
min(location.blockZ, location2.blockZ)
)
fun getVolume() =
(x2 - x1 + 1) * (y2 - y1 + 1) * (z2 - z1 + 1)
fun getBlocks(): List<Block> {
val blocks = mutableListOf<Block>()
for (x in x1..x2) for (y in y1..y2) for (z in z1..z2) blocks.add(
world.getBlockAt(x, y, z)
)
return blocks
}
override fun iterator(): Iterator<Block> =
getBlocks().iterator()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment