Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: Better Enchant Detection and Error Logging #2816

Merged
merged 7 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ object EnchantParser {
private val config get() = SkyHanniMod.feature.inventory.enchantParsing

val patternGroup = RepoPattern.group("misc.items.enchantparsing")
// Pattern to check that the line contains ONLY enchants (and the other bits that come with a valid enchant line)
val enchantmentExclusivePattern by patternGroup.pattern(
VixidDev marked this conversation as resolved.
Show resolved Hide resolved
"exclusive",
"(?:(?:§7§l|§d§l|§9)+([A-Za-z][A-Za-z '-]+) (?:[IVXLCDM]+|[0-9]+)(?:[§r]?§9, |\$| §8\\d{1,3}(?:,\\d{3})*))+\$",
)
val enchantmentPattern by patternGroup.pattern(
"enchants.new",
"(§7§l|§d§l|§9)(?<enchant>[A-Za-z][A-Za-z '-]+) (?<levelNumeral>[IVXLCDM]+|[0-9]+)(?<stacking>(§r)?§9, |\$| §8\\d{1,3}(,\\d{3})*)",
Expand Down Expand Up @@ -186,20 +191,6 @@ object EnchantParser {
return
}

// Remove enchantment lines so we can insert ours
try {
loreList.subList(startEnchant, endEnchant + 1).clear()
} catch (e: IndexOutOfBoundsException) {
ErrorManager.logErrorWithData(
e,
"Error parsing enchantment info from item",
"loreList" to loreList,
"startEnchant" to startEnchant,
"endEnchant" to endEnchant,
)
return
}

val insertEnchants: MutableList<String> = mutableListOf()

// Format enchants based on format config option
Expand All @@ -220,10 +211,24 @@ object EnchantParser {
"ConcurrentModificationException whilst formatting enchants",
"loreList" to loreList,
"format" to config.format.get(),
"orderedEnchants" to orderedEnchants
"orderedEnchants" to orderedEnchants.toString()
)
}

// Remove enchantment lines so we can insert ours
try {
loreList.subList(startEnchant, endEnchant + 1).clear()
} catch (e: IndexOutOfBoundsException) {
ErrorManager.logErrorWithData(
e,
"Error parsing enchantment info from item",
"loreList" to loreList,
"startEnchant" to startEnchant,
"endEnchant" to endEnchant,
)
return
}

// Add our parsed enchants back into the lore
loreList.addAll(startEnchant, insertEnchants)
// Cache parsed lore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.hannibal2.skyhanni.features.misc.items.enchants

import at.hannibal2.skyhanni.features.misc.items.enchants.EnchantParser.enchantmentExclusivePattern
import at.hannibal2.skyhanni.features.misc.items.enchants.EnchantParser.enchantmentPattern
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
Expand Down Expand Up @@ -27,15 +28,20 @@ class EnchantsJson {
}

fun containsEnchantment(enchants: Map<String, Int>, line: String): Boolean {
val exclusiveMatch = enchantmentExclusivePattern.matcher(line)
if (!exclusiveMatch.find()) return false // This is the case that the line is not exclusively enchants

val matcher = enchantmentPattern.matcher(line)
while (matcher.find()) {
val enchant = this.getFromLore(matcher.group("enchant"))
if (enchants.isNotEmpty()) {
if (enchants.containsKey(enchant.nbtName)) return true
} else {
if (normal.containsKey(enchant.loreName.lowercase())) return true
if (ultimate.containsKey(enchant.loreName.lowercase())) return true
if (stacking.containsKey(enchant.loreName.lowercase())) return true
if (normal.containsKey(enchant.loreName.lowercase()) ||
ultimate.containsKey(enchant.loreName.lowercase()) ||
stacking.containsKey(enchant.loreName.lowercase())
)
return true
}
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ class FormattedEnchant(

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

override fun toString(): String {
return "FormattedEnchant[enchant: " + enchant.nbtName + ", level: " + level + ", isRoman: " + isRoman + "]"
}
}
Loading