Skip to content

Commit

Permalink
Improvement: Enchant Parsing SBA Convert Roman Numerals and NEU Inven…
Browse files Browse the repository at this point in the history
…tories compat (hannibal002#1717)

Co-authored-by: Cal <cwolfson58@gmail.com>
  • Loading branch information
2 people authored and MTOnline69 committed Sep 10, 2024
1 parent 4335562 commit 5f67252
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package at.hannibal2.skyhanni.features.misc.items.enchants

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.features.inventory.EnchantParsingConfig
import at.hannibal2.skyhanni.config.features.inventory.EnchantParsingConfig.CommaFormat
import at.hannibal2.skyhanni.events.ChatHoverEvent
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.events.item.ItemHoverEvent
import at.hannibal2.skyhanni.features.chroma.ChromaManager
import at.hannibal2.skyhanni.mixins.hooks.GuiChatHook
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
Expand Down Expand Up @@ -38,7 +39,7 @@ object EnchantParser {

val patternGroup = RepoPattern.group("misc.items.enchantparsing")
val enchantmentPattern by patternGroup.pattern(
"enchants", "(?<enchant>[A-Za-z][A-Za-z '-]+) (?<levelNumeral>[IVXLCDM]+)(?<stacking>, |\$| \\d{1,3}(,\\d{3})*)"
"enchants", "(§9§d§l|§d§l§d§l|§9)(?<enchant>[A-Za-z][A-Za-z '-]+) (?<levelNumeral>[IVXLCDM]+|[0-9]+)(?<stacking>§9, |\$| §8\\d{1,3}(,\\d{3})*)"
)
private val grayEnchantPattern by patternGroup.pattern(
"grayenchants", "^(Respiration|Aqua Affinity|Depth Strider|Efficiency).*"
Expand Down Expand Up @@ -91,10 +92,10 @@ object EnchantParser {
}
}

@SubscribeEvent
fun onTooltipEvent(event: LorenzToolTipEvent) {
@HandleEvent(onlyOnSkyblock = true)
fun onTooltipEvent(event: ItemHoverEvent) {
// If enchants doesn't have any enchant data then we have no data to parse enchants correctly
if (!isEnabled() || !this.enchants.hasEnchantData()) return
if (!this.enchants.hasEnchantData()) return

currentItem = event.itemStack

Expand Down Expand Up @@ -220,7 +221,7 @@ object EnchantParser {
val strippedLine = loreList[i].removeColor()

if (startEnchant == -1) {
if (this.enchants.containsEnchantment(enchants, strippedLine)) startEnchant = i
if (this.enchants.containsEnchantment(enchants, loreList[i])) startEnchant = i
} else if (strippedLine.trim().isEmpty() && endEnchant == -1) endEnchant = i - 1
}

Expand All @@ -232,22 +233,27 @@ object EnchantParser {
var lastEnchant: FormattedEnchant? = null

for (i in startEnchant..endEnchant) {
val unformattedLine = loreList[i].removeColor()
val matcher = enchantmentPattern.matcher(unformattedLine)
val matcher = enchantmentPattern.matcher(loreList[i])
var containsEnchant = false
var enchantsOnThisLine = 0
var isRoman = true

while (matcher.find()) {
// Pull enchant, enchant level and stacking amount if applicable
val enchant = this.enchants.getFromLore(matcher.group("enchant"))
val level = matcher.group("levelNumeral").romanToDecimal()
val stacking = if (matcher.group("stacking").trimStart().matches("[\\d,]+\$".toRegex())) {
val level = try {
// If one enchant is not a roman numeral we assume all are not roman numerals (idk a situation where this wouldn't be the case)
matcher.group("levelNumeral").toInt().also { isRoman = false }
} catch (e: NumberFormatException) {
matcher.group("levelNumeral").romanToDecimal()
}
val stacking = if (matcher.group("stacking").trimStart().removeColor().matches("[\\d,]+\$".toRegex())) {
shouldBeSingleColumn = true
matcher.group("stacking")
} else "empty"

// Last found enchant
lastEnchant = FormattedEnchant(enchant, level, stacking)
lastEnchant = FormattedEnchant(enchant, level, stacking, isRoman)

if (!orderedEnchants.add(lastEnchant)) {
for (e: FormattedEnchant in orderedEnchants) {
Expand Down Expand Up @@ -275,10 +281,10 @@ object EnchantParser {
// Normal is leaving the formatting as Hypixel provides it
if (config.format.get() == EnchantParsingConfig.EnchantFormat.NORMAL) {
normalFormatting(insertEnchants)
// Compressed is always forcing 3 enchants per line, except when there is stacking enchant progress visible
// Compressed is always forcing 3 enchants per line, except when there is stacking enchant progress visible
} else if (config.format.get() == EnchantParsingConfig.EnchantFormat.COMPRESSED && !shouldBeSingleColumn) {
compressedFormatting(insertEnchants)
// Stacked is always forcing 1 enchant per line
// Stacked is always forcing 1 enchant per line
} else {
stackedFormatting(insertEnchants)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class FormattedEnchant(
private val enchant: Enchant,
private val level: Int,
stacking: String,
private val isRoman: Boolean
) : Comparable<FormattedEnchant> {
private val stacking: String = stacking
get() = "§8$field"
Expand All @@ -19,7 +20,7 @@ class FormattedEnchant(

fun getFormattedString(): String {
val builder = StringBuilder()
builder.append(enchant.getFormattedName(level)).append(" ").append(level.toRoman())
builder.append(enchant.getFormattedName(level)).append(" ").append(if (isRoman) level.toRoman() else level)

return if (!stacking.contains("empty")) builder.append(stacking).toString() else builder.toString()
}
Expand Down

0 comments on commit 5f67252

Please sign in to comment.