Updated SimpleLocations

This commit is contained in:
bluefireoly
2020-10-16 23:45:46 +02:00
parent 2c1d08e58e
commit d9720d90cf
2 changed files with 37 additions and 12 deletions

View File

@@ -0,0 +1,11 @@
package net.axay.kspigot.extensions.bukkit
import org.bukkit.Location
import org.bukkit.World
/**
* Assumes that this Location has world data.
* If not, an exception will be thrown.
*/
val Location.worldOrException: World get() = world
?: throw NullPointerException("The world of the location is null!")

View File

@@ -7,28 +7,42 @@ import org.bukkit.Location
import org.bukkit.World
import org.bukkit.util.Vector
data class SimpleLocation2D(val x: Double, val y: Double) {
constructor(x: Number, y: Number) : this(x.toDouble(), y.toDouble())
data class SimpleLocation2D(
val x: Double,
val y: Double
) {
constructor(x: Number, y: Number)
: this(x.toDouble(), y.toDouble())
}
data class SimpleLocation3D(val x: Double, val y: Double, val z: Double) {
constructor(x: Number, y: Number, z: Number) : this(x.toDouble(), y.toDouble(), z.toDouble())
val chunk: SimpleChunkLocation get() = SimpleChunkLocation(x.toInt() shr 4, z.toInt() shr 4)
data class SimpleLocation3D(
val x: Double,
val y: Double,
val z: Double,
val direction: Vector = vec(0, 0, 0)
) {
constructor(x: Number, y: Number, z: Number)
: this(x.toDouble(), y.toDouble(), z.toDouble())
val chunk: SimpleChunkLocation
get() = SimpleChunkLocation(x.toInt() shr 4, z.toInt() shr 4)
}
data class SimpleChunkLocation(val x: Int, val z: Int)
data class SimpleChunkLocation(
val x: Int,
val z: Int
)
// CONVERTER
fun Location.toSimple() = SimpleLocation3D(x, y, z)
fun Chunk.toSimple() = SimpleChunkLocation(x, z)
fun Vector.toSimpleLoc() = SimpleLocation3D(x, y, z)
fun SimpleLocation3D.withWorld(world: World) = Location(world, x, y, z)
fun SimpleLocation3D.withWorld(world: World) = Location(world, x, y, z).apply { direction = this@withWorld.direction }
fun SimpleChunkLocation.withWorld(world: World) = world.getChunkAt(x, z)
fun SimpleLocation3D.toVector() = Vector(x, y, z)
fun Vector.toSimpleLoc() = SimpleLocation3D(x, y, z)
// EXTENSIONS
val Location.worldOrException: World get() = world ?: throw NullPointerException("The world of the location is null!")
fun SimpleLocation3D.toVector() = Vector(x, y, z)