aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt2
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt51
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt102
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt28
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt9
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt38
6 files changed, 180 insertions, 50 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt b/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt
index 9d00958f2..e22f1cba5 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/CachedItemData.kt
@@ -21,4 +21,6 @@ data class CachedItemData(
// null = not loaded
var itemRarity: LorenzRarity? = null,
+
+ var itemCategory: ItemCategory? = null,
)
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt
new file mode 100644
index 000000000..fa8003ca5
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt
@@ -0,0 +1,51 @@
+package at.hannibal2.skyhanni.utils
+
+enum class ItemCategory {
+ SWORD,
+ LONGSWORD,
+ BOW,
+ SHORT_BOW,
+ WAND,
+ FISHING_WEAPON,
+ FISHING_ROD,
+ AXE,
+ GAUNTLET,
+ HOE,
+ PICKAXE,
+ SHOVEL,
+ DRILL,
+ SHEARS,
+ DEPLOYABLE,
+ VACUUM,
+ ABIPHONE,
+ BELT,
+ NECKLACE,
+ CLOAK,
+ GLOVES,
+ BRACELET,
+ HELMET,
+ CHESTPLATE,
+ LEGGINGS,
+ BOOTS,
+ HATCESSORY,
+ ACCESSORY,
+ POWER_STONE,
+ TRAVEL_SCROLL,
+ REFORGE_STONE,
+ BAIT,
+ PET,
+ TROPHY_FISH,
+ ARROW,
+ ARROW_POISON,
+ ITEM,
+ PET_ITEM,
+ ENCHANTED_BOOK,
+ POTION,
+ RIFT_TIMECHARM,
+ COSMETIC,
+ MEMENTO,
+ PORTAL,
+
+ NONE,
+ ;
+}
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) {
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt
index 64d5f8538..75412b83f 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzRarity.kt
@@ -1,12 +1,10 @@
package at.hannibal2.skyhanni.utils
import at.hannibal2.skyhanni.test.command.ErrorManager
-import at.hannibal2.skyhanni.utils.ItemUtils.getLore
-import net.minecraft.item.ItemStack
-
// TODO: replace id with ordinal
enum class LorenzRarity(val color: LorenzColor, val id: Int) {
+
COMMON(LorenzColor.WHITE, 0),
UNCOMMON(LorenzColor.GREEN, 1),
RARE(LorenzColor.BLUE, 2),
@@ -17,15 +15,11 @@ enum class LorenzRarity(val color: LorenzColor, val id: Int) {
SUPREME(LorenzColor.DARK_RED, 7),
SPECIAL(LorenzColor.RED, 8),
VERY_SPECIAL(LorenzColor.RED, 9),
- ULTIMATE_COSMETIC(LorenzColor.DARK_RED, 10),
+ ULTIMATE(LorenzColor.DARK_RED, 10),
;
val chatColorCode by lazy { color.getChatColor() }
- private val rawName by lazy { name.replace("_", " ") }
- private val normalName by lazy { "$chatColorCode§l$rawName" }
- private val recombName by lazy { "$chatColorCode§l§ka§r $chatColorCode§l$chatColorCode§l$rawName" }
-
- //§d§l§ka§r §d§l§d§lMYTHIC
+ val rawName = name.replace("_", " ")
fun oneBelow(logError: Boolean = true): LorenzRarity? {
val rarityBelow = getById(ordinal - 1)
@@ -54,20 +48,8 @@ enum class LorenzRarity(val color: LorenzColor, val id: Int) {
fun isAtLeast(other: LorenzRarity): Boolean = this.ordinal >= other.ordinal
companion object {
- fun getById(id: Int) = entries.firstOrNull { it.ordinal == id }
- fun getByName(name: String) = entries.firstOrNull { it.name == name }
- fun readItemRarity(itemStack: ItemStack): LorenzRarity? {
- for (line in itemStack.getLore()) {
- val string = line.replace("SHINY ", "")
- for (rarity in LorenzRarity.entries) {
- if (string.startsWith(rarity.normalName) || string.startsWith(rarity.recombName)) {
- return rarity
- }
- }
- }
- return null
- }
+ fun getById(id: Int) = if (entries.size > id) entries[id] else null
+ fun getByName(name: String) = entries.firstOrNull { it.name == name }
}
-
}
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
index 9e8fc6b8e..572aa147f 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
@@ -265,7 +265,7 @@ object LorenzUtils {
fun getSBMonthByName(month: String): Int {
var monthNr = 0
- for (i in 1..12) {
+ for (i in 1 .. 12) {
val monthName = SkyBlockTime.monthName(i)
if (month == monthName) {
monthNr = i
@@ -352,7 +352,7 @@ object LorenzUtils {
hover: List<String>,
command: String? = null,
prefix: Boolean = true,
- prefixColor: String = "§e"
+ prefixColor: String = "§e",
) {
val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else ""
val text = ChatComponentText(msgPrefix + message)
@@ -452,7 +452,7 @@ object LorenzUtils {
prefix: String,
getName: (T) -> String,
isCurrent: (T) -> Boolean,
- crossinline onChange: (T) -> Unit
+ crossinline onChange: (T) -> Unit,
) = buildList {
add(prefix)
for (entry in enumValues<T>()) {
@@ -672,6 +672,9 @@ object LorenzUtils {
enumValueOfOrNull<T>(name)
?: kotlin.error("Unknown enum constant for ${enumValues<T>().first().name.javaClass.simpleName}: '$name'")
+ inline fun <reified T : Enum<T>> enumJoinToPattern(noinline transform: (T) -> CharSequence = { it.name }) =
+ enumValues<T>().joinToString("|", transform = transform)
+
fun isInDevEnviromen() = Launch.blackboard["fml.deobfuscatedEnvironment"] as Boolean
fun shutdownMinecraft(reason: String? = null) {
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt b/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt
new file mode 100644
index 000000000..fdaa65050
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt
@@ -0,0 +1,38 @@
+package at.hannibal2.skyhanni.utils
+
+import at.hannibal2.skyhanni.utils.LorenzUtils.enumJoinToPattern
+import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
+
+object UtilsPatterns {
+
+ private val patternGroup = RepoPattern.group("utils")
+
+ /** Examples:
+ §d§l§ka§r §d§l§d§lMYTHIC ACCESSORY §d§l§ka
+ §d§l§ka§r §d§l§d§lSHINY MYTHIC DUNGEON CHESTPLATE §d§l§ka
+ §c§l§ka§r §c§l§c§lVERY SPECIAL HATCESSORY §c§l§ka
+ §6§lSHINY LEGENDARY DUNGEON BOOTS
+ §6§lLEGENDARY DUNGEON BOOTS
+ §5§lEPIC BOOTS
+ §f§lCOMMON
+ **/
+ val rarityLoreLinePattern by patternGroup.pattern(
+ "item.lore.rarity.line",
+ "^(?:§.){2,3}(?:.§. (?:§.){4})?(?:SHINY )?(?<rarity>${enumJoinToPattern<LorenzRarity> { it.name.replace("_", " ") }}) ?(?:DUNGEON )?(?<itemCategory>[^§]*)(?: (?:§.){3}.)?$"
+ )
+
+ val abiPhonePattern by patternGroup.pattern(
+ "item.name.abiphone",
+ ".{2}Abiphone .*"
+ )
+
+ val enchantedBookPattern by patternGroup.pattern(
+ "item.name.enchanted.book",
+ ".{2}?Enchanted Book"
+ )
+
+ val potionPattern by patternGroup.pattern(
+ "item.name.potion",
+ ".*Potion"
+ )
+}