forked from hannibal002/SkyHanni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a feature to replace Roman Numerals in tooltips
- Loading branch information
1 parent
a87b85a
commit 1aa6167
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/at/hannibal2/skyhanni/features/misc/ReplaceRomanNumerals.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package at.hannibal2.skyhanni.features.misc | ||
|
||
import at.hannibal2.skyhanni.SkyHanniMod | ||
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.isRoman | ||
import at.hannibal2.skyhanni.utils.StringUtils.removeColor | ||
import net.minecraft.event.HoverEvent | ||
import net.minecraft.util.ChatComponentText | ||
import net.minecraftforge.fml.common.eventhandler.EventPriority | ||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent | ||
|
||
class ReplaceRomanNumerals { | ||
@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) | ||
} | ||
|
||
private fun String.transformLine() = split(" ").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 | ||
} |