Skip to content

Commit 7f9b59c

Browse files
committed
chore: fix loc and quaternion serializers
1 parent 92ecc8c commit 7f9b59c

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

idofront-serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/LocationSerializer.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mineinabyss.idofront.serialization
22

3+
import com.mineinabyss.idofront.util.ensureSize
4+
import com.nexomc.nexo.utils.ifNotEmpty
35
import kotlinx.serialization.EncodeDefault
46
import kotlinx.serialization.KSerializer
57
import kotlinx.serialization.SerialName
@@ -60,9 +62,9 @@ object LocationAltSerializer : KSerializer<Location> {
6062
// location: x,y,z yaw,pitch world
6163
override fun deserialize(decoder: Decoder): Location {
6264
val string = decoder.decodeString()
63-
val (x,y,z) = string.substringBefore(" ").split(",").map { it.toDoubleOrNull() ?: 0.0 }
64-
val (yaw, pitch) = string.substringAfter(" ").substringBeforeLast(" ").split(",").map { it.toFloatOrNull() ?: 0.0f }
65-
val world = string.substringAfterLast(" ").let(Bukkit::getWorld) ?: defaultWorld
65+
val (x,y,z) = string.substringBefore(" ").split(",").ensureSize(3, "0").map { it.toDoubleOrNull() ?: 0.0 }
66+
val (yaw, pitch) = string.substringAfter(" ", "").substringBeforeLast(" ").split((",")).ensureSize(2, "0").map { it.toFloatOrNull() ?: 0f }
67+
val world = string.substringAfterLast(" ", "").let(Bukkit::getWorld) ?: defaultWorld
6668

6769
return Location(world, x, y, z, yaw, pitch)
6870
}

idofront-serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/QuaternionSerializer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ object QuaternionfAltSerializer : KSerializer<Quaternionf> {
4747
}
4848

4949
override fun deserialize(decoder: Decoder): Quaternionf {
50-
val string = decoder.decodeString()
51-
val (x, y, z, w) = string.split(",", limit = 4).map { it.toFloatOrNull() }
50+
val string = decoder.decodeString().split(",", limit = 4).toMutableList()
51+
val (x, y, z, w) = (0..3).map { string.getOrNull(it)?.toFloatOrNull() }
5252

5353
return Quaternionf(x ?: 0f, y ?: 0f, z ?: 0f, w ?: 1f)
5454
}

idofront-util/src/main/kotlin/com/mineinabyss/idofront/util/FastUtils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,10 @@ inline fun <T, K> Iterable<T>.groupByFastSet(keySelector: (T) -> K): Object2Obje
276276
map.computeIfAbsent(key) { ObjectOpenHashSet() }.add(element)
277277
}
278278
return map
279+
}
280+
281+
fun <T> List<T>.ensureSize(size: Int, default: T): MutableList<T> {
282+
val list = toMutableList()
283+
while (list.size < size) list.add(default)
284+
return list
279285
}

0 commit comments

Comments
 (0)