speedup legacy text parsing with urls

This commit is contained in:
Bixilon 2021-07-30 21:11:08 +02:00
parent 0e81029432
commit a9ab04b41f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 13 additions and 10 deletions

View File

@ -60,6 +60,7 @@ class BaseComponent : ChatComponent {
val spaceSplit = currentText.split(' ') val spaceSplit = currentText.split(' ')
for ((index, split) in spaceSplit.withIndex()) { for ((index, split) in spaceSplit.withIndex()) {
var clickEvent: ClickEvent? = null var clickEvent: ClickEvent? = null
if (split.isNotBlank()) {
for (protocol in URLProtocols.VALUES) { for (protocol in URLProtocols.VALUES) {
if (!split.startsWith(protocol.prefix)) { if (!split.startsWith(protocol.prefix)) {
continue continue
@ -70,7 +71,10 @@ class BaseComponent : ChatComponent {
clickEvent = ClickEvent(ClickEvent.ClickEventActions.OPEN_URL, split) clickEvent = ClickEvent(ClickEvent.ClickEventActions.OPEN_URL, split)
break break
} }
}
if (split.isNotEmpty()) {
parts += TextComponent(message = split, color = currentColor, formatting = currentFormatting.toMutableSet(), clickEvent = clickEvent) parts += TextComponent(message = split, color = currentColor, formatting = currentFormatting.toMutableSet(), clickEvent = clickEvent)
}
if (index != spaceSplit.size - 1) { if (index != spaceSplit.size - 1) {
parts += TextComponent(message = " ", color = currentColor, formatting = currentFormatting.toMutableSet()) parts += TextComponent(message = " ", color = currentColor, formatting = currentFormatting.toMutableSet())
} }
@ -81,7 +85,6 @@ class BaseComponent : ChatComponent {
} }
while (char != CharacterIterator.DONE) { while (char != CharacterIterator.DONE) {
// ToDo: Parse urls with click event (and respect restrictedMode)
if (char != ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR) { if (char != ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR) {
currentText.append(char) currentText.append(char)
char = iterator.next() char = iterator.next()

View File

@ -104,7 +104,7 @@ interface ChatComponent {
} }
} }
return BaseComponent(parent, string) return BaseComponent(parent, string, restrictedMode)
} }
fun String.chat(): ChatComponent { fun String.chat(): ChatComponent {