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,17 +60,21 @@ 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
for (protocol in URLProtocols.VALUES) { if (split.isNotBlank()) {
if (!split.startsWith(protocol.prefix)) { for (protocol in URLProtocols.VALUES) {
continue if (!split.startsWith(protocol.prefix)) {
} continue
if (protocol.restricted && restrictedMode) { }
if (protocol.restricted && restrictedMode) {
break
}
clickEvent = ClickEvent(ClickEvent.ClickEventActions.OPEN_URL, split)
break break
} }
clickEvent = ClickEvent(ClickEvent.ClickEventActions.OPEN_URL, split)
break
} }
parts += TextComponent(message = split, color = currentColor, formatting = currentFormatting.toMutableSet(), clickEvent = clickEvent) if (split.isNotEmpty()) {
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 {