diff --git a/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt b/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt new file mode 100644 index 00000000..9d972d15 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt @@ -0,0 +1,45 @@ +package net.axay.kspigot.structures + +import net.axay.kspigot.extensions.geometry.LocationArea +import org.bukkit.Location +import org.bukkit.block.Block +import org.bukkit.entity.Entity + +/** + * @return A [Structure] containing all data of the given [LocationArea]. + */ +fun LocationArea.loadStructure(includeBlocks: Boolean = true, includeEntities: Boolean = false) + = Structure( + if (includeBlocks) + fillBlocks.mapTo(HashSet()) { SingleStructureData(it.location, StructureDataBlock(it)) } + else + emptySet(), + if (includeEntities) + entities.mapTo(HashSet()) { SingleStructureData(it.location, StructureDataEntity(it)) } + else + emptySet() + ) + +/** + * @return All blocks in the given [LocationArea]. + * Sorted by their coordinates. + */ +val LocationArea.fillBlocks: Set get() + = LinkedHashSet().apply { + (minLoc.blockX until maxLoc.blockX + 1).forEach { x -> + (minLoc.blockY until maxLoc.blockY + 1).forEach { y -> + (minLoc.blockZ until maxLoc.blockZ + 1).forEach { z -> + this += Location(world, x.toDouble(), y.toDouble(), z.toDouble()).block + } } } + } + +/** + * @return All entities in the given [LocationArea]. + */ +val LocationArea.entities: Set get() + = HashSet().apply { + touchedChunks.forEach { it.entities.forEach { en -> + if (locationPair.isInArea(en.location)) + this += en + } } + } \ No newline at end of file