Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.github.pylonmc.pylon.core.config.Config
import io.github.pylonmc.pylon.core.config.Settings
import io.github.pylonmc.pylon.core.datatypes.PylonSerializers
import io.github.pylonmc.pylon.core.i18n.AddonTranslator
import io.github.pylonmc.pylon.core.item.PylonItemSchema
import io.github.pylonmc.pylon.core.registry.PylonRegistry
import io.github.pylonmc.pylon.core.util.position.BlockPosition
import io.github.pylonmc.pylon.core.util.position.position
Expand All @@ -32,7 +31,7 @@ open class PylonBlock protected constructor(val block: Block) {
@JvmSynthetic
internal var errorBlock: BlockDisplay? = null

open val name: Component = Component.translatable("pylon.${schema.key.namespace}.item.${schema.key.key}.waila")
open val name: Component = Component.translatable("pylon.${schema.key.namespace}.item.${schema.key.key}.waila", "pylon.${schema.key.namespace}.item.${schema.key.key}.name")

constructor(block: Block, context: BlockCreateContext) : this(block)
constructor(block: Block, pdc: PersistentDataContainer) : this(block)
Expand Down Expand Up @@ -65,28 +64,9 @@ open class PylonBlock protected constructor(val block: Block) {
private val pylonBlockPositionKey = pylonKey("position")
private val pylonBlockErrorKey = pylonKey("error")

private val wailaWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()

private fun checkWaila(schema: PylonBlockSchema) {
val translator = AddonTranslator.translators[schema.addon]
check(translator != null) {
"Addon does not have a translator; did you forget to call registerWithPylon()?"
}

for (locale in schema.addon.languages) {
val translationKey = "pylon.${schema.key.namespace}.item.${schema.key.key}.waila"
if (!translator.translationKeyExists(translationKey, locale)) {
PylonCore.logger.warning("${schema.key.namespace} is missing a WAILA translation key for block ${schema.key} (locale: ${locale.displayName} | expected translation key: $translationKey")
}
}
}

@JvmStatic
fun register(key: NamespacedKey, material: Material, blockClass: Class<out PylonBlock>) {
val schema = PylonBlockSchema(key, material, blockClass)
if (key !in wailaWarningsSupressed) {
checkWaila(schema)
}
PylonRegistry.BLOCKS.register(schema)
}

Expand Down Expand Up @@ -158,10 +138,5 @@ open class PylonBlock protected constructor(val block: Block) {
}
}
}

@JvmStatic
fun supressWailaWarnings(key: NamespacedKey) {
wailaWarningsSupressed.add(key)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextReplacementConfig
import net.kyori.adventure.text.TranslatableComponent
import net.kyori.adventure.text.VirtualComponent
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import net.kyori.adventure.translation.GlobalTranslator
import net.kyori.adventure.translation.Translator
import org.apache.commons.lang3.LocaleUtils
Expand Down Expand Up @@ -37,7 +38,14 @@ class AddonTranslator(private val addon: PylonAddon) : Translator {
override fun translate(key: String, locale: Locale): MessageFormat? = null

override fun translate(component: TranslatableComponent, locale: Locale): Component? {
var translated = getTranslation(component, locale) ?: return null
var translated = getTranslation(component, locale).let {
if (it == null) {
val fallback = component.fallback()
if (fallback != null) {
getTranslation(fallback, locale) ?: return Component.text(fallback)
} else return null
} else it
}
Comment thread
ihateblueb marked this conversation as resolved.
Outdated
for (arg in component.arguments()) {
val component = arg.asComponent()
if (component !is VirtualComponent) continue
Expand Down Expand Up @@ -75,7 +83,7 @@ class AddonTranslator(private val addon: PylonAddon) : Translator {
}

fun translationKeyExists(key: String, locale: Locale): Boolean
= getTranslation(key, locale) != null
= getTranslation(key, locale) != null

companion object : Listener {

Expand Down
Loading