forked from AuthMe/AuthMeReloaded
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Full MiniMessage support #184
Draft
HaHaWTH
wants to merge
8
commits into
master
Choose a base branch
from
feat/minimessage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 可以不要
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR brings full support of MiniMessage to AuthMe.
Current TODOs: