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.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<CompoundTag?> {
|
||||
}
|
||||
|
||||
fun decimal(prop: KMutableProperty0<Decimal>, name: String = prop.name, default: Decimal = Decimal.ZERO): Stateless<Decimal, Tag> {
|
||||
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<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> {
|
||||
return Stateless(prop, name, NumericTag::class.java)
|
||||
.withSerializer { FloatTag.valueOf(it) }
|
||||
.withDeserializer { it.asFloat }
|
||||
return float(prop.asGetterSetter(), name)
|
||||
}
|
||||
|
||||
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> {
|
||||
return Stateless(prop, name, NumericTag::class.java)
|
||||
.withSerializer { DoubleTag.valueOf(it) }
|
||||
.withDeserializer { it.asDouble }
|
||||
return double(prop.asGetterSetter(), name)
|
||||
}
|
||||
|
||||
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> {
|
||||
return Stateless(prop, name, NumericTag::class.java)
|
||||
.withSerializer { IntTag.valueOf(it) }
|
||||
.withDeserializer { it.asInt }
|
||||
return int(prop.asGetterSetter(), name)
|
||||
}
|
||||
|
||||
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> {
|
||||
return Stateless(prop, name, NumericTag::class.java)
|
||||
.withSerializer { LongTag.valueOf(it) }
|
||||
.withDeserializer { it.asLong }
|
||||
return long(prop.asGetterSetter(), name)
|
||||
}
|
||||
|
||||
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> {
|
||||
return Stateless(prop, name, NumericTag::class.java)
|
||||
.withSerializer { ByteTag.valueOf(it) }
|
||||
.withDeserializer { it.asByte > 0 }
|
||||
return bool(prop.asGetterSetter(), name)
|
||||
}
|
||||
|
||||
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> {
|
||||
return Stateless(prop, name, StringTag::class.java)
|
||||
.withSerializer { StringTag.valueOf(it) }
|
||||
.withDeserializer { it.asString }
|
||||
return string(prop.asGetterSetter(), name)
|
||||
}
|
||||
|
||||
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> {
|
||||
return Stateless(prop, name, StringTag::class.java)
|
||||
.withSerializer { StringTag.valueOf(it.name) }
|
||||
.withDeserializer { map.invoke(it.asString) }
|
||||
return enum(prop.asGetterSetter(), name, map)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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> {
|
||||
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: 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> {
|
||||
return codec(prop.asGetterSetter(), codec, name)
|
||||
}
|
||||
|
||||
fun vector(prop: GetterSetter<Vector>, name: String, default: Vector = Vector.ZERO): Stateless<Vector, ListTag> {
|
||||
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 <T : Any> codec(prop: KMutableProperty0<T>, codec: Codec<T>, name: String = prop.name, default: T): Stateless<T, Tag> {
|
||||
return codec(prop.asGetterSetter(), codec, name, default)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
fun location(prop: GetterSetter<ResourceLocation>, name: String): Stateless<ResourceLocation, StringTag> {
|
||||
return Stateless(prop, name, StringTag::class.java)
|
||||
.withSerializer { StringTag.valueOf(it.toString()) }
|
||||
.withDeserializer { ResourceLocation(it.asString) }
|
||||
fun location(prop: GetterSetter<ResourceLocation>, name: String): Stateless<ResourceLocation, Tag> {
|
||||
return codec(prop, ResourceLocation.CODEC, name)
|
||||
}
|
||||
|
||||
fun location(prop: KMutableProperty0<ResourceLocation>, name: String = prop.name): Stateless<ResourceLocation, StringTag> {
|
||||
return Stateless(prop, name, StringTag::class.java)
|
||||
.withSerializer { StringTag.valueOf(it.toString()) }
|
||||
.withDeserializer { ResourceLocation(it.asString) }
|
||||
fun location(prop: KMutableProperty0<ResourceLocation>, name: String = prop.name): Stateless<ResourceLocation, Tag> {
|
||||
return location(prop.asGetterSetter(), name)
|
||||
}
|
||||
|
||||
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