From 104600e9b049759b17143a0dae5916d663f8ab3b Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Sat, 27 Jun 2020 00:10:35 +0200 Subject: [PATCH] Create DirectionUtils.kt --- .../net/axay/kspigot/utils/DirectionUtils.kt | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt diff --git a/src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt b/src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt new file mode 100644 index 00000000..36079a50 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt @@ -0,0 +1,52 @@ +package net.axay.kspigot.utils + +import com.google.common.base.Enums +import org.bukkit.Location +import org.bukkit.block.BlockFace + +enum class VerticalDirection { + + UP, DOWN, STRAIGHT; + + val facing: BlockFace? + get() = Enums.getIfPresent(BlockFace::class.java, this.name).orNull() + + companion object { + + fun fromLocation(location: Location): VerticalDirection { + val pitch: Float = location.pitch + return when { + pitch <= -45 -> DOWN + pitch >= 45 -> UP + else -> STRAIGHT + } + } + + } + +} + +enum class CardinalDirection { + + NORTH, EAST, SOUTH, WEST; + + val facing: BlockFace? + get() = Enums.getIfPresent(BlockFace::class.java, this.name).orNull() + + companion object { + + fun fromLocation(location: Location): CardinalDirection { + var yaw: Float = location.yaw + if (yaw < 0) yaw += 360f + return when { + yaw >= 315 || yaw < 45 -> SOUTH + yaw < 135 -> WEST + yaw < 225 -> NORTH + yaw < 315 -> EAST + else -> NORTH + } + } + + } + +} \ No newline at end of file