diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantParser.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantParser.kt index 1c859828b662..e353ea1cddac 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantParser.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantParser.kt @@ -39,6 +39,16 @@ 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) + /** + * REGEX-TEST: §d§l§d§lWisdom V§9, §9Depth Strider III§9, §9Feather Falling X + * REGEX-TEST: §9Compact X§9, §9Efficiency V§9, §9Experience IV + */ + val enchantmentExclusivePattern by patternGroup.pattern( + "exclusive", + "(?:(?:§7§l|§d§l|§9)+([A-Za-z][A-Za-z '-]+) (?:[IVXLCDM]+|[0-9]+)(?:[§r]?§9, |\$| §8\\d{1,3}(?:,\\d{3})*))+\$", + ) + // Above regex tests apply to this pattern also val enchantmentPattern by patternGroup.pattern( "enchants.new", "(§7§l|§d§l|§9)(?[A-Za-z][A-Za-z '-]+) (?[IVXLCDM]+|[0-9]+)(?(§r)?§9, |\$| §8\\d{1,3}(,\\d{3})*)", @@ -186,20 +196,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 = mutableListOf() // Format enchants based on format config option @@ -220,10 +216,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 diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantsJson.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantsJson.kt index 6e1550a974a2..863e019fb4e3 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantsJson.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/EnchantsJson.kt @@ -1,6 +1,5 @@ package at.hannibal2.skyhanni.features.misc.items.enchants -import at.hannibal2.skyhanni.features.misc.items.enchants.EnchantParser.enchantmentPattern import com.google.gson.annotations.Expose import com.google.gson.annotations.SerializedName @@ -27,15 +26,21 @@ class EnchantsJson { } fun containsEnchantment(enchants: Map, line: String): Boolean { - val matcher = enchantmentPattern.matcher(line) + val exclusiveMatch = EnchantParser.enchantmentExclusivePattern.matcher(line) + if (!exclusiveMatch.find()) return false // This is the case that the line is not exclusively enchants + + val matcher = EnchantParser.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 + val key = enchant.loreName.lowercase() + if (normal.containsKey(key) || + ultimate.containsKey(key) || + stacking.containsKey(key) + ) + return true } } return false diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/FormattedEnchant.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/FormattedEnchant.kt index 834959fbba85..11f6e77c2752 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/FormattedEnchant.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/items/enchants/FormattedEnchant.kt @@ -3,14 +3,12 @@ package at.hannibal2.skyhanni.features.misc.items.enchants import at.hannibal2.skyhanni.utils.NumberUtil.toRoman import net.minecraft.item.ItemStack -class FormattedEnchant( +data class FormattedEnchant( private val enchant: Enchant, private val level: Int, - stacking: String, + private val stacking: String, private val isRoman: Boolean, ) : Comparable { - private val stacking: String = stacking - get() = "§8$field" private val loreDescription: MutableList = mutableListOf() fun addLore(lineOfLore: String) = loreDescription.add(lineOfLore) @@ -23,6 +21,6 @@ class FormattedEnchant( val builder = StringBuilder() builder.append(enchant.getFormattedName(level, itemStack)).append(" ").append(if (isRoman) level.toRoman() else level) - return if (!stacking.contains("empty")) builder.append(stacking).toString() else builder.toString() + return if (!stacking.contains("empty")) builder.append("§8$stacking").toString() else builder.toString() } }