Applied kotlin style conventions

This commit is contained in:
bluefireoly
2020-10-18 18:36:49 +02:00
parent e1d4e8bbfc
commit da848728d2
39 changed files with 509 additions and 385 deletions

View File

@@ -55,8 +55,16 @@ abstract class Circle(val radius: Number) {
while (currentRadius >= 0) {
this += circleEdgeLocations(currentRadius).mapTo(HashSet()) {
mutableSetOf(it).apply {
this += SimpleLocation2D(it.x + when { it.x >= 1 -> -1; it.x <= -1 -> 1; else -> 0 }, it.y)
this += SimpleLocation2D(it.x, it.y + when { it.y >= 1 -> -1; it.y <= -1 -> 1; else -> 0 })
this += SimpleLocation2D(
it.x + when {
it.x >= 1 -> -1; it.x <= -1 -> 1; else -> 0
}, it.y
)
this += SimpleLocation2D(
it.x, it.y + when {
it.y >= 1 -> -1; it.y <= -1 -> 1; else -> 0
}
)
}
}.flatten()
currentRadius--

View File

@@ -18,12 +18,12 @@ interface StructureData {
}
class SingleStructureData(
val location: SimpleLocation3D,
val structureData: StructureData
val location: SimpleLocation3D,
val structureData: StructureData
)
data class Structure(
val structureData: Set<SingleStructureData>
val structureData: Set<SingleStructureData>
) {
constructor(vararg structureDataSets: Set<SingleStructureData>)
: this(structureDataSets.flatMapTo(HashSet()) { it })
@@ -34,7 +34,7 @@ data class Structure(
*/
data class StructureDataMaterial(
val material: Material
val material: Material
) : StructureData {
override fun createAt(loc: Location) {
@@ -44,8 +44,8 @@ data class StructureDataMaterial(
}
data class StructureDataBlock(
val material: Material,
val blockData: BlockData
val material: Material,
val blockData: BlockData
) : StructureData {
constructor(block: Block) : this(block.type, block.blockData)
@@ -61,8 +61,8 @@ data class StructureDataBlock(
@NMS_General
data class StructureDataEntity(
val entityType: EntityType,
val nbtData: NBTData
val entityType: EntityType,
val nbtData: NBTData
) : StructureData {
constructor(entity: Entity) : this(entity.type, entity.nbtData)
@@ -75,7 +75,7 @@ data class StructureDataEntity(
}
data class StructureDataParticle(
val particle: KSpigotParticle
val particle: KSpigotParticle
) : StructureData {
override fun createAt(loc: Location) {

View File

@@ -15,26 +15,22 @@ fun Structure.buildAt(loc: Location) {
}
/** @see Structure.rotate */
fun Structure.rotateAroundX(angle: Number)
= rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
fun Structure.rotateAroundX(angle: Number) = rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
/** @see Structure.rotate */
fun Structure.rotateAroundY(angle: Number)
= rotate(angle) { it, rad -> it.rotateAroundY(rad) }
fun Structure.rotateAroundY(angle: Number) = rotate(angle) { it, rad -> it.rotateAroundY(rad) }
/** @see Structure.rotate */
fun Structure.rotateAroundZ(angle: Number)
= rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
fun Structure.rotateAroundZ(angle: Number) = rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
/** @param angle The angle of rotation in degrees.*/
inline fun Structure.rotate(angle: Number, vectorRotation: (Vector, Double) -> Vector)
= Structure(
HashSet<SingleStructureData>().apply {
structureData.forEach {
this += SingleStructureData(
vectorRotation.invoke(it.location.toVector(), Math.toRadians(angle.toDouble())).toSimpleLoc(),
it.structureData
)
}
inline fun Structure.rotate(angle: Number, vectorRotation: (Vector, Double) -> Vector) = Structure(
HashSet<SingleStructureData>().apply {
structureData.forEach {
this += SingleStructureData(
vectorRotation.invoke(it.location.toVector(), Math.toRadians(angle.toDouble())).toSimpleLoc(),
it.structureData
)
}
)
}
)

View File

@@ -12,38 +12,45 @@ 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 relationTo minLoc.blockLoc, StructureDataBlock(it)) }
else emptySet(),
if (includeEntities)
entities.mapTo(HashSet()) {
SingleStructureData(it.location relationTo minLoc.blockLoc, StructureDataEntity(it)) }
else emptySet()
)
fun LocationArea.loadStructure(includeBlocks: Boolean = true, includeEntities: Boolean = false) = Structure(
if (includeBlocks)
fillBlocks.mapTo(HashSet()) {
SingleStructureData(it.location relationTo minLoc.blockLoc, StructureDataBlock(it))
}
else emptySet(),
if (includeEntities)
entities.mapTo(HashSet()) {
SingleStructureData(it.location relationTo minLoc.blockLoc, StructureDataEntity(it))
}
else emptySet()
)
/**
* @return All blocks in the given [LocationArea].
* Sorted by their coordinates.
*/
val LocationArea.fillBlocks: Set<Block> get()
val LocationArea.fillBlocks: Set<Block>
get()
= LinkedHashSet<Block>().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
} } }
(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<Entity> get()
val LocationArea.entities: Set<Entity>
get()
= HashSet<Entity>().apply {
touchedChunks.forEach { it.entities.forEach { en ->
if (simpleLocationPair.isInArea(en.location))
this += en
} }
touchedChunks.forEach {
it.entities.forEach { en ->
if (simpleLocationPair.isInArea(en.location))
this += en
}
}
}