diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt | 102 |
1 files changed, 78 insertions, 24 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index 38e1c40c0..3bc79dbf2 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -8,6 +8,7 @@ import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.cachedData import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getEnchantments import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRecombobulated +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.matches import at.hannibal2.skyhanni.utils.StringUtils.removeColor import com.google.gson.GsonBuilder @@ -205,42 +206,95 @@ object ItemUtils { fun ItemStack.getItemRarityOrCommon() = getItemRarityOrNull() ?: LorenzRarity.COMMON - fun ItemStack.getItemRarityOrNull(logError: Boolean = true): LorenzRarity? { - val data = cachedData - if (data.itemRarityLastCheck.asTimeMark().passedSince() < 1.seconds) { - return data.itemRarity + private fun ItemStack.readItemCategoryAndRarity(): Pair<LorenzRarity?, ItemCategory?> { + val name = this.name ?: "" + val cleanName = this.cleanName() + + if (isPet(cleanName)) { + return getPetRarity(this) to ItemCategory.PET } - data.itemRarityLastCheck = SimpleTimeMark.now().toMillis() + for (line in this.getLore().reversed()) { + val (category, rarity) = UtilsPatterns.rarityLoreLinePattern.matchMatcher(line) { + group("itemCategory").replace(" ", "_") to + group("rarity").replace(" ", "_") + } ?: continue + + val itemCategory = getItemCategory(category, name, cleanName) + val itemRarity = LorenzRarity.getByName(rarity) + + if (itemCategory == null) { + ErrorManager.logErrorStateWithData( + "Could not read category for item $name", + "Failed to read category from item rarity via item lore", + "internal name" to getInternalName(), + "item name" to name, + "inventory name" to InventoryUtils.openInventoryName(), + "pattern result" to category, + "lore" to getLore(), + ) + } + if (itemRarity == null) { + ErrorManager.logErrorStateWithData( + "Could not read rarity for item $name", + "Failed to read rarity from item rarity via item lore", + "internal name" to getInternalName(), + "item name" to name, + "inventory name" to InventoryUtils.openInventoryName(), + "lore" to getLore(), + ) + } + + return itemRarity to itemCategory + } + return null to null + } + + private fun getItemCategory(itemCategory: String, name: String, cleanName: String = name.removeColor()) = + if (itemCategory.isEmpty()) when { + UtilsPatterns.abiPhonePattern.matches(name) -> ItemCategory.ABIPHONE + isPet(cleanName) -> ItemCategory.PET + UtilsPatterns.enchantedBookPattern.matches(name) -> ItemCategory.ENCHANTED_BOOK + UtilsPatterns.potionPattern.matches(name) -> ItemCategory.POTION + else -> ItemCategory.NONE + } else { + LorenzUtils.enumValueOfOrNull<ItemCategory>(itemCategory) + } + + private fun ItemStack.updateCategoryAndRarity() { + val data = cachedData + data.itemRarityLastCheck = SimpleTimeMark.now().toMillis() val internalName = getInternalName() if (internalName == NEUInternalName.NONE) { data.itemRarity = null - return null + data.itemCategory = null + return } + val pair = this.readItemCategoryAndRarity() + data.itemRarity = pair.first + data.itemCategory = pair.second + } - - if (isPet(cleanName())) { - val petRarity = getPetRarity(this) - data.itemRarity = petRarity - return petRarity + fun ItemStack.getItemCategoryOrNull(): ItemCategory? { + val data = cachedData + if (itemRarityLastCheck(data)) { + this.updateCategoryAndRarity() } + return data.itemCategory + } - val rarity = LorenzRarity.readItemRarity(this) - data.itemRarity = rarity - if (rarity == null && logError) { - ErrorManager.logErrorStateWithData( - "Could not read rarity for item $name", - "Failed to read rarity from item rarity via item lore", - "internal name" to internalName, - "item name" to name, - "inventory name" to InventoryUtils.openInventoryName(), - "lore" to getLore(), - ) + fun ItemStack.getItemRarityOrNull(): LorenzRarity? { + val data = cachedData + if (itemRarityLastCheck(data)) { + this.updateCategoryAndRarity() } - return rarity + return data.itemRarity } - //extra method for shorter name and kotlin nullability logic + private fun itemRarityLastCheck(data: CachedItemData) = + data.itemRarityLastCheck.asTimeMark().passedSince() > 10.seconds + + // extra method for shorter name and kotlin nullability logic var ItemStack.name: String? get() = this.displayName set(value) { |