From 3cb7b8471a35c9fcb67cbf25241cd5515efec8f6 Mon Sep 17 00:00:00 2001 From: Cal Date: Fri, 22 Nov 2024 21:46:12 +1100 Subject: [PATCH] remove duplicated code --- .../api/enoughupdates/ItemResolutionQuery.kt | 39 +++++++++---------- .../skyhanni/utils/ItemNameResolver.kt | 24 ++---------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/api/enoughupdates/ItemResolutionQuery.kt b/src/main/java/at/hannibal2/skyhanni/api/enoughupdates/ItemResolutionQuery.kt index a7b23cbf0132..5df62a73b34a 100644 --- a/src/main/java/at/hannibal2/skyhanni/api/enoughupdates/ItemResolutionQuery.kt +++ b/src/main/java/at/hannibal2/skyhanni/api/enoughupdates/ItemResolutionQuery.kt @@ -7,8 +7,10 @@ import at.hannibal2.skyhanni.utils.ItemUtils import at.hannibal2.skyhanni.utils.ItemUtils.extraAttributes import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal +import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.cleanString import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import at.hannibal2.skyhanni.utils.UtilsPatterns import com.google.gson.JsonObject import net.minecraft.client.Minecraft import net.minecraft.client.gui.Gui @@ -30,7 +32,6 @@ class ItemResolutionQuery { private var guiContext: Gui? = null companion object { - val enchantedBookNamePattern = "^((?:§.)*)([^§]+) ([IVXL]+)$".toPattern() private val petPattern = ".*(\\[Lvl .*] )§(.).*".toPattern() val petRarities = listOf("COMMON", "UNCOMMON", "RARE", "EPIC", "LEGENDARY", "MYTHIC") @@ -116,6 +117,22 @@ class ItemResolutionQuery { } return candidates } + + fun resolveEnchantmentByName(displayName: String): String? = + UtilsPatterns.enchantmentNamePattern.matchMatcher(displayName) { + val name = group("name").trim { it <= ' ' } + val ultimate = group("format").lowercase().contains("§l") + val prefix = if (ultimate && name != "Ultimate Wise" && name != "Ultimate Jerry") "ULTIMATE_" else "" + val cleanedEnchantName = turboCheck(name).replace(" ", "_").replace("-", "_").uppercase() + "$prefix$cleanedEnchantName;${group("level").romanToDecimal()}".uppercase() + } + + private fun turboCheck(text: String): String { + if (text == "Turbo-Cocoa") return "Turbo-Coco" + if (text == "Turbo-Cacti") return "Turbo-Cactus" + + return text + } } fun withItemNbt(compound: NBTTagCompound): ItemResolutionQuery { @@ -237,26 +254,6 @@ class ItemResolutionQuery { return "BALLOON_HAT_2024_" + color.uppercase() } - private fun resolveEnchantmentByName(displayName: String): String? { - val matcher = enchantedBookNamePattern.matcher(displayName) - if (!matcher.matches()) return null - val format = matcher.group(1).lowercase() - val enchantmentName = matcher.group(2).trim() - val romanLevel = matcher.group(3) - val ultimate = (format.contains("§l")) - - val prefix = if (ultimate && enchantmentName != "Ultimate Wise" && enchantmentName != "Ultimate Jerry") "ULTIMATE_" else "" - val cleanedEnchantName = turboCheck(enchantmentName).replace(" ", "_").replace("-", "_").uppercase() - return "$prefix$cleanedEnchantName;${romanLevel.romanToDecimal()}".uppercase() - } - - private fun turboCheck(text: String): String { - if (text == "Turbo-Cocoa") return "Turbo-Coco" - if (text == "Turbo-Cacti") return "Turbo-Cactus" - - return text - } - private fun resolveItemInCatacombsRngMeter(): String? { val lore = compound.getLore() if (lore.size > 16) { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt index 4a1522610246..0d438a683c6b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt @@ -3,7 +3,6 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.api.enoughupdates.ItemResolutionQuery import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.toInternalName import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull -import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.allLettersFirstUppercase @@ -26,12 +25,14 @@ object ItemNameResolver { return itemNameCache.getOrPut(lowercase) { NEUInternalName.MISSING_ITEM } } - resolveEnchantmentByName(itemName)?.let { + ItemResolutionQuery.resolveEnchantmentByName(itemName)?.let { return itemNameCache.getOrPut(lowercase) { fixEnchantmentName(it) } } + resolveEnchantmentByCleanName(itemName)?.let { return itemNameCache.getOrPut(lowercase) { it } } + if (itemName.endsWith("gemstone", ignoreCase = true)) { val split = lowercase.split(" ") if (split.size == 3) { @@ -94,25 +95,6 @@ object ItemNameResolver { return null } - // does not work without color codes, or with roman numbers - // Taken and edited from NEU - private fun resolveEnchantmentByName(enchantmentName: String) = - UtilsPatterns.enchantmentNamePattern.matchMatcher(enchantmentName) { - val name = group("name").trim { it <= ' ' } - val ultimate = group("format").lowercase().contains("§l") - ( - (if (ultimate && name != "Ultimate Wise" && name != "Ultimate Jerry") "ULTIMATE_" else "") + - turboCheck(name).replace(" ", "_").replace("-", "_").uppercase() + - ";" + group("level").romanToDecimal() - ) - } - - private fun turboCheck(text: String): String { - if (text == "Turbo-Cocoa") return "Turbo-Coco" - if (text == "Turbo-Cacti") return "Turbo-Cactus" - return text - } - // Workaround for duplex private val duplexPattern = "ULTIMATE_DUPLEX;(?.*)".toPattern()