diff options
| author | Mikecraft1224 <85994411+Mikecraft1224@users.noreply.github.com> | 2024-05-08 21:12:47 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-08 21:12:47 +0200 |
| commit | bd117b2d181a6abb419021f815e4ad07cc05a37f (patch) | |
| tree | 4beae077201fd4edfc75ad7df73a5ae95edaf8b0 /src/main/java/at/hannibal2/skyhanni/utils | |
| parent | f3bad6b63b3c4efbf3088d58b9f83944949ba367 (diff) | |
| download | skyhanni-bd117b2d181a6abb419021f815e4ad07cc05a37f.tar.gz skyhanni-bd117b2d181a6abb419021f815e4ad07cc05a37f.tar.bz2 skyhanni-bd117b2d181a6abb419021f815e4ad07cc05a37f.zip | |
Feature: Replace Roman Numerals with Arabic Numerals (#1722)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index c52927291..c048004f2 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -1,11 +1,14 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.hypixel.chat.event.SystemMessageEvent import at.hannibal2.skyhanni.mixins.transformers.AccessorChatComponentText import at.hannibal2.skyhanni.utils.GuiRenderUtils.darkenColor import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiUtilRenderComponents +import net.minecraft.event.ClickEvent +import net.minecraft.event.HoverEvent import net.minecraft.util.ChatComponentText import net.minecraft.util.ChatStyle import net.minecraft.util.EnumChatFormatting @@ -411,6 +414,42 @@ object StringUtils { } } + /** + * Applies a transformation on the message of a SystemMessageEvent if possible. + */ + fun SystemMessageEvent.applyIfPossible(transform: (String) -> String) { + val original = chatComponent.formattedText + val new = transform(original) + if (new == original) return + + val clickEvents = mutableListOf<ClickEvent>() + val hoverEvents = mutableListOf<HoverEvent>() + chatComponent.findAllEvents(clickEvents, hoverEvents) + + if (clickEvents.size > 1 || hoverEvents.size > 1) return + + chatComponent = ChatComponentText(new) + if (clickEvents.size == 1) chatComponent.chatStyle.chatClickEvent = clickEvents.first() + if (hoverEvents.size == 1) chatComponent.chatStyle.chatHoverEvent = hoverEvents.first() + } + + private fun IChatComponent.findAllEvents( + clickEvents: MutableList<ClickEvent>, + hoverEvents: MutableList<HoverEvent> + ) { + siblings.forEach { it.findAllEvents(clickEvents, hoverEvents) } + + val clickEvent = chatStyle.chatClickEvent + val hoverEvent = chatStyle.chatHoverEvent + + if (clickEvent?.action != null && clickEvents.none { it.value == clickEvent.value }) { + clickEvents.add(clickEvent) + } + if (hoverEvent?.action != null && hoverEvents.none { it.value == hoverEvent.value }) { + hoverEvents.add(hoverEvent) + } + } + fun String.replaceAll(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { var text = this while (true) { |
