Allow partial results from codecs in Savetables

This commit is contained in:
DBotThePony 2024-08-31 10:01:45 +07:00
parent 64b8956c80
commit 5bdaa8cc88
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -24,6 +24,7 @@ import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.math.Vector
import ru.dbotthepony.mc.otm.core.nbt.set
import java.util.function.Supplier
import kotlin.jvm.optionals.getOrNull
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty0
@ -174,8 +175,8 @@ class Savetables : INBTSerializable<CompoundTag?> {
fun <T : Any> codecNullable(prop: Delegate<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() { throw IllegalStateException("Failed to write NBT data for $name: $it") } } }
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).getOrThrow() { throw IllegalStateException("Failed to read NBT data for $name: $it") }.first }
.withSerializer { prop.get()?.let { codec.encode(it, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow { throw IllegalStateException("Failed to write save data for '$name': $it") } } }
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).resultOrPartial { throw IllegalStateException("Failed to read save data for '$name'", RuntimeException(it)) }.getOrNull()?.first }
}
fun <T : Any> codecNullable(prop: KMutableProperty0<T?>, codec: Codec<T>, name: String = prop.name): Stateless<T?, Tag> {
@ -184,8 +185,8 @@ class Savetables : INBTSerializable<CompoundTag?> {
fun <T : Any> codecNullable(prop: Delegate<T?>, codec: Codec<T>, name: String, default: T?): Stateless<T?, Tag> {
return Stateless(prop, name, Tag::class.java)
.withSerializer { prop.get()?.let { codec.encode(it, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow { throw IllegalStateException("Failed to write NBT data for $name: $it") } } }
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).mapOrElse({ it.first }, { LOGGER.error("Failed to read NBT data for $name", RuntimeException(it.message())); default }) }
.withSerializer { prop.get()?.let { codec.encode(it, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow { throw IllegalStateException("Failed to write save data for '$name': $it") } } }
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).resultOrPartial { LOGGER.error("Failed to read save data for '$name'", RuntimeException(it)) }.map { it.first }.orElse(default) }
.withDefault { default }
}
@ -195,14 +196,27 @@ class Savetables : INBTSerializable<CompoundTag?> {
fun <T : Any> codec(prop: Delegate<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 { throw IllegalStateException("Failed to write NBT data for $name: $it") } }
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).getOrThrow() { throw IllegalStateException("Failed to read NBT data for $name: $it") }.first }
.withSerializer { codec.encode(prop.get(), NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow { throw IllegalStateException("Failed to write save data for '$name': $it") } }
.withDeserializer {
codec.decode(NbtOps.INSTANCE, it).let { res ->
res
.resultOrPartial { LOGGER.error("Failed to read save data for '$name'", RuntimeException(it)) }
.map { it.first }
.orElseThrow { throw RuntimeException("Failed to read save data for '$name': ${res.error().get().message()}") }
}
}
}
fun <T : Any> codec(prop: Delegate<T>, codec: Codec<T>, name: String, default: T): Stateless<T, Tag> {
return Stateless(prop, name, Tag::class.java)
.withSerializer { codec.encode(prop.get(), NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow { throw IllegalStateException("Failed to write NBT data for $name: $it") } }
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).mapOrElse({ it.first }, { LOGGER.error("Failed to read NBT data for $name", RuntimeException(it.message())); default }) }
.withSerializer { codec.encode(prop.get(), NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow { throw IllegalStateException("Failed to write save data for '$name': $it") } }
.withDeserializer {
codec
.decode(NbtOps.INSTANCE, it)
.resultOrPartial { LOGGER.error("Failed to read save data for '$name'", RuntimeException(it)) }
.map { it.first }
.orElse(default)
}
.withDefault { default }
}