-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
ReplaceRomanNumerals.kt
138 lines (114 loc) · 5.62 KB
/
ReplaceRomanNumerals.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package at.hannibal2.skyhanni.features.misc
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.data.hypixel.chat.event.SystemMessageEvent
import at.hannibal2.skyhanni.events.ChatHoverEvent
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.mixins.hooks.GuiChatHook
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal
import at.hannibal2.skyhanni.utils.RecalculatingValue
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.applyIfPossible
import at.hannibal2.skyhanni.utils.StringUtils.isRoman
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.event.HoverEvent
import net.minecraft.util.ChatComponentText
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds
@SkyHanniModule
object ReplaceRomanNumerals {
// Using toRegex here since toPattern doesn't seem to provide the necessary functionality
private val splitRegex = "((§\\w)|(\\s+)|(\\W))+|(\\w*)".toRegex()
private val cachedStrings = TimeLimitedCache<String, String>(5.seconds)
private val patternGroup = RepoPattern.group("replace.roman.numerals")
@Suppress("MaxLineLength")
private val allowedPatterns by patternGroup.list(
"allowed.patterns",
"§o§a(?:Combat|Farming|Fishing|Mining|Foraging|Enchanting|Alchemy|Carpentry|Runecrafting|Taming|Social|)( Level)? (?<roman>[IVXLCDM]+)§r",
"(?:§5§o)?§7Progress to (?:Collection|Level|Tier|Floor|Milestone|Chocolate Factory) (?<roman>[IVXLCDM]+): §.(?:.*)%",
"§5§o §e(?:\\w+) (?<roman>[IVXLCDM]+)",
"(?:§.)*Abiphone (?<roman>[IVXLCDM]+) .*",
"§o§a§a(?:§c§lMM§c )?The Catacombs §8- §eFloor (?<roman>[IVXLCDM]+)§r",
".*Extra Farming Fortune (?<roman>[IVXLCDM]+)",
".*(?:Collection|Level|Tier|Floor|Milestone) (?<roman>[IVXLCDM]+)(?: ?§(?:7|r).*)?",
"(?:§5§o§a ✔|§5§o§c ✖) §.* (?<roman>[IVXLCDM]+)",
"§o§a✔ §.* (?<roman>[IVXLCDM]+)§r",
"§5§o§7Purchase §a.* (?<roman>[IVXLCDM]+) §7.*",
"§5§o(?:§7)§.(?<roman>[IVXLCDM]+).*",
".*Heart of the Mountain (?<roman>[IVXLCDM]+) ?.*"
)
/**
* REGEX-TEST: §eSelect an option: §r§a[§aOk, then what?§a]
*/
private val isSelectOptionPattern by patternGroup.pattern(
"string.isselectoption",
"§eSelect an option: .*",
)
// TODO: Remove after pr 1717 is ready and switch to ItemHoverEvent
@SubscribeEvent(priority = EventPriority.LOWEST)
fun onTooltip(event: LorenzToolTipEvent) {
if (!isEnabled()) return
event.toolTip.replaceAll { it.tryReplace() }
}
@HandleEvent(priority = HandleEvent.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.tryReplace() }
val chatComponentText = ChatComponentText(lore.joinToString("\n"))
val hoverEvent = HoverEvent(event.component.chatStyle.chatHoverEvent?.action, chatComponentText)
GuiChatHook.replaceOnlyHoverEvent(hoverEvent)
}
@HandleEvent
fun onSystemMessage(event: SystemMessageEvent) {
if (!isEnabled() || event.message.isSelectOption()) return
event.applyIfPossible { it.tryReplace() }
}
@SubscribeEvent(priority = EventPriority.LOW)
fun onRepoReload(event: RepositoryReloadEvent) {
cachedStrings.clear()
}
private fun String.isSelectOption(): Boolean = isSelectOptionPattern.matches(this)
private fun String.tryReplace(): String = cachedStrings.getOrPut(this) {
if (allowedPatterns.matches(this)) replace() else this
}
fun replaceLine(line: String, checkIfEnabled: Boolean = true): String {
if (checkIfEnabled && !isEnabled()) return line
return cachedStrings.getOrPut(line) {
line.replace()
}
}
private fun String.replace() = splitRegex.findAll(this).map { it.value }.joinToString("") {
it.takeIf { it.isValidRomanNumeral() && it.removeFormatting().romanToDecimal() != 2000 }?.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.get()
init {
RecalculatingValue
}
@HandleEvent
fun onDebug(event: DebugDataCollectEvent) {
event.title("Replace Roman Numerals")
event.addIrrelevant {
val map = cachedStrings.toMap()
add("cachedStrings: (${map.size})")
for ((original, changed) in map) {
if (original == changed) {
add("unchanged: '$original'")
} else {
add("'$original' -> '$changed'")
}
}
}
}
}