Skip to content

Conversation

@HaHaWTH
Copy link
Owner

@HaHaWTH HaHaWTH commented Jul 22, 2024

This PR brings full support of MiniMessage to AuthMe.

Current TODOs:

  • Gradient working
  • HoverEvents working
  • Legacy format char compatibility
  • Version compatibility (from 1.8.8 to latest) (Tested versions: 1.12.2, 1.21)
  • Support for kick message
  • Support for deny login message
  • Support for chat message

HaHaWTH added 3 commits July 22, 2024 14:29
Replace direct references to the '§' and '&' chars with named constants
to improve code readability and maintainability within the MiniMessageUtils
class
@HaHaWTH HaHaWTH added the enhancement New feature or request label Jul 22, 2024
@HaHaWTH HaHaWTH self-assigned this Jul 22, 2024
@HaHaWTH HaHaWTH marked this pull request as draft July 22, 2024 20:19
@HaHaWTH HaHaWTH linked an issue Jul 27, 2024 that may be closed by this pull request
}

@SuppressWarnings("all")
private static String convertLegacyToMiniMessage(String legacy, boolean concise, char charCode, boolean rgb) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here offers a more effective mothod (Written in Kotlin):

import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.AMPERSAND_CHAR
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.SECTION_CHAR

fun convertLegacyToMiniMessage(source: String, vararg tagResolver: TagResolver): Component =
    LegacyComponentSerializer.deserialize(translateAmpersandColor(mark(source)))
            .let { MiniMessageSerializer.serialize(it) }
            .let { MiniMessageSerializer.deserialize(deMark(it), *tagResolver) }

private fun mark(source: String) =
   source.replaceNonEscaped("<", "{marked:start}").replaceNonEscaped(">", "{marked:end}")

private fun deMark(source: String) =
    source.replaceNonEscaped("{marked:start}", "<").replaceNonEscaped("{marked:end}", ">")

/**
 * 将 '&' 转换成 '§'
 */
fun translateAmpersandColor(target: String) = target.replace(AMPERSAND_CHAR, SECTION_CHAR)

/**
 * 替换非转义字符
 * @see String.replace
 */
@JvmOverloads
fun String.replaceNonEscaped(
    oldValue: String,
    newValue: String,
    ignoreCase: Boolean = false,
    startIndex: Int = 0,
    escapeChar: String = ESCAPE_CHAR,
): String = buildString {
    // 索引记录
    var lastIndex = 0
    // 匹配字符串
    allIndexOf(oldValue, startIndex, ignoreCase) { index ->
        // 从上次找到的位置到当前找到的位置之前的字符串
        val segment = this@replaceNonEscaped.substring(lastIndex, index)
        // 检查转义字符串
        if (this@replaceNonEscaped.startsWith(escapeChar, index - escapeChar.length))
            append(segment.dropLast(escapeChar.length)).append(oldValue)
        else append(segment).append(newValue)
        // 更新索引
        lastIndex = index + oldValue.length
    }
    append(this@replaceNonEscaped.substring(lastIndex)) // 尾处理
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

具体逻辑:

  • 提前替换 "<" ">",因为直接用LegacyComponentSerializer的话 "<" ">" 会被转义,为避免转义对其预先处理,替换成过渡字符
  • 此时已经得到了已经通过旧版解析的Componet,用MiniMessageSerializer再将其序列化成字符串,相当于说,把旧版的形式转换成了MiniMessage的形式
  • 然后将过渡字符还原成 "<" ">",再交给MiniMessageSerializer,就得到最终成品了

ps: 宽松点的话,replaceNonEscaped 可以不要

@STASynBOSS
Copy link

Add Minimessage example: click:run_command:'/help'test click
Pls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support for minimessage

4 participants