Skip to content

Commit 2e55175

Browse files
committed
Merge branch 'add-language-file-warnings' into merge-waila-entries-with-items
2 parents 342c1a1 + 1e39948 commit 2e55175

4 files changed

Lines changed: 44 additions & 21 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class PhantomBlock(
5151

5252
override fun getWaila(player: Player): WailaConfig {
5353
return WailaConfig(
54-
key,
54+
text = name,
5555
placeholders = mapOf("block" to Component.text(erroredBlockKey.toString())),
5656
color = BossBar.Color.RED
5757
)

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.github.pylonmc.pylon.core.config.Config
88
import io.github.pylonmc.pylon.core.config.Settings
99
import io.github.pylonmc.pylon.core.datatypes.PylonSerializers
1010
import io.github.pylonmc.pylon.core.i18n.AddonTranslator
11+
import io.github.pylonmc.pylon.core.item.PylonItemSchema
1112
import io.github.pylonmc.pylon.core.registry.PylonRegistry
1213
import io.github.pylonmc.pylon.core.util.position.BlockPosition
1314
import io.github.pylonmc.pylon.core.util.position.position
@@ -37,7 +38,7 @@ open class PylonBlock protected constructor(val block: Block) {
3738
constructor(block: Block, pdc: PersistentDataContainer) : this(block)
3839

3940
open fun getWaila(player: Player): WailaConfig {
40-
return WailaConfig(key)
41+
return WailaConfig(name)
4142
}
4243

4344
open fun getItem(context: BlockItemContext): ItemStack? {
@@ -64,7 +65,9 @@ open class PylonBlock protected constructor(val block: Block) {
6465
private val pylonBlockPositionKey = pylonKey("position")
6566
private val pylonBlockErrorKey = pylonKey("error")
6667

67-
fun register(schema: PylonBlockSchema) {
68+
private val wailaWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()
69+
70+
private fun checkWaila(schema: PylonBlockSchema) {
6871
val translator = AddonTranslator.translators[schema.addon]
6972
check(translator != null) {
7073
"Addon does not have a translator; did you forget to call registerWithPylon()?"
@@ -76,13 +79,16 @@ open class PylonBlock protected constructor(val block: Block) {
7679
PylonCore.logger.warning("Block ${schema.key} is missing a WAILA translation key (${locale.displayName} | $wailaKey")
7780
}
7881
}
79-
80-
PylonRegistry.BLOCKS.register(schema)
8182
}
8283

8384
@JvmStatic
84-
fun register(key: NamespacedKey, material: Material, blockClass: Class<out PylonBlock>)
85-
= register(PylonBlockSchema(key, material, blockClass))
85+
fun register(key: NamespacedKey, material: Material, blockClass: Class<out PylonBlock>) {
86+
val schema = PylonBlockSchema(key, material, blockClass)
87+
if (!wailaWarningsSupressed.contains(key)) {
88+
checkWaila(schema)
89+
}
90+
PylonRegistry.BLOCKS.register(schema)
91+
}
8692

8793
@JvmSynthetic
8894
internal fun serialize(
@@ -152,5 +158,10 @@ open class PylonBlock protected constructor(val block: Block) {
152158
}
153159
}
154160
}
161+
162+
@JvmStatic
163+
fun supressWailaWarnings(key: NamespacedKey) {
164+
wailaWarningsSupressed.add(key)
165+
}
155166
}
156167
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package io.github.pylonmc.pylon.core.block.waila
33
import io.github.pylonmc.pylon.core.i18n.PlaceholderAttacher
44
import net.kyori.adventure.bossbar.BossBar
55
import net.kyori.adventure.text.Component
6-
import org.bukkit.NamespacedKey
76
import org.bukkit.entity.Player
87

98
data class WailaConfig @JvmOverloads constructor(
10-
val key: NamespacedKey,
9+
val text: Component,
1110
val placeholders: Map<String, Component> = emptyMap(),
1211
val color: BossBar.Color = BossBar.Color.WHITE,
1312
val style: BossBar.Overlay = BossBar.Overlay.PROGRESS,
@@ -18,9 +17,9 @@ data class WailaConfig @JvmOverloads constructor(
1817
val player = bar.viewers().singleOrNull() as? Player
1918
if (player != null) {
2019
val attacher = PlaceholderAttacher(placeholders)
21-
bar.name(attacher.render(Component.translatable("pylon.${key.namespace}.item.${key.key}.waila"), Unit))
20+
bar.name(attacher.render(text, Unit))
2221
} else {
23-
bar.name(Component.translatable("pylon.${key.namespace}.item.${key.key}.waila"))
22+
bar.name(text)
2423
}
2524
bar.color(color)
2625
bar.overlay(style)

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import io.github.pylonmc.pylon.core.datatypes.PylonSerializers
88
import io.github.pylonmc.pylon.core.i18n.AddonTranslator
99
import io.github.pylonmc.pylon.core.item.builder.ItemStackBuilder
1010
import io.github.pylonmc.pylon.core.registry.PylonRegistry
11+
import io.papermc.paper.datacomponent.DataComponentTypes
1112
import net.kyori.adventure.text.ComponentLike
12-
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
13+
import net.kyori.adventure.text.TranslatableComponent
1314
import org.bukkit.Keyed
1415
import org.bukkit.NamespacedKey
1516
import org.bukkit.block.Block
@@ -44,38 +45,45 @@ open class PylonItem(val stack: ItemStack) : Keyed {
4445

4546
companion object {
4647

47-
private fun register(schema: PylonItemSchema) {
48+
private val nameAndLoreWarningsSupressed: MutableSet<NamespacedKey> = mutableSetOf()
49+
50+
private fun checkNameAndLore(schema: PylonItemSchema) {
4851
val translator = AddonTranslator.translators[schema.addon]
4952
check(translator != null) {
5053
"Addon does not have a translator; did you forget to call registerWithPylon()?"
5154
}
5255

5356
// Adventure is a perfect API with absolutely no problems whatsoever.
54-
val name = PlainTextComponentSerializer.plainText().serialize(schema.itemStack.effectiveName())
55-
val lore = schema.itemStack.lore()?.get(0)?.let { PlainTextComponentSerializer.plainText().serialize(it) }
57+
val name = schema.itemStack.getData(DataComponentTypes.ITEM_NAME) as? TranslatableComponent
58+
val lore = schema.itemStack.getData(DataComponentTypes.LORE)?.lines()?.get(0) as? TranslatableComponent
5659

5760
var isNameAndLoreValid = true
58-
if (name != ItemStackBuilder.nameKey(schema.key)) {
61+
if (name == null || name.key() != ItemStackBuilder.nameKey(schema.key)) {
5962
PylonCore.logger.warning("Item ${schema.key}'s name is not a translation key; check your item uses ItemStackBuilder.pylonItem(...)")
6063
isNameAndLoreValid = false
6164
}
6265

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

6871
if (isNameAndLoreValid) {
6972
for (locale in schema.addon.languages) {
70-
if (!translator.translationKeyExists(name, locale)) {
71-
PylonCore.logger.warning("Item ${schema.key} is missing a name translation key (${locale.displayName} | ${ItemStackBuilder.nameKey(schema.key)}")
73+
if (!translator.translationKeyExists(name!!.key(), locale)) {
74+
PylonCore.logger.warning("${schema.key.namespace} is missing a name translation key for item ${schema.key} (locale: ${locale.displayName} | expected translation key: ${ItemStackBuilder.nameKey(schema.key)}")
7275
}
73-
if (!translator.translationKeyExists(lore!!, locale)) {
74-
PylonCore.logger.warning("Item ${schema.key} is missing a lore translation key (${locale.displayName} | ${ItemStackBuilder.loreKey(schema.key)}")
76+
if (!translator.translationKeyExists(lore!!.key(), locale)) {
77+
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)}")
7578
}
7679
}
7780
}
81+
}
7882

83+
private fun register(schema: PylonItemSchema) {
84+
if (!nameAndLoreWarningsSupressed.contains(schema.key)) {
85+
checkNameAndLore(schema)
86+
}
7987
PylonRegistry.ITEMS.register(schema)
8088
}
8189

@@ -106,5 +114,10 @@ open class PylonItem(val stack: ItemStack) : Keyed {
106114
fun isPylonitem(stack: ItemStack?): Boolean {
107115
return stack != null && stack.persistentDataContainer.has(PylonItemSchema.pylonItemKeyKey)
108116
}
117+
118+
@JvmStatic
119+
fun supressNameAndLoreWarnings(key: NamespacedKey) {
120+
nameAndLoreWarningsSupressed.add(key)
121+
}
109122
}
110123
}

0 commit comments

Comments
 (0)