From 03a953fdd85d1465e422b6ef11ca222d30ac5eaf Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sat, 23 Sep 2023 10:13:46 +0200 Subject: Fixed item rarity problems --- .../at/hannibal2/skyhanni/utils/CachedItemData.kt | 2 + .../java/at/hannibal2/skyhanni/utils/ItemUtils.kt | 46 +++++++++------------- .../at/hannibal2/skyhanni/utils/LorenzRarity.kt | 19 +++++++++ 3 files changed, 40 insertions(+), 27 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt b/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt index b9beea397..8f05d64ec 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt @@ -6,4 +6,6 @@ data class CachedItemData( /** -1 = not loaded */ var sackInASack: Int? = -1, /** null = not loaded */ var riftTransferable: Boolean? = null, /** null = not loaded */ var riftExportable: Boolean? = null, + /** null = not loaded */ var itemRarityLastCheck: Long = 0L, // Cant use SimpleTimeMark here + /** null = not loaded */ var itemRarity: LorenzRarity? = null, ) \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index fd2ca66e1..10fd389d9 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -3,6 +3,8 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.test.command.CopyErrorCommand import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName import at.hannibal2.skyhanni.utils.NEUItems.getItemStack +import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark +import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.cachedData import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRecombobulated import at.hannibal2.skyhanni.utils.StringUtils.matchRegex import at.hannibal2.skyhanni.utils.StringUtils.removeColor @@ -13,6 +15,7 @@ import net.minecraft.init.Items import net.minecraft.item.ItemStack import net.minecraftforge.common.util.Constants import java.util.* +import kotlin.time.Duration.Companion.seconds object ItemUtils { @@ -155,38 +158,27 @@ object ItemUtils { fun ItemStack.getItemRarityOrCommon() = getItemRarityOrNull() ?: LorenzRarity.COMMON - fun ItemStack.getItemRarityOrNull(): LorenzRarity? { + fun ItemStack.getItemRarityOrNull(logError: Boolean = true): LorenzRarity? { + val data = cachedData + if (data.itemRarityLastCheck.asTimeMark().passedSince() < 1.seconds) { + return data.itemRarity + } + data.itemRarityLastCheck = SimpleTimeMark.now().toMillis() + + if (isPet(cleanName())) { return getPetRarity(this) } - val lore = getLore() - var lastLine = lore.lastOrNull() ?: return null - if (lastLine == "§eClick to inspect!") { - // Assuming inside ah browser - val index = lore.indexOfFirst { it.startsWith("§7Seller: ") } - 2 - if (index > 0) { - lastLine = lore[index] - } - } - return when (lastLine.take(4)) { - "§f§l" -> LorenzRarity.COMMON - "§a§l" -> LorenzRarity.UNCOMMON - "§9§l" -> LorenzRarity.RARE - "§5§l" -> LorenzRarity.EPIC - "§6§l" -> LorenzRarity.LEGENDARY - "§d§l" -> LorenzRarity.MYTHIC - "§b§l" -> LorenzRarity.DIVINE - "§4§l" -> LorenzRarity.SUPREME - "§c§l" -> LorenzRarity.SPECIAL - else -> { - CopyErrorCommand.logErrorState( - "Could not read rarity for item $name", - "getItemRarityOrNull not found for: ${getInternalName()}, name:'$name', lastLine:'$lastLine'" - ) - return null - } + val rarity = LorenzRarity.readItemRarity(this) + data.itemRarity = rarity + if (rarity == null && logError) { + CopyErrorCommand.logErrorState( + "Could not read rarity for item $name", + "getItemRarityOrNull not found for: ${getInternalName()}, name:'$name''" + ) } + return rarity } //extra method for shorter name and kotlin nullability logic diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt index 4af8f7e2f..4889bdd04 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt @@ -1,6 +1,8 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.test.command.CopyErrorCommand +import at.hannibal2.skyhanni.utils.ItemUtils.getLore +import net.minecraft.item.ItemStack // TODO: replace id with ordinal @@ -17,6 +19,12 @@ enum class LorenzRarity(val color: LorenzColor, val id: Int) { VERY_SPECIAL(LorenzColor.RED, 9), ; + private val chatColorCode by lazy { color.getChatColor() } + private val normalName by lazy { "$chatColorCode§l$name" } + private val recombName by lazy { "$chatColorCode§l§ka§r $chatColorCode§l$chatColorCode§l$name" } + + //§d§l§ka§r §d§l§d§lMYTHIC + fun oneBelow(logError: Boolean = true): LorenzRarity? { val rarityBelow = getById(ordinal - 1) if (rarityBelow == null && logError) { @@ -41,6 +49,17 @@ enum class LorenzRarity(val color: LorenzColor, val id: Int) { companion object { fun getById(id: Int) = entries.firstOrNull { it.ordinal == id } + + fun readItemRarity(itemStack: ItemStack): LorenzRarity? { + for (line in itemStack.getLore()) { + for (rarity in LorenzRarity.entries) { + if (line.startsWith(rarity.normalName) || line.startsWith(rarity.recombName)) { + return rarity + } + } + } + return null + } } } \ No newline at end of file -- cgit