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 | |
parent | f3bad6b63b3c4efbf3088d58b9f83944949ba367 (diff) | |
download | skyhanni-bd117b2d181a6abb419021f815e4ad07cc05a37f.tar.gz skyhanni-bd117b2d181a6abb419021f815e4ad07cc05a37f.tar.bz2 skyhanni-bd117b2d181a6abb419021f815e4ad07cc05a37f.zip |
Feature: Replace Roman Numerals with Arabic Numerals (#1722)
5 files changed, 113 insertions, 22 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index d65d1dc82..63b638586 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -345,6 +345,7 @@ import at.hannibal2.skyhanni.features.misc.PetItemDisplay import at.hannibal2.skyhanni.features.misc.PocketSackInASackDisplay import at.hannibal2.skyhanni.features.misc.PrivateIslandNoPickaxeAbility import at.hannibal2.skyhanni.features.misc.QuickModMenuSwitch +import at.hannibal2.skyhanni.features.misc.ReplaceRomanNumerals import at.hannibal2.skyhanni.features.misc.RestorePieceOfWizardPortalLore import at.hannibal2.skyhanni.features.misc.ServerRestartTitle import at.hannibal2.skyhanni.features.misc.SkyBlockKickDuration @@ -726,6 +727,7 @@ class SkyHanniMod { loadModule(TpsCounter()) loadModule(ParticleHider()) loadModule(MiscFeatures()) + loadModule(ReplaceRomanNumerals()) loadModule(GardenPlotMenuHighlighting()) loadModule(SkyMartCopperPrice()) loadModule(GardenVisitorFeatures) diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java index df5b8904c..5e9a82e03 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java @@ -252,6 +252,12 @@ public class MiscConfig { @FeatureToggle public boolean fixGhostEntities = true; + @Expose + @ConfigOption(name = "Replace Roman Numerals", desc = "Replaces Roman Numerals with Arabic Numerals on any item.") + @ConfigEditorBoolean + @FeatureToggle + public boolean replaceRomanNumerals = false; + @ConfigOption(name = "Hide Far Entities", desc = "") @Accordion @Expose diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt index e2a4f779c..52e95b3a1 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt @@ -4,6 +4,7 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.data.hypixel.chat.event.SystemMessageEvent import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager +import at.hannibal2.skyhanni.utils.StringUtils.applyIfPossible import net.minecraft.event.ClickEvent import net.minecraft.event.HoverEvent import net.minecraft.util.ChatComponentText @@ -22,28 +23,7 @@ class PlayerChatModifier { @SubscribeEvent fun onChat(event: SystemMessageEvent) { - val original = event.chatComponent.formattedText - val new = cutMessage(original) - if (new == original) return - - val clickEvents = mutableListOf<ClickEvent>() - val hoverEvents = mutableListOf<HoverEvent>() - findClickableTexts(event.chatComponent, clickEvents) - findHoverTexts(event.chatComponent, hoverEvents) - val clickSize = clickEvents.size - val hoverSize = hoverEvents.size - - // do not change the message if more than one hover or click is found - if (clickSize > 1 || hoverSize > 1) return - - val text = ChatComponentText(new) - if (clickSize == 1) { - text.chatStyle.chatClickEvent = clickEvents.first() - } - if (hoverSize == 1) { - text.chatStyle.chatHoverEvent = hoverEvents.first() - } - event.chatComponent = text + event.applyIfPossible { cutMessage(it) } } private fun findClickableTexts(chatComponent: IChatComponent, clickEvents: MutableList<ClickEvent>) { 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 +} 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) { |