Skip to content

Commit

Permalink
remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
CalMWolfs committed Nov 22, 2024
1 parent ad19a27 commit 3cb7b84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 3 additions & 21 deletions src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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;(?<tier>.*)".toPattern()

Expand Down

0 comments on commit 3cb7b84

Please sign in to comment.