Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.github.pylonmc.pylon.core.datatypes.PylonSerializers
import io.github.pylonmc.pylon.core.event.PylonBlockDeserializeEvent
import io.github.pylonmc.pylon.core.event.PylonBlockSerializeEvent
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 @@ -33,7 +34,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 @@ -68,28 +69,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 @@ -165,10 +147,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 @@ -38,7 +38,9 @@ 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
val fallback = component.fallback()
var translated = getTranslation(component, locale)
?: fallback?.let { getTranslation(it, locale) ?: Component.text(fallback) } ?: return null
for (arg in component.arguments()) {
val component = arg.asComponent()
if (component !is VirtualComponent) continue
Expand Down Expand Up @@ -76,7 +78,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
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,24 @@ open class PylonItem(val stack: ItemStack) : Keyed {

companion object {

private val nameAndLoreWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()
private val nameWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()

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

// Adventure is a perfect API with absolutely no problems whatsoever.
val name = schema.itemStack.getData(DataComponentTypes.ITEM_NAME) as? TranslatableComponent
val lore = schema.itemStack.getData(DataComponentTypes.LORE)?.lines()?.get(0) as? TranslatableComponent

var isNameAndLoreValid = true
var isNameValid = true
if (name == null || name.key() != ItemStackBuilder.nameKey(schema.key)) {
PylonCore.logger.warning("Item ${schema.key}'s name is not a translation key; check your item uses ItemStackBuilder.pylonItem(...)")
isNameAndLoreValid = false
isNameValid = false
}

if (lore == null || lore.key() != ItemStackBuilder.loreKey(schema.key)) {
PylonCore.logger.warning("Item ${schema.key}'s lore is not a translation key; check your item uses ItemStackBuilder.pylonItem(...)")
isNameAndLoreValid = false
}

if (isNameAndLoreValid) {
if (isNameValid) {
for (locale in schema.addon.languages) {
if (!translator.translationKeyExists(name!!.key(), locale)) {
PylonCore.logger.warning(
Expand All @@ -75,22 +69,13 @@ open class PylonItem(val stack: ItemStack) : Keyed {
}"
)
}
if (!translator.translationKeyExists(lore!!.key(), locale)) {
PylonCore.logger.warning(
"${schema.key.namespace} is missing a lore translation key for item ${schema.key} (locale: ${locale.displayName} | expected translation key: ${
ItemStackBuilder.loreKey(
schema.key
)
}"
)
}
}
}
}

private fun register(schema: PylonItemSchema) {
if (schema.key !in nameAndLoreWarningsSupressed) {
checkNameAndLore(schema)
if (schema.key !in nameWarningsSupressed) {
checkName(schema)
}
PylonRegistry.ITEMS.register(schema)
}
Expand Down Expand Up @@ -122,8 +107,8 @@ open class PylonItem(val stack: ItemStack) : Keyed {
}

@JvmStatic
fun supressNameAndLoreWarnings(key: NamespacedKey) {
nameAndLoreWarningsSupressed.add(key)
fun supressNameWarnings(key: NamespacedKey) {
nameWarningsSupressed.add(key)
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ open class ItemStackBuilder private constructor(val stack: ItemStack) : ItemProv
fun lore(vararg lore: String) = lore(*lore.map(::fromMiniMessage).toTypedArray())

fun defaultTranslatableLore(key: NamespacedKey) =
lore(Component.translatable(loreKey(key)))
lore(Component.translatable(loreKey(key), ""))

fun build(): ItemStack = stack.clone()

Expand Down
Loading