-
Notifications
You must be signed in to change notification settings - Fork 17
Full MiniMessage support #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Replace direct references to the '§' and '&' chars with named constants to improve code readability and maintainability within the MiniMessageUtils class
| } | ||
|
|
||
| @SuppressWarnings("all") | ||
| private static String convertLegacyToMiniMessage(String legacy, boolean concise, char charCode, boolean rgb) { |
There was a problem hiding this comment.
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)) // 尾处理
}There was a problem hiding this comment.
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 可以不要
|
Add Minimessage example: click:run_command:'/help'test click |
This PR brings full support of MiniMessage to AuthMe.
Current TODOs: