aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util/json/BlockPosSerializer.kt
blob: 144b0a00fde8f4cd3490b72a7df517af7b7cf2a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package moe.nea.firmament.util.json

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.serializer
import net.minecraft.util.math.BlockPos

object BlockPosSerializer : KSerializer<BlockPos> {
    val delegate = serializer<List<Int>>()

    override val descriptor: SerialDescriptor
        get() = SerialDescriptor("BlockPos", delegate.descriptor)

    override fun deserialize(decoder: Decoder): BlockPos {
        val list = decoder.decodeSerializableValue(delegate)
        require(list.size == 3)
        return BlockPos(list[0], list[1], list[2])
    }

    override fun serialize(encoder: Encoder, value: BlockPos) {
        encoder.encodeSerializableValue(delegate, listOf(value.x, value.y, value.z))
    }
}