mirror of
https://gitlab.bixilon.de/bixilon/pixlyzer.git
synced 2025-09-28 14:41:57 -04:00
trim message types, fix for < 1.19
This commit is contained in:
parent
8d145e23f9
commit
b47115f94e
@ -1,37 +1,59 @@
|
|||||||
package de.bixilon.pixlyzer.generator.generators
|
package de.bixilon.pixlyzer.generator.generators
|
||||||
|
|
||||||
import de.bixilon.pixlyzer.generator.Generator
|
import de.bixilon.pixlyzer.generator.Generator
|
||||||
|
import de.bixilon.pixlyzer.util.ReflectionUtil.getClass
|
||||||
|
import de.bixilon.pixlyzer.util.ReflectionUtil.getField
|
||||||
import de.bixilon.pixlyzer.util.Util
|
import de.bixilon.pixlyzer.util.Util
|
||||||
import de.bixilon.pixlyzer.util.Util.JSON_MAP_TYPE
|
import de.bixilon.pixlyzer.util.Util.JSON_MAP_TYPE
|
||||||
import de.bixilon.pixlyzer.util.Util.compound
|
import de.bixilon.pixlyzer.util.Util.compound
|
||||||
|
import de.bixilon.pixlyzer.util.Util.nullCast
|
||||||
import net.minecraft.text.Decoration
|
import net.minecraft.text.Decoration
|
||||||
import net.minecraft.text.Decoration.Parameter
|
import net.minecraft.text.Decoration.Parameter
|
||||||
import net.minecraft.util.registry.BuiltinRegistries
|
import net.minecraft.text.Style
|
||||||
|
import net.minecraft.util.registry.Registry
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
object MessageTypeGenerator : Generator(
|
object MessageTypeGenerator : Generator(
|
||||||
"message_types"
|
"message_types",
|
||||||
|
allowedFail = true,
|
||||||
|
allowEmpty = true,
|
||||||
) {
|
) {
|
||||||
|
private val DEFAULT_FONT = Util.MAPPER.convertValue<MutableMap<String, Any>>(Style.DEFAULT_FONT_ID, JSON_MAP_TYPE)
|
||||||
|
|
||||||
|
|
||||||
|
val registry = getField(getClass("net.minecraft.util.registry.BuiltinRegistries"), "MESSAGE_TYPE")?.get(null) as Registry<Any>?
|
||||||
|
val messageTypeClass = getClass("net.minecraft.network.message.MessageType")
|
||||||
|
|
||||||
override fun generate() {
|
override fun generate() {
|
||||||
val registry = BuiltinRegistries.MESSAGE_TYPE
|
if (registry == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for (type in registry) {
|
for (type in registry) {
|
||||||
val identifier = registry.getId(type)
|
val identifier = registry.getId(type)
|
||||||
|
|
||||||
val json = compound()
|
val json = compound()
|
||||||
|
|
||||||
json["id"] = registry.getRawId(type)
|
json["id"] = registry.getRawId(type)
|
||||||
json["chat"] = type.chat.serialize()
|
getField(messageTypeClass, "chat")?.get(type)?.serializeChat()?.let { json["chat"] = it }
|
||||||
json["narration"] = type.narration.serialize()
|
getField(messageTypeClass, "narration")?.get(type)?.nullCast<Decoration>()?.serialize()?.let { json["narration"] = it }
|
||||||
|
|
||||||
this.data[identifier.toString()] = json
|
this.data[identifier.toString()] = json
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun Any.serializeChat(): Map<String, Any>? {
|
||||||
|
if (this is Decoration) {
|
||||||
|
return this.serialize()
|
||||||
|
}
|
||||||
|
return getField(this::class.java, "decoration")?.nullCast<Optional<Decoration>>()?.get()?.serialize()
|
||||||
|
}
|
||||||
|
|
||||||
fun Decoration.serialize(): Map<String, Any> {
|
fun Decoration.serialize(): Map<String, Any> {
|
||||||
return mapOf(
|
return mapOf(
|
||||||
"translation_key" to translationKey,
|
"translation_key" to translationKey,
|
||||||
"parameters" to parameters.serialize(),
|
"parameters" to parameters.serialize(),
|
||||||
"style" to Util.MAPPER.convertValue(style, JSON_MAP_TYPE),
|
"style" to Util.MAPPER.convertValue<MutableMap<String, Any>>(style, JSON_MAP_TYPE).trimStyle(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,4 +66,22 @@ object MessageTypeGenerator : Generator(
|
|||||||
|
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun MutableMap<String, Any>.trimStyle(): Map<String, Any> {
|
||||||
|
removeIf("empty") { it == true }
|
||||||
|
removeIf("underlined") { it == false }
|
||||||
|
removeIf("strikethrough") { it == false }
|
||||||
|
removeIf("bold") { it == false }
|
||||||
|
removeIf("italic") { it == false }
|
||||||
|
removeIf("obfuscated") { it == false }
|
||||||
|
removeIf("font") { it == DEFAULT_FONT }
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <K, V> MutableMap<K, V>.removeIf(key: K, check: (V?) -> Boolean) {
|
||||||
|
if (check(this[key])) {
|
||||||
|
remove(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package de.bixilon.pixlyzer.util
|
package de.bixilon.pixlyzer.util
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import com.fasterxml.jackson.databind.type.MapType
|
import com.fasterxml.jackson.databind.type.MapType
|
||||||
import net.minecraft.client.MinecraftClient
|
import net.minecraft.client.MinecraftClient
|
||||||
@ -9,6 +10,7 @@ import java.security.MessageDigest
|
|||||||
|
|
||||||
object Util {
|
object Util {
|
||||||
val MAPPER = ObjectMapper()
|
val MAPPER = ObjectMapper()
|
||||||
|
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
MAPPER.setDefaultPrettyPrinter(PrettyPrinter())
|
MAPPER.setDefaultPrettyPrinter(PrettyPrinter())
|
||||||
@ -121,7 +123,7 @@ object Util {
|
|||||||
}
|
}
|
||||||
fileInputStream.close()
|
fileInputStream.close()
|
||||||
|
|
||||||
val hash: String = Util.byteArrayToHexString(crypt.digest())
|
val hash: String = byteArrayToHexString(crypt.digest())
|
||||||
|
|
||||||
val outputFile = File(outputPath.replace("\${shortHash}", hash.substring(0, 2)).replace("\${hash}", hash))
|
val outputFile = File(outputPath.replace("\${shortHash}", hash.substring(0, 2)).replace("\${hash}", hash))
|
||||||
outputFile.parentFile.mkdirs()
|
outputFile.parentFile.mkdirs()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user