From 0a95a791eb57a546fb5421594abae740faf1dfdb Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Wed, 6 Mar 2024 23:51:02 +0100 Subject: Internal Changes: Many small code cleanups (#1101) Co-authored-by: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../java/at/hannibal2/skyhanni/utils/APIUtil.kt | 7 +++++- .../at/hannibal2/skyhanni/utils/ItemCategory.kt | 1 + .../java/at/hannibal2/skyhanni/utils/ItemUtils.kt | 25 ++++++++++++++-------- .../at/hannibal2/skyhanni/utils/LorenzUtils.kt | 3 --- .../java/at/hannibal2/skyhanni/utils/NEUItems.kt | 8 +++---- .../at/hannibal2/skyhanni/utils/SimpleTimeMark.kt | 1 + .../skyhanni/utils/SkyBlockItemModifierUtils.kt | 2 +- .../java/at/hannibal2/skyhanni/utils/TimeUtils.kt | 7 ++++-- .../at/hannibal2/skyhanni/utils/UtilsPatterns.kt | 6 ++++++ 9 files changed, 40 insertions(+), 20 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt index fd1c4030b..7e35d02b1 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt @@ -121,7 +121,12 @@ object APIUtil { } val message = "POST request to '$urlString' returned status ${status.statusCode}" - ChatUtils.error("SkyHanni ran into an error. Status: ${status.statusCode}") + ErrorManager.logErrorStateWithData( + "Error communicating with API", "APIUtil POST request returned an error code", + "statusCode" to status.statusCode, + "urlString" to urlString, + "body" to body, + ) return ApiResponse(false, message, JsonObject()) } } catch (throwable: Throwable) { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt index 975a409c3..4a16585f1 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemCategory.kt @@ -43,6 +43,7 @@ enum class ItemCategory { ITEM, PET_ITEM, ENCHANTED_BOOK, + FISHING_BAIT, POTION, RIFT_TIMECHARM, COSMETIC, diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index 30e8adcee..196103e2d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -4,7 +4,7 @@ import at.hannibal2.skyhanni.data.PetAPI import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull -import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber +import at.hannibal2.skyhanni.utils.NumberUtil.formatInt import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.cachedData import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getEnchantments @@ -203,7 +203,6 @@ object ItemUtils { fun ItemStack.getItemRarityOrCommon() = getItemRarityOrNull() ?: LorenzRarity.COMMON private fun ItemStack.readItemCategoryAndRarity(): Pair { - val name = this.name ?: "" val cleanName = this.cleanName() if (PetAPI.hasPetName(cleanName)) { @@ -250,6 +249,7 @@ object ItemUtils { if (itemCategory.isEmpty()) when { UtilsPatterns.abiPhonePattern.matches(name) -> ItemCategory.ABIPHONE PetAPI.hasPetName(cleanName) -> ItemCategory.PET + UtilsPatterns.baitPattern.matches(cleanName) -> ItemCategory.FISHING_BAIT UtilsPatterns.enchantedBookPattern.matches(name) -> ItemCategory.ENCHANTED_BOOK UtilsPatterns.potionPattern.matches(name) -> ItemCategory.POTION UtilsPatterns.sackPattern.matches(name) -> ItemCategory.SACK @@ -291,17 +291,24 @@ object ItemUtils { private fun itemRarityLastCheck(data: CachedItemData) = data.itemRarityLastCheck.asTimeMark().passedSince() > 10.seconds - // use when compaaring the name (e.g. regex), not for showing to the user - // extra method for shorter name and kotlin nullability logic - var ItemStack.name: String? - get() = this.displayName + /** + * Use when comparing the name (e.g. regex), not for showing to the user + * Member that provides the item name, is null save or throws visual error + */ + var ItemStack.name: String + get() = this.displayName ?: ErrorManager.skyHanniError( + "Could not get name if ItemStack", + "itemStack" to this, + "displayName" to displayName, + "internal name" to getInternalNameOrNull(), + ) set(value) { setStackDisplayName(value) } - @Deprecated("outdated", ReplaceWith("itemName")) + @Deprecated("outdated", ReplaceWith("this.itemName")) val ItemStack.nameWithEnchantment: String? - get() = getInternalNameOrNull()?.itemName + get() = itemName fun isSkyBlockMenuItem(stack: ItemStack?): Boolean = stack?.getInternalName()?.equals("SKYBLOCK_MENU") ?: false @@ -341,7 +348,7 @@ object ItemUtils { private fun makePair(input: String, itemName: String, matcher: Matcher): Pair { val matcherAmount = matcher.group("amount") - val amount = matcherAmount?.formatNumber()?.toInt() ?: 1 + val amount = matcherAmount?.formatInt() ?: 1 val pair = Pair(itemName, amount) itemAmountCache[input] = pair return pair diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index 60a466b98..80fca6178 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -305,9 +305,6 @@ object LorenzUtils { TitleManager.sendTitle(text, duration, height, fontSize) } - @Deprecated("Dont use this approach at all. check with regex or equals instead.", ReplaceWith("Regex or equals")) - fun Iterable.anyContains(element: String) = any { it.contains(element) } - inline fun > enumValueOfOrNull(name: String): T? { val enums = enumValues() return enums.firstOrNull { it.name == name } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index 12acfb2ff..67f01b531 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -63,7 +63,7 @@ object NEUItems { allItemsCache = readAllNeuItems() } - @Deprecated("Use NEUInternalName rather than String", ReplaceWith("getInternalNameFromItemName()")) + @Deprecated("Use NEUInternalName rather than String", ReplaceWith("NEUInternalName.fromItemName(itemName)")) fun getRawInternalName(itemName: String): String = NEUInternalName.fromItemName(itemName).asString() fun readAllNeuItems(): Map { @@ -111,7 +111,7 @@ object NEUItems { if (result != -1.0) return result if (equals("JACK_O_LANTERN")) { - return getPrice("PUMPKIN", useSellingPrice) + 1 + return "PUMPKIN".asInternalName().getPrice(useSellingPrice) + 1 } if (equals("GOLDEN_CARROT")) { // 6.8 for some players @@ -131,7 +131,7 @@ object NEUItems { fun getItemStackOrNull(internalName: String) = internalName.asInternalName().getItemStackOrNull() // TODO remove - @Deprecated("Use NEUInternalName rather than String", ReplaceWith("getItemStack()")) + @Deprecated("Use NEUInternalName rather than String", ReplaceWith("internalName.asInternalName().getItemStack()")) fun getItemStack(internalName: String): ItemStack = internalName.asInternalName().getItemStack() @@ -268,7 +268,7 @@ object NEUItems { return result } - @Deprecated("Do not use strings as id", ReplaceWith("getMultiplier with NEUInternalName")) + @Deprecated("Do not use strings as id", ReplaceWith("NEUItems.getMultiplier(internalName.asInternalName())")) fun getMultiplier(internalName: String, tryCount: Int = 0): Pair { val pair = getMultiplier(internalName.asInternalName(), tryCount) return Pair(pair.first.asString(), pair.second) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt index 728a9f4ba..621f2fd5e 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt @@ -39,6 +39,7 @@ value class SimpleTimeMark(private val millis: Long) : Comparable "pitchin_koi" - it == "warped" && name!!.removeColor().startsWith("Hyper ") -> "endstone_geode" + it == "warped" && name.removeColor().startsWith("Hyper ") -> "endstone_geode" else -> it } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt index df34ec70e..d2b269e9a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt @@ -30,7 +30,10 @@ object TimeUtils { else -> default } - @Deprecated("Has an offset of one second", ReplaceWith("use kotlin Duration")) + @Deprecated( + "Has an offset of one second", + ReplaceWith("millis.toDuration(DurationUnit.MILLISECONDS).format(biggestUnit, showMilliSeconds, longName, maxUnits)") + ) fun formatDuration( millis: Long, biggestUnit: TimeUnit = TimeUnit.YEAR, @@ -78,7 +81,7 @@ object TimeUtils { return builder.toString().trim() } - @Deprecated("Do no longer use long for time", ReplaceWith("getDuration(string)")) + @Deprecated("Do no longer use long for time", ReplaceWith("TimeUtils.getDuration(string)")) fun getMillis(string: String) = getDuration(string).inWholeMilliseconds fun getDuration(string: String) = getMillis_(string.replace("m", "m ").replace(" ", " ").trim()) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt b/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt index ae7c24783..42ff1a06d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt @@ -32,6 +32,12 @@ object UtilsPatterns { "item.name.enchanted.book", ".{2}?Enchanted Book" ) + + val baitPattern by patternGroup.pattern( + "item.name.bait", + "^(Obfuscated.*|.* Bait)$" + ) + val enchantmentNamePattern by patternGroup.pattern( "item.neuitems.enchantmentname", "^(?(?:§.)+)(?[^§]+) (?[IVXL]+)$" -- cgit