Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.github.pylonmc.rebar.guide.pages.settings

import io.github.pylonmc.rebar.config.RebarConfig
import io.github.pylonmc.rebar.guide.button.PageButton
import io.github.pylonmc.rebar.guide.button.setting.TogglePlayerSettingButton
import io.github.pylonmc.rebar.content.guide.RebarGuide.Companion.guideHints
import io.github.pylonmc.rebar.content.guide.RebarGuide.Companion.guideSounds
import io.github.pylonmc.rebar.guide.button.PageButton
import io.github.pylonmc.rebar.guide.button.setting.TogglePlayerSettingButton
import io.github.pylonmc.rebar.i18n.RebarTranslator.Companion.storyText
import io.github.pylonmc.rebar.item.research.Research.Companion.researchConfetti
import io.github.pylonmc.rebar.item.research.Research.Companion.researchSounds
import io.github.pylonmc.rebar.util.rebarKey
Expand Down Expand Up @@ -49,6 +50,13 @@ object MainSettingsPage : PlayerSettingsPage(rebarKey("settings")) {
isEnabled = { player -> player.guideSounds }
)

@JvmStatic
val storyTextButton = TogglePlayerSettingButton(
rebarKey("toggle-story-text"),
toggle = { player -> player.storyText = !player.storyText },
isEnabled = { player -> player.storyText }
)

init {
if (RebarConfig.WailaConfig.ENABLED) {
addSetting(wailaSettingsButton)
Expand All @@ -67,5 +75,6 @@ object MainSettingsPage : PlayerSettingsPage(rebarKey("settings")) {

addSetting(guideHintsButton)
addSetting(guideSoundsButton)
addSetting(storyTextButton)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ open class PlayerSettingsPage(key: NamespacedKey) : SimpleStaticGuidePage(key, m
"# b # # # # # s #",
"# # # # # # # # #",
"# x x x x x x x #",
"# x x x x x x x #",
"# # # # # # # # #",
)
.addIngredient('#', GuiItems.background())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlayerTranslationHandler internal constructor(private val player: Player)
val rebarFluid = RebarFluid.fromStack(stack)
val placeholders = rebarItem?.getPlaceholders().orEmpty()

stack.translate(player.locale(), placeholders)
stack.translate(player, placeholders)

if ((rebarItem != null || rebarFluid != null) && !stack.persistentDataContainer.has(FOOTER_APPENDED)) {
stack.editData(DataComponentTypes.LORE) { lore ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import io.github.pylonmc.rebar.datatypes.RebarSerializers
import io.github.pylonmc.rebar.event.RebarRegisterEvent
import io.github.pylonmc.rebar.event.RebarUnregisterEvent
import io.github.pylonmc.rebar.i18n.RebarTranslator.Companion.translator
import io.github.pylonmc.rebar.item.RebarItemSchema
import io.github.pylonmc.rebar.item.builder.ItemStackBuilder
import io.github.pylonmc.rebar.nms.NmsAccessor
import io.github.pylonmc.rebar.registry.RebarRegistry
import io.github.pylonmc.rebar.util.editData
import io.github.pylonmc.rebar.util.mergeResource
import io.github.pylonmc.rebar.util.persistentData
import io.github.pylonmc.rebar.util.plainText
import io.github.pylonmc.rebar.util.rebarKey
import io.github.pylonmc.rebar.util.withArguments
Expand All @@ -28,6 +30,7 @@ import net.kyori.adventure.translation.Translator
import org.apache.commons.lang3.LocaleUtils
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
Expand Down Expand Up @@ -147,20 +150,25 @@ class RebarTranslator private constructor(private val addon: RebarAddon) : Trans

private val loreType = RebarSerializers.LIST.listTypeFrom(RebarSerializers.COMPONENT)

private val storyTextKey = rebarKey("story_text")

@JvmStatic
var Player.storyText: Boolean by persistentData(storyTextKey, RebarSerializers.BOOLEAN, true)

@JvmStatic
@get:JvmName("getTranslatorForAddon")
val RebarAddon.translator: RebarTranslator
get() = translators[this.key]
?: error("Addon does not have a translator; did you forget to call registerWithRebar()?")

/**
* Modifies the [ItemStack] to translate its name and lore into the specified [locale].
* Modifies the [ItemStack] to translate its name and lore into the locale of the specified [player].
*/
@JvmStatic
@JvmOverloads
@JvmName("translateItem")
@Suppress("UnstableApiUsage")
fun ItemStack.translate(locale: Locale, arguments: List<RebarArgument> = emptyList()) {
fun ItemStack.translate(player: Player, arguments: List<RebarArgument> = emptyList()) {
fun isRebarOrAddon(component: Component): Boolean {
if (component is TranslatableComponent) {
for (addon in RebarRegistry.ADDONS) {
Expand All @@ -172,6 +180,8 @@ class RebarTranslator private constructor(private val addon: RebarAddon) : Trans
return component.children().any(::isRebarOrAddon)
}

val locale = player.locale()

editData(DataComponentTypes.ITEM_NAME) {
if (!isRebarOrAddon(it)) return@editData it

Expand Down Expand Up @@ -221,7 +231,20 @@ class RebarTranslator private constructor(private val addon: RebarAddon) : Trans
if (result.style().color() == null) result.color(NamedTextColor.WHITE) else result
}
editData(DataComponentTypes.LORE) { lore ->
val originalLore = lore.lines()
val originalLore = lore.lines().toMutableList()

// Add story text to lore if this is a Rebar item with story text and player has story text enabled
val rebarItemSchema = RebarItemSchema.fromStack(this)
if (rebarItemSchema != null && player.storyText) {
val storyKey = "${rebarItemSchema.key.namespace}.item.${rebarItemSchema.key.key}.story"
if (translators[rebarItemSchema.addon.key]!!.canTranslate(storyKey, locale)) {
if (originalLore.isNotEmpty()) {
originalLore.add(Component.empty()) // newline
}
originalLore.add(Component.translatable(storyKey, null as String?))
}
}

val newLore = originalLore.flatMap { line ->
if (!isRebarOrAddon(line)) return@flatMap listOf(line)
val concatenatedArguments: MutableList<TranslationArgumentLike> = arguments.toMutableList()
Expand All @@ -240,6 +263,9 @@ class RebarTranslator private constructor(private val addon: RebarAddon) : Trans
}

editPersistentDataContainer { pdc -> pdc.set(originalLoreKey, loreType, originalLore) }
check(newLore.size <= 256) {
"Lore for item had too many lines ($locale) (256 lines max but had ${newLore.size}): ${newLore.map { it.plainText }}\\n"
}
ItemLore.lore(newLore)
}
}
Expand Down
9 changes: 9 additions & 0 deletions rebar/src/main/resources/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ guide:
name: "<white>Guide Sounds: <red>Disabled <dark_gray>(Click to toggle)"
lore: |-
<guidearrow> Whether to play sounds in the guide
toggle-story-text:
enabled:
name: "<white>Story text: <green>Enabled <dark_gray>(Click to toggle)"
lore: |-
<guidearrow> Whether to show <story>story text</story>
disabled:
name: "<white>Story text: <red>Disabled <dark_gray>(Click to toggle)"
lore: |-
<guidearrow> Whether to show <story>story text</story>
info:
discord:
name: "<white>Discord"
Expand Down
Loading