Skip to content

Commit

Permalink
feat: math extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidJoker committed Jan 4, 2025
1 parent b0e5f45 commit 1b770a7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/cc/modlabs/kpaper/extensions/LocationExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@ fun Location.inWorld(worlds: String) = this.clone().apply { this.world = Bukkit.

fun Location.toNorth(): Location {
return this.apply { this.yaw = 180.0f; this.pitch = 0f }
}

fun Location.toSaveAbleString(): String {
return "${world.name};$x;$y;$z"
}

fun Location.toSaveAbleBlockString(): String {
return "${world.name};${blockX};${blockY};${blockZ}"
}

fun Location.toSaveAbleDirectionalString(): String {
return "${world.name};$x;$y;$z;$yaw;$pitch"
}
50 changes: 50 additions & 0 deletions src/main/kotlin/cc/modlabs/kpaper/extensions/MathExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cc.modlabs.kpaper.extensions

import kotlin.math.abs


private fun getNextId(x: Int, y: Int): Pair<Int, Int> {
val absX: Int = abs(x)
val absY: Int = abs(y)
return if (absX > absY) {
if (x > 0) {
Pair(x, y + 1)
} else {
Pair(x, y - 1)
}
} else if (absY > absX) {
if (y > 0) {
Pair(x - 1, y)
} else {
Pair(x + 1, y)
}
} else {
if (x == y && x > 0) {
return Pair(x, y + 1)
}
if (x == absX) {
return Pair(x, y + 1)
}
if (y == absY) {
Pair(x, y - 1)
} else Pair(x + 1, y)
}
}

fun getPlotId(id: Int): Pair<Int, Int> {
var x = 0
var y = 0
var i = 0
while (i < id) {
val next = getNextId(x, y)
x = next.first
y = next.second
i++
}
return Pair(x, y)
}

fun getPlotIdAsHash(id: Int): String {
val pair = getPlotId(id)
return Base64.encodeToString("${pair.first}:${pair.second}").replace("=", "")
}

0 comments on commit 1b770a7

Please sign in to comment.