Skip to content

Commit

Permalink
feat: convert legacy strings to minimessage format
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Aug 2, 2024
1 parent c5bf788 commit 7512a02
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ interface ServerTranslator {
*/
var defaultLanguage: String

/**
* Format used to parse components.
*/
var format: ServerLanguageFormat

/**
* Registers the translations for translatable components.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TranslatableComponent
import net.kyori.adventure.translation.GlobalTranslator
import net.kyori.adventure.translation.Translator
import su.plo.slib.api.language.ServerLanguageFormat
import su.plo.slib.api.language.ServerTranslator
import java.text.MessageFormat
import java.util.*
Expand All @@ -24,12 +23,6 @@ class AdventureServerTranslator : Translator, ServerTranslator {
serverTranslator.defaultLanguage = value
}

override var format: ServerLanguageFormat
get() = serverTranslator.format
set(value) {
serverTranslator.format = value
}

override fun register(languageName: String, languageMap: Map<String, String>) {
serverTranslator.register(languageName, languageMap)
}
Expand All @@ -44,13 +37,7 @@ class AdventureServerTranslator : Translator, ServerTranslator {
val language = getLanguage(locale.toString())
val translationString = language[component.key()] ?: return null

return when (format) {
ServerLanguageFormat.LEGACY_AMPERSAND ->
LegacyComponentRenderer.renderTranslatable(component, translationString, locale)

ServerLanguageFormat.MINI_MESSAGE ->
MiniMessageComponentRenderer.renderTranslatable(component, translationString, locale)
}
return MiniMessageComponentRenderer.renderTranslatable(component, translationString, locale)
}

override fun translate(key: String, locale: Locale): MessageFormat? {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package su.plo.slib.language

import su.plo.slib.api.language.ServerLanguageFormat
import su.plo.slib.api.language.ServerTranslator

class MapServerTranslator : ServerTranslator {
Expand All @@ -11,8 +10,6 @@ class MapServerTranslator : ServerTranslator {
field = value
}

override var format: ServerLanguageFormat = ServerLanguageFormat.LEGACY_AMPERSAND

private val languages: MutableMap<String, MutableMap<String, String>> = hashMapOf(
"en_us" to HashMap()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,70 @@ object MiniMessageComponentRenderer {
locale: Locale,
renderer: TranslatableComponentRenderer<Locale> = GlobalTranslator.renderer()
): Component =
MiniMessage.miniMessage().deserialize(miniMessageString, ArgumentTagResolver(component.args(), locale, renderer))
MiniMessage.miniMessage().deserialize(
miniMessageString
.let { convertLegacy(it) }
.let { convertArgsToTags(it) },
ArgumentTagResolver(component.args(), locale, renderer)
)
.mergeStyle(component)
.hoverEvent(
component.hoverEvent()?.withRenderedValue(renderer, locale)
)
.children(
component.children().map { renderer.render(it, locale) }
)
.let { c ->
c.children(
c.children() + component.children().map { renderer.render(it, locale) }
)
}
.let { renderer.render(it, locale) }

private fun convertLegacy(miniMessageString: String, prefix: String = "&"): String =
miniMessageString
.replace("${prefix}0", "<black>")
.replace("${prefix}1", "<dark_blue>")
.replace("${prefix}2", "<dark_green>")
.replace("${prefix}3", "<dark_aqua>")
.replace("${prefix}4", "<dark_red>")
.replace("${prefix}5", "<dark_purple>")
.replace("${prefix}6", "<gold>")
.replace("${prefix}7", "<gray>")
.replace("${prefix}8", "<dark_gray>")
.replace("${prefix}9", "<blue>")
.replace("${prefix}a", "<green>")
.replace("${prefix}b", "<aqua>")
.replace("${prefix}c", "<red>")
.replace("${prefix}d", "<light_purple>")
.replace("${prefix}e", "<yellow>")
.replace("${prefix}f", "<white>")

.replace("${prefix}n", "<underlined>")
.replace("${prefix}m", "<strikethrough>")
.replace("${prefix}k", "<obfuscated>")
.replace("${prefix}o", "<italic>")
.replace("${prefix}l", "<bold>")
.replace("${prefix}r", "<reset>")

.replace(Regex("${prefix}#([0-9a-fA-F]{6})")) {
"<#${it.groupValues[1]}>"
}

private val stringFormatRegex = Regex("%(\\d+\\$)?[\\d.]*[a-zA-Z]")

private fun convertArgsToTags(miniMessageString: String): String {
var index = 0

return miniMessageString.replace(stringFormatRegex) { match ->
val position = match.groupValues[1]
.takeIf { it.isNotEmpty() }
?.dropLast(1)
?.toIntOrNull()
?.minus(1)
?: index++

"<argument:$position>"
}
}

class ArgumentTagResolver(
private val arguments: List<Component>,
private val locale: Locale,
Expand Down

0 comments on commit 7512a02

Please sign in to comment.