Add codec to savetables
This commit is contained in:
parent
6105ca797b
commit
f5a9e98c6f
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.mc.otm.core.util
|
package ru.dbotthepony.mc.otm.core.util
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec
|
||||||
import net.minecraft.nbt.ByteTag
|
import net.minecraft.nbt.ByteTag
|
||||||
import net.minecraft.nbt.CompoundTag
|
import net.minecraft.nbt.CompoundTag
|
||||||
import net.minecraft.nbt.DoubleTag
|
import net.minecraft.nbt.DoubleTag
|
||||||
@ -7,6 +8,7 @@ import net.minecraft.nbt.FloatTag
|
|||||||
import net.minecraft.nbt.IntTag
|
import net.minecraft.nbt.IntTag
|
||||||
import net.minecraft.nbt.ListTag
|
import net.minecraft.nbt.ListTag
|
||||||
import net.minecraft.nbt.LongTag
|
import net.minecraft.nbt.LongTag
|
||||||
|
import net.minecraft.nbt.NbtOps
|
||||||
import net.minecraft.nbt.NumericTag
|
import net.minecraft.nbt.NumericTag
|
||||||
import net.minecraft.nbt.StringTag
|
import net.minecraft.nbt.StringTag
|
||||||
import net.minecraft.nbt.Tag
|
import net.minecraft.nbt.Tag
|
||||||
@ -17,12 +19,14 @@ import ru.dbotthepony.mc.otm.core.asGetterSetter
|
|||||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||||
import ru.dbotthepony.mc.otm.core.math.Vector
|
import ru.dbotthepony.mc.otm.core.math.Vector
|
||||||
import ru.dbotthepony.mc.otm.core.nbt.set
|
import ru.dbotthepony.mc.otm.core.nbt.set
|
||||||
|
import ru.dbotthepony.mc.otm.data.getOrNull
|
||||||
import java.util.function.Supplier
|
import java.util.function.Supplier
|
||||||
import kotlin.reflect.KMutableProperty0
|
import kotlin.reflect.KMutableProperty0
|
||||||
import kotlin.reflect.KProperty0
|
import kotlin.reflect.KProperty0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class to manage serialization and deserialization of properties
|
* Utility class to manage serialization and deserialization of properties
|
||||||
|
* of mutable class
|
||||||
*/
|
*/
|
||||||
class Savetables : INBTSerializable<CompoundTag?> {
|
class Savetables : INBTSerializable<CompoundTag?> {
|
||||||
private val entries = ArrayList<Entry<*, *>>()
|
private val entries = ArrayList<Entry<*, *>>()
|
||||||
@ -175,6 +179,26 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
.withDeserializer { map.invoke(it.asString) }
|
.withDeserializer { map.invoke(it.asString) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T : Any> codecNullable(prop: GetterSetter<T?>, codec: Codec<T>, name: String): Stateless<T?, Tag> {
|
||||||
|
return Stateless(prop, name, Tag::class.java)
|
||||||
|
.withSerializer { prop.get()?.let { codec.encode(it, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow(false) { throw IllegalStateException("Failed to write NBT data for $name: $it") } } }
|
||||||
|
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).getOrThrow(false) { throw IllegalStateException("Failed to read NBT data for $name: $it") }.first }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Any> codecNullable(prop: KMutableProperty0<T?>, codec: Codec<T>, name: String = prop.name): Stateless<T?, Tag> {
|
||||||
|
return codecNullable(prop.asGetterSetter(), codec, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Any> codec(prop: GetterSetter<T>, codec: Codec<T>, name: String): Stateless<T, Tag> {
|
||||||
|
return Stateless(prop, name, Tag::class.java)
|
||||||
|
.withSerializer { codec.encode(prop.get(), NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow(false) { throw IllegalStateException("Failed to write NBT data for $name: $it") } }
|
||||||
|
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).getOrThrow(false) { throw IllegalStateException("Failed to read NBT data for $name: $it") }.first }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Any> codec(prop: KMutableProperty0<T>, codec: Codec<T>, name: String = prop.name): Stateless<T, Tag> {
|
||||||
|
return codec(prop.asGetterSetter(), codec, name)
|
||||||
|
}
|
||||||
|
|
||||||
fun vector(prop: GetterSetter<Vector>, name: String, default: Vector = Vector.ZERO): Stateless<Vector, ListTag> {
|
fun vector(prop: GetterSetter<Vector>, name: String, default: Vector = Vector.ZERO): Stateless<Vector, ListTag> {
|
||||||
return Stateless(prop, name, ListTag::class.java)
|
return Stateless(prop, name, ListTag::class.java)
|
||||||
.withSerializer { ListTag().also { l ->
|
.withSerializer { ListTag().also { l ->
|
||||||
|
Loading…
Reference in New Issue
Block a user