Skip to content

Commit 322f81f

Browse files
authored
Merge pull request #139 from pylonmc/waila-name-fallback
add PylonBlock waila fallback to name
2 parents 3d38a7e + 8926d90 commit 322f81f

4 files changed

Lines changed: 16 additions & 52 deletions

File tree

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/block/PylonBlock.kt

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.github.pylonmc.pylon.core.datatypes.PylonSerializers
1010
import io.github.pylonmc.pylon.core.event.PylonBlockDeserializeEvent
1111
import io.github.pylonmc.pylon.core.event.PylonBlockSerializeEvent
1212
import io.github.pylonmc.pylon.core.i18n.AddonTranslator
13+
import io.github.pylonmc.pylon.core.item.PylonItemSchema
1314
import io.github.pylonmc.pylon.core.registry.PylonRegistry
1415
import io.github.pylonmc.pylon.core.util.position.BlockPosition
1516
import io.github.pylonmc.pylon.core.util.position.position
@@ -33,7 +34,7 @@ open class PylonBlock protected constructor(val block: Block) {
3334
@JvmSynthetic
3435
internal var errorBlock: BlockDisplay? = null
3536

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

3839
constructor(block: Block, context: BlockCreateContext) : this(block)
3940
constructor(block: Block, pdc: PersistentDataContainer) : this(block)
@@ -68,28 +69,9 @@ open class PylonBlock protected constructor(val block: Block) {
6869
private val pylonBlockPositionKey = pylonKey("position")
6970
private val pylonBlockErrorKey = pylonKey("error")
7071

71-
private val wailaWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()
72-
73-
private fun checkWaila(schema: PylonBlockSchema) {
74-
val translator = AddonTranslator.translators[schema.addon]
75-
check(translator != null) {
76-
"Addon does not have a translator; did you forget to call registerWithPylon()?"
77-
}
78-
79-
for (locale in schema.addon.languages) {
80-
val translationKey = "pylon.${schema.key.namespace}.item.${schema.key.key}.waila"
81-
if (!translator.translationKeyExists(translationKey, locale)) {
82-
PylonCore.logger.warning("${schema.key.namespace} is missing a WAILA translation key for block ${schema.key} (locale: ${locale.displayName} | expected translation key: $translationKey")
83-
}
84-
}
85-
}
86-
8772
@JvmStatic
8873
fun register(key: NamespacedKey, material: Material, blockClass: Class<out PylonBlock>) {
8974
val schema = PylonBlockSchema(key, material, blockClass)
90-
if (key !in wailaWarningsSupressed) {
91-
checkWaila(schema)
92-
}
9375
PylonRegistry.BLOCKS.register(schema)
9476
}
9577

@@ -165,10 +147,5 @@ open class PylonBlock protected constructor(val block: Block) {
165147
}
166148
}
167149
}
168-
169-
@JvmStatic
170-
fun supressWailaWarnings(key: NamespacedKey) {
171-
wailaWarningsSupressed.add(key)
172-
}
173150
}
174151
}

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/i18n/AddonTranslator.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class AddonTranslator(private val addon: PylonAddon) : Translator {
3838
override fun translate(key: String, locale: Locale): MessageFormat? = null
3939

4040
override fun translate(component: TranslatableComponent, locale: Locale): Component? {
41-
var translated = getTranslation(component, locale) ?: return null
41+
val fallback = component.fallback()
42+
var translated = getTranslation(component, locale)
43+
?: fallback?.let { getTranslation(it, locale) ?: Component.text(fallback) } ?: return null
4244
for (arg in component.arguments()) {
4345
val component = arg.asComponent()
4446
if (component !is VirtualComponent) continue
@@ -76,7 +78,7 @@ class AddonTranslator(private val addon: PylonAddon) : Translator {
7678
}
7779

7880
fun translationKeyExists(key: String, locale: Locale): Boolean
79-
= getTranslation(key, locale) != null
81+
= getTranslation(key, locale) != null
8082

8183
companion object : Listener {
8284

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/item/PylonItem.kt

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,24 @@ open class PylonItem(val stack: ItemStack) : Keyed {
4141

4242
companion object {
4343

44-
private val nameAndLoreWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()
44+
private val nameWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()
4545

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

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

56-
var isNameAndLoreValid = true
55+
var isNameValid = true
5756
if (name == null || name.key() != ItemStackBuilder.nameKey(schema.key)) {
5857
PylonCore.logger.warning("Item ${schema.key}'s name is not a translation key; check your item uses ItemStackBuilder.pylonItem(...)")
59-
isNameAndLoreValid = false
58+
isNameValid = false
6059
}
6160

62-
if (lore == null || lore.key() != ItemStackBuilder.loreKey(schema.key)) {
63-
PylonCore.logger.warning("Item ${schema.key}'s lore is not a translation key; check your item uses ItemStackBuilder.pylonItem(...)")
64-
isNameAndLoreValid = false
65-
}
66-
67-
if (isNameAndLoreValid) {
61+
if (isNameValid) {
6862
for (locale in schema.addon.languages) {
6963
if (!translator.translationKeyExists(name!!.key(), locale)) {
7064
PylonCore.logger.warning(
@@ -75,22 +69,13 @@ open class PylonItem(val stack: ItemStack) : Keyed {
7569
}"
7670
)
7771
}
78-
if (!translator.translationKeyExists(lore!!.key(), locale)) {
79-
PylonCore.logger.warning(
80-
"${schema.key.namespace} is missing a lore translation key for item ${schema.key} (locale: ${locale.displayName} | expected translation key: ${
81-
ItemStackBuilder.loreKey(
82-
schema.key
83-
)
84-
}"
85-
)
86-
}
8772
}
8873
}
8974
}
9075

9176
private fun register(schema: PylonItemSchema) {
92-
if (schema.key !in nameAndLoreWarningsSupressed) {
93-
checkNameAndLore(schema)
77+
if (schema.key !in nameWarningsSupressed) {
78+
checkName(schema)
9479
}
9580
PylonRegistry.ITEMS.register(schema)
9681
}
@@ -122,8 +107,8 @@ open class PylonItem(val stack: ItemStack) : Keyed {
122107
}
123108

124109
@JvmStatic
125-
fun supressNameAndLoreWarnings(key: NamespacedKey) {
126-
nameAndLoreWarningsSupressed.add(key)
110+
fun supressNameWarnings(key: NamespacedKey) {
111+
nameWarningsSupressed.add(key)
127112
}
128113

129114
@JvmStatic

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/item/builder/ItemStackBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ open class ItemStackBuilder private constructor(val stack: ItemStack) : ItemProv
8383
fun lore(vararg lore: String) = lore(*lore.map(::fromMiniMessage).toTypedArray())
8484

8585
fun defaultTranslatableLore(key: NamespacedKey) =
86-
lore(Component.translatable(loreKey(key)))
86+
lore(Component.translatable(loreKey(key), ""))
8787

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

0 commit comments

Comments
 (0)