Add custom global textures
This commit is contained in:
@@ -6,15 +6,24 @@
|
||||
|
||||
package moe.nea.firmament.features.texturepack
|
||||
|
||||
import com.google.gson.JsonArray
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonNull
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonPrimitive
|
||||
import com.google.gson.internal.LazilyParsedNumber
|
||||
import java.util.function.Predicate
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import net.minecraft.nbt.NbtString
|
||||
import net.minecraft.text.Text
|
||||
import moe.nea.firmament.util.MC
|
||||
import moe.nea.firmament.util.removeColorCodes
|
||||
|
||||
@Serializable(with = StringMatcher.Serializer::class)
|
||||
interface StringMatcher {
|
||||
fun matches(string: String): Boolean
|
||||
fun matches(text: Text): Boolean {
|
||||
@@ -46,7 +55,28 @@ interface StringMatcher {
|
||||
}
|
||||
}
|
||||
|
||||
object Serializer : KSerializer<StringMatcher> {
|
||||
val delegateSerializer = kotlinx.serialization.json.JsonElement.serializer()
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = SerialDescriptor("StringMatcher", delegateSerializer.descriptor)
|
||||
|
||||
override fun deserialize(decoder: Decoder): StringMatcher {
|
||||
val delegate = decoder.decodeSerializableValue(delegateSerializer)
|
||||
val gsonDelegate = delegate.intoGson()
|
||||
return parse(gsonDelegate)
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: StringMatcher) {
|
||||
encoder.encodeSerializableValue(delegateSerializer, Companion.serialize(value).intoKotlinJson())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun serialize(stringMatcher: StringMatcher):JsonElement {
|
||||
TODO("Cannot serialize string matchers rn")
|
||||
}
|
||||
|
||||
fun parse(jsonElement: JsonElement): StringMatcher {
|
||||
if (jsonElement is JsonPrimitive) {
|
||||
return Equals(jsonElement.asString, true)
|
||||
@@ -69,3 +99,58 @@ interface StringMatcher {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun JsonElement.intoKotlinJson(): kotlinx.serialization.json.JsonElement {
|
||||
when (this) {
|
||||
is JsonNull -> return kotlinx.serialization.json.JsonNull
|
||||
is JsonObject -> {
|
||||
return kotlinx.serialization.json.JsonObject(this.entrySet()
|
||||
.associate { it.key to it.value.intoKotlinJson() })
|
||||
}
|
||||
|
||||
is JsonArray -> {
|
||||
return kotlinx.serialization.json.JsonArray(this.map { it.intoKotlinJson() })
|
||||
}
|
||||
|
||||
is JsonPrimitive -> {
|
||||
if (this.isString)
|
||||
return kotlinx.serialization.json.JsonPrimitive(this.asString)
|
||||
if (this.isBoolean)
|
||||
return kotlinx.serialization.json.JsonPrimitive(this.asBoolean)
|
||||
return kotlinx.serialization.json.JsonPrimitive(this.asNumber)
|
||||
}
|
||||
|
||||
else -> error("Unknown json variant $this")
|
||||
}
|
||||
}
|
||||
|
||||
fun kotlinx.serialization.json.JsonElement.intoGson(): JsonElement {
|
||||
when (this) {
|
||||
is kotlinx.serialization.json.JsonNull -> return JsonNull.INSTANCE
|
||||
is kotlinx.serialization.json.JsonPrimitive -> {
|
||||
if (this.isString)
|
||||
return JsonPrimitive(this.content)
|
||||
if (this.content == "true")
|
||||
return JsonPrimitive(true)
|
||||
if (this.content == "false")
|
||||
return JsonPrimitive(false)
|
||||
return JsonPrimitive(LazilyParsedNumber(this.content))
|
||||
}
|
||||
|
||||
is kotlinx.serialization.json.JsonObject -> {
|
||||
val obj = JsonObject()
|
||||
for ((k, v) in this) {
|
||||
obj.add(k, v.intoGson())
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
is kotlinx.serialization.json.JsonArray -> {
|
||||
val arr = JsonArray()
|
||||
for (v in this) {
|
||||
arr.add(v.intoGson())
|
||||
}
|
||||
return arr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user