More concise savetables internals
This commit is contained in:
parent
6fbdb04ee4
commit
2935b3c2ad
@ -15,6 +15,7 @@ import net.minecraft.nbt.StringTag
|
|||||||
import net.minecraft.nbt.Tag
|
import net.minecraft.nbt.Tag
|
||||||
import net.minecraft.resources.ResourceLocation
|
import net.minecraft.resources.ResourceLocation
|
||||||
import net.minecraftforge.common.util.INBTSerializable
|
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.GetterSetter
|
||||||
import ru.dbotthepony.mc.otm.core.asGetterSetter
|
import ru.dbotthepony.mc.otm.core.asGetterSetter
|
||||||
import ru.dbotthepony.mc.otm.core.immutableList
|
import ru.dbotthepony.mc.otm.core.immutableList
|
||||||
@ -90,10 +91,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun decimal(prop: KMutableProperty0<Decimal>, name: String = prop.name, default: Decimal = Decimal.ZERO): Stateless<Decimal, Tag> {
|
fun decimal(prop: KMutableProperty0<Decimal>, name: String = prop.name, default: Decimal = Decimal.ZERO): Stateless<Decimal, Tag> {
|
||||||
return Stateless(prop, name, Tag::class.java)
|
return decimal(prop.asGetterSetter(), name, default)
|
||||||
.withDeserializer { Decimal.deserializeNBT(it) }
|
|
||||||
.withSerializer { it.serializeNBT() }
|
|
||||||
.withDefault { default }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun decimalNullable(prop: KMutableProperty0<Decimal?>, name: String = prop.name, default: Decimal = Decimal.ZERO): Stateless<Decimal?, Tag> {
|
fun decimalNullable(prop: KMutableProperty0<Decimal?>, name: String = prop.name, default: Decimal = Decimal.ZERO): Stateless<Decimal?, Tag> {
|
||||||
@ -110,9 +108,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun float(prop: KMutableProperty0<Float>, name: String = prop.name): Stateless<Float, NumericTag> {
|
fun float(prop: KMutableProperty0<Float>, name: String = prop.name): Stateless<Float, NumericTag> {
|
||||||
return Stateless(prop, name, NumericTag::class.java)
|
return float(prop.asGetterSetter(), name)
|
||||||
.withSerializer { FloatTag.valueOf(it) }
|
|
||||||
.withDeserializer { it.asFloat }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun double(prop: GetterSetter<Double>, name: String): Stateless<Double, NumericTag> {
|
fun double(prop: GetterSetter<Double>, name: String): Stateless<Double, NumericTag> {
|
||||||
@ -122,9 +118,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun double(prop: KMutableProperty0<Double>, name: String = prop.name): Stateless<Double, NumericTag> {
|
fun double(prop: KMutableProperty0<Double>, name: String = prop.name): Stateless<Double, NumericTag> {
|
||||||
return Stateless(prop, name, NumericTag::class.java)
|
return double(prop.asGetterSetter(), name)
|
||||||
.withSerializer { DoubleTag.valueOf(it) }
|
|
||||||
.withDeserializer { it.asDouble }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun int(prop: GetterSetter<Int>, name: String): Stateless<Int, NumericTag> {
|
fun int(prop: GetterSetter<Int>, name: String): Stateless<Int, NumericTag> {
|
||||||
@ -134,9 +128,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun int(prop: KMutableProperty0<Int>, name: String = prop.name): Stateless<Int, NumericTag> {
|
fun int(prop: KMutableProperty0<Int>, name: String = prop.name): Stateless<Int, NumericTag> {
|
||||||
return Stateless(prop, name, NumericTag::class.java)
|
return int(prop.asGetterSetter(), name)
|
||||||
.withSerializer { IntTag.valueOf(it) }
|
|
||||||
.withDeserializer { it.asInt }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun long(prop: GetterSetter<Long>, name: String): Stateless<Long, NumericTag> {
|
fun long(prop: GetterSetter<Long>, name: String): Stateless<Long, NumericTag> {
|
||||||
@ -146,9 +138,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun long(prop: KMutableProperty0<Long>, name: String = prop.name): Stateless<Long, NumericTag> {
|
fun long(prop: KMutableProperty0<Long>, name: String = prop.name): Stateless<Long, NumericTag> {
|
||||||
return Stateless(prop, name, NumericTag::class.java)
|
return long(prop.asGetterSetter(), name)
|
||||||
.withSerializer { LongTag.valueOf(it) }
|
|
||||||
.withDeserializer { it.asLong }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bool(prop: GetterSetter<Boolean>, name: String): Stateless<Boolean, NumericTag> {
|
fun bool(prop: GetterSetter<Boolean>, name: String): Stateless<Boolean, NumericTag> {
|
||||||
@ -158,9 +148,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun bool(prop: KMutableProperty0<Boolean>, name: String = prop.name): Stateless<Boolean, NumericTag> {
|
fun bool(prop: KMutableProperty0<Boolean>, name: String = prop.name): Stateless<Boolean, NumericTag> {
|
||||||
return Stateless(prop, name, NumericTag::class.java)
|
return bool(prop.asGetterSetter(), name)
|
||||||
.withSerializer { ByteTag.valueOf(it) }
|
|
||||||
.withDeserializer { it.asByte > 0 }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun string(prop: GetterSetter<String>, name: String): Stateless<String, StringTag> {
|
fun string(prop: GetterSetter<String>, name: String): Stateless<String, StringTag> {
|
||||||
@ -170,9 +158,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun string(prop: KMutableProperty0<String>, name: String = prop.name): Stateless<String, StringTag> {
|
fun string(prop: KMutableProperty0<String>, name: String = prop.name): Stateless<String, StringTag> {
|
||||||
return Stateless(prop, name, StringTag::class.java)
|
return string(prop.asGetterSetter(), name)
|
||||||
.withSerializer { StringTag.valueOf(it) }
|
|
||||||
.withDeserializer { it.asString }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <E : Enum<E>> enum(prop: GetterSetter<E>, name: String, map: (String) -> E): Stateless<E, StringTag> {
|
fun <E : Enum<E>> enum(prop: GetterSetter<E>, name: String, map: (String) -> E): Stateless<E, StringTag> {
|
||||||
@ -182,9 +168,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun <E : Enum<E>> enum(prop: KMutableProperty0<E>, name: String = prop.name, map: (String) -> E): Stateless<E, StringTag> {
|
fun <E : Enum<E>> enum(prop: KMutableProperty0<E>, name: String = prop.name, map: (String) -> E): Stateless<E, StringTag> {
|
||||||
return Stateless(prop, name, StringTag::class.java)
|
return enum(prop.asGetterSetter(), name, map)
|
||||||
.withSerializer { StringTag.valueOf(it.name) }
|
|
||||||
.withDeserializer { map.invoke(it.asString) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : Any> codecNullable(prop: GetterSetter<T?>, codec: Codec<T>, name: String): Stateless<T?, Tag> {
|
fun <T : Any> codecNullable(prop: GetterSetter<T?>, codec: Codec<T>, name: String): Stateless<T?, Tag> {
|
||||||
@ -197,49 +181,52 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
return codecNullable(prop.asGetterSetter(), codec, name)
|
return codecNullable(prop.asGetterSetter(), codec, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T : Any> codecNullable(prop: GetterSetter<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(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 <T : Any> codecNullable(prop: KMutableProperty0<T?>, codec: Codec<T>, name: String = prop.name, default: T?): Stateless<T?, Tag> {
|
||||||
|
return codecNullable(prop.asGetterSetter(), codec, name, default)
|
||||||
|
}
|
||||||
|
|
||||||
fun <T : Any> codec(prop: GetterSetter<T>, codec: Codec<T>, name: String): Stateless<T, Tag> {
|
fun <T : Any> codec(prop: GetterSetter<T>, codec: Codec<T>, name: String): Stateless<T, Tag> {
|
||||||
return Stateless(prop, name, Tag::class.java)
|
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") } }
|
.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 }
|
.withDeserializer { codec.decode(NbtOps.INSTANCE, it).getOrThrow(false) { throw IllegalStateException("Failed to read NBT data for $name: $it") }.first }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T : Any> codec(prop: GetterSetter<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(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 <T : Any> codec(prop: KMutableProperty0<T>, codec: Codec<T>, name: String = prop.name): Stateless<T, Tag> {
|
fun <T : Any> codec(prop: KMutableProperty0<T>, codec: Codec<T>, name: String = prop.name): Stateless<T, Tag> {
|
||||||
return codec(prop.asGetterSetter(), codec, name)
|
return codec(prop.asGetterSetter(), codec, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun vector(prop: GetterSetter<Vector>, name: String, default: Vector = Vector.ZERO): Stateless<Vector, ListTag> {
|
fun <T : Any> codec(prop: KMutableProperty0<T>, codec: Codec<T>, name: String = prop.name, default: T): Stateless<T, Tag> {
|
||||||
return Stateless(prop, name, ListTag::class.java)
|
return codec(prop.asGetterSetter(), codec, name, default)
|
||||||
.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 vector(prop: KMutableProperty0<Vector>, name: String = prop.name, default: Vector = Vector.ZERO): Stateless<Vector, ListTag> {
|
fun vector(prop: GetterSetter<Vector>, name: String, default: Vector = Vector.ZERO): Stateless<Vector, Tag> {
|
||||||
|
return codec(prop, Vector.CODEC, name, default)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun vector(prop: KMutableProperty0<Vector>, name: String = prop.name, default: Vector = Vector.ZERO): Stateless<Vector, Tag> {
|
||||||
return vector(prop.asGetterSetter(), name, default)
|
return vector(prop.asGetterSetter(), name, default)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun location(prop: GetterSetter<ResourceLocation>, name: String): Stateless<ResourceLocation, StringTag> {
|
fun location(prop: GetterSetter<ResourceLocation>, name: String): Stateless<ResourceLocation, Tag> {
|
||||||
return Stateless(prop, name, StringTag::class.java)
|
return codec(prop, ResourceLocation.CODEC, name)
|
||||||
.withSerializer { StringTag.valueOf(it.toString()) }
|
|
||||||
.withDeserializer { ResourceLocation(it.asString) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun location(prop: KMutableProperty0<ResourceLocation>, name: String = prop.name): Stateless<ResourceLocation, StringTag> {
|
fun location(prop: KMutableProperty0<ResourceLocation>, name: String = prop.name): Stateless<ResourceLocation, Tag> {
|
||||||
return Stateless(prop, name, StringTag::class.java)
|
return location(prop.asGetterSetter(), name)
|
||||||
.withSerializer { StringTag.valueOf(it.toString()) }
|
|
||||||
.withDeserializer { ResourceLocation(it.asString) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serializeNBT(): CompoundTag {
|
override fun serializeNBT(): CompoundTag {
|
||||||
@ -381,4 +368,8 @@ class Savetables : INBTSerializable<CompoundTag?> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val LOGGER = LogManager.getLogger()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user