From 2935b3c2adcb2f1dedb663d1c4edcbe2a4cbb0db Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 22 Aug 2023 11:34:41 +0700 Subject: [PATCH] More concise savetables internals --- .../mc/otm/core/util/Savetables.kt | 93 +++++++++---------- 1 file changed, 42 insertions(+), 51 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt index ba1e29b5f..bd8a2b099 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt @@ -15,6 +15,7 @@ import net.minecraft.nbt.StringTag import net.minecraft.nbt.Tag import net.minecraft.resources.ResourceLocation import net.minecraftforge.common.util.INBTSerializable +import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.core.GetterSetter import ru.dbotthepony.mc.otm.core.asGetterSetter import ru.dbotthepony.mc.otm.core.immutableList @@ -90,10 +91,7 @@ class Savetables : INBTSerializable { } fun decimal(prop: KMutableProperty0, name: String = prop.name, default: Decimal = Decimal.ZERO): Stateless { - return Stateless(prop, name, Tag::class.java) - .withDeserializer { Decimal.deserializeNBT(it) } - .withSerializer { it.serializeNBT() } - .withDefault { default } + return decimal(prop.asGetterSetter(), name, default) } fun decimalNullable(prop: KMutableProperty0, name: String = prop.name, default: Decimal = Decimal.ZERO): Stateless { @@ -110,9 +108,7 @@ class Savetables : INBTSerializable { } fun float(prop: KMutableProperty0, name: String = prop.name): Stateless { - return Stateless(prop, name, NumericTag::class.java) - .withSerializer { FloatTag.valueOf(it) } - .withDeserializer { it.asFloat } + return float(prop.asGetterSetter(), name) } fun double(prop: GetterSetter, name: String): Stateless { @@ -122,9 +118,7 @@ class Savetables : INBTSerializable { } fun double(prop: KMutableProperty0, name: String = prop.name): Stateless { - return Stateless(prop, name, NumericTag::class.java) - .withSerializer { DoubleTag.valueOf(it) } - .withDeserializer { it.asDouble } + return double(prop.asGetterSetter(), name) } fun int(prop: GetterSetter, name: String): Stateless { @@ -134,9 +128,7 @@ class Savetables : INBTSerializable { } fun int(prop: KMutableProperty0, name: String = prop.name): Stateless { - return Stateless(prop, name, NumericTag::class.java) - .withSerializer { IntTag.valueOf(it) } - .withDeserializer { it.asInt } + return int(prop.asGetterSetter(), name) } fun long(prop: GetterSetter, name: String): Stateless { @@ -146,9 +138,7 @@ class Savetables : INBTSerializable { } fun long(prop: KMutableProperty0, name: String = prop.name): Stateless { - return Stateless(prop, name, NumericTag::class.java) - .withSerializer { LongTag.valueOf(it) } - .withDeserializer { it.asLong } + return long(prop.asGetterSetter(), name) } fun bool(prop: GetterSetter, name: String): Stateless { @@ -158,9 +148,7 @@ class Savetables : INBTSerializable { } fun bool(prop: KMutableProperty0, name: String = prop.name): Stateless { - return Stateless(prop, name, NumericTag::class.java) - .withSerializer { ByteTag.valueOf(it) } - .withDeserializer { it.asByte > 0 } + return bool(prop.asGetterSetter(), name) } fun string(prop: GetterSetter, name: String): Stateless { @@ -170,9 +158,7 @@ class Savetables : INBTSerializable { } fun string(prop: KMutableProperty0, name: String = prop.name): Stateless { - return Stateless(prop, name, StringTag::class.java) - .withSerializer { StringTag.valueOf(it) } - .withDeserializer { it.asString } + return string(prop.asGetterSetter(), name) } fun > enum(prop: GetterSetter, name: String, map: (String) -> E): Stateless { @@ -182,9 +168,7 @@ class Savetables : INBTSerializable { } fun > enum(prop: KMutableProperty0, name: String = prop.name, map: (String) -> E): Stateless { - return Stateless(prop, name, StringTag::class.java) - .withSerializer { StringTag.valueOf(it.name) } - .withDeserializer { map.invoke(it.asString) } + return enum(prop.asGetterSetter(), name, map) } fun codecNullable(prop: GetterSetter, codec: Codec, name: String): Stateless { @@ -197,49 +181,52 @@ class Savetables : INBTSerializable { return codecNullable(prop.asGetterSetter(), codec, name) } + fun codecNullable(prop: GetterSetter, codec: Codec, name: String, default: T?): Stateless { + 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).get().map({ it.first }, { LOGGER.error("Failed to read NBT data for $name", RuntimeException(it.message())); default }) } + .withDefault { default } + } + + fun codecNullable(prop: KMutableProperty0, codec: Codec, name: String = prop.name, default: T?): Stateless { + return codecNullable(prop.asGetterSetter(), codec, name, default) + } + fun codec(prop: GetterSetter, codec: Codec, name: String): Stateless { 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 codec(prop: GetterSetter, codec: Codec, name: String, default: T): Stateless { + 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).get().map({ it.first }, { LOGGER.error("Failed to read NBT data for $name", RuntimeException(it.message())); default }) } + .withDefault { default } + } + fun codec(prop: KMutableProperty0, codec: Codec, name: String = prop.name): Stateless { return codec(prop.asGetterSetter(), codec, name) } - fun vector(prop: GetterSetter, name: String, default: Vector = Vector.ZERO): Stateless { - return Stateless(prop, name, ListTag::class.java) - .withSerializer { ListTag().also { l -> - l.add(DoubleTag.valueOf(it.x)) - l.add(DoubleTag.valueOf(it.y)) - l.add(DoubleTag.valueOf(it.z)) - } } - .withDeserializer { - if (it.size < 3) { - default - } else { - val x = it[0] as? NumericTag ?: return@withDeserializer default - val y = it[1] as? NumericTag ?: return@withDeserializer default - val z = it[2] as? NumericTag ?: return@withDeserializer default - Vector(x.asDouble, y.asDouble, z.asDouble) - } - } + fun codec(prop: KMutableProperty0, codec: Codec, name: String = prop.name, default: T): Stateless { + return codec(prop.asGetterSetter(), codec, name, default) } - fun vector(prop: KMutableProperty0, name: String = prop.name, default: Vector = Vector.ZERO): Stateless { + fun vector(prop: GetterSetter, name: String, default: Vector = Vector.ZERO): Stateless { + return codec(prop, Vector.CODEC, name, default) + } + + fun vector(prop: KMutableProperty0, name: String = prop.name, default: Vector = Vector.ZERO): Stateless { return vector(prop.asGetterSetter(), name, default) } - fun location(prop: GetterSetter, name: String): Stateless { - return Stateless(prop, name, StringTag::class.java) - .withSerializer { StringTag.valueOf(it.toString()) } - .withDeserializer { ResourceLocation(it.asString) } + fun location(prop: GetterSetter, name: String): Stateless { + return codec(prop, ResourceLocation.CODEC, name) } - fun location(prop: KMutableProperty0, name: String = prop.name): Stateless { - return Stateless(prop, name, StringTag::class.java) - .withSerializer { StringTag.valueOf(it.toString()) } - .withDeserializer { ResourceLocation(it.asString) } + fun location(prop: KMutableProperty0, name: String = prop.name): Stateless { + return location(prop.asGetterSetter(), name) } override fun serializeNBT(): CompoundTag { @@ -381,4 +368,8 @@ class Savetables : INBTSerializable { } } } + + companion object { + private val LOGGER = LogManager.getLogger() + } }