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/features/misc | |
| 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/features/misc')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/misc/ReplaceRomanNumerals.kt | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/ReplaceRomanNumerals.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/ReplaceRomanNumerals.kt new file mode 100644 index 000000000..3a93588b6 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/ReplaceRomanNumerals.kt @@ -0,0 +1,64 @@ +package at.hannibal2.skyhanni.features.misc + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.hypixel.chat.event.SystemMessageEvent +import at.hannibal2.skyhanni.events.ChatHoverEvent +import at.hannibal2.skyhanni.events.LorenzToolTipEvent +import at.hannibal2.skyhanni.mixins.hooks.GuiChatHook +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal +import at.hannibal2.skyhanni.utils.StringUtils.applyIfPossible +import at.hannibal2.skyhanni.utils.StringUtils.isRoman +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import net.minecraft.event.ClickEvent +import net.minecraft.event.HoverEvent +import net.minecraft.util.ChatComponentText +import net.minecraft.util.IChatComponent +import net.minecraftforge.fml.common.eventhandler.EventPriority +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class ReplaceRomanNumerals { + // Using toRegex here since toPattern doesn't seem to provide the necessary functionality + private val splitRegex = "((ยง\\w)|(\\s+)|(\\W))+|(\\w*)".toRegex() + + @SubscribeEvent(priority = EventPriority.LOWEST) + fun onTooltip(event: LorenzToolTipEvent) { + if (!isEnabled()) return + + event.toolTip.replaceAll { it.transformLine() } + } + + @SubscribeEvent(priority = EventPriority.LOWEST) + fun onChatHover(event: ChatHoverEvent) { + if (event.getHoverEvent().action != HoverEvent.Action.SHOW_TEXT) return + if (!isEnabled()) return + + val lore = event.getHoverEvent().value.formattedText.split("\n").toMutableList() + lore.replaceAll { it.transformLine() } + + val chatComponentText = ChatComponentText(lore.joinToString("\n")) + val hoverEvent = HoverEvent(event.component.chatStyle.chatHoverEvent.action, chatComponentText) + + GuiChatHook.replaceOnlyHoverEvent(hoverEvent) + } + + @SubscribeEvent + fun onSystemMessage(event: SystemMessageEvent) { + if (!isEnabled()) return + event.applyIfPossible { it.transformLine() } + } + + private fun String.transformLine() = splitRegex.findAll(this).map { it.value }.joinToString("") { + it.takeIf { it.isValidRomanNumeral() }?.coloredRomanToDecimal() ?: it + } + + private fun String.removeFormatting() = removeColor().replace(",", "") + + private fun String.isValidRomanNumeral() = removeFormatting() + .let { it.isRoman() && it.isNotEmpty() } + + private fun String.coloredRomanToDecimal() = removeFormatting() + .let { replace(it, it.romanToDecimal().toString()) } + + private fun isEnabled() = LorenzUtils.inSkyBlock && SkyHanniMod.feature.misc.replaceRomanNumerals +} |
