From 56cb5ef4697acdb2acb9ff1bcbc0e63043b46b92 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Fri, 23 Feb 2024 07:43:57 +1100 Subject: Make future npc price fetch errors better debuggable. #1036 --- .../skyhanni/features/bazaar/BazaarDataHolder.kt | 31 +++++++++++++--------- .../java/at/hannibal2/skyhanni/utils/JsonUtils.kt | 24 +++++++++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt b/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt index 5a4f956a9..44ce4b481 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.features.bazaar import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.features.rift.RiftAPI +import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.ItemUtils.name @@ -10,6 +11,8 @@ import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull import at.hannibal2.skyhanni.utils.NEUItems.getPrice import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import at.hannibal2.skyhanni.utils.getDoubleOrNull +import at.hannibal2.skyhanni.utils.getStringOrNull import kotlinx.coroutines.launch import kotlin.concurrent.fixedRateTimer @@ -25,25 +28,29 @@ class BazaarDataHolder { private fun loadNpcPrices(): MutableMap { val list = mutableMapOf() + val itemsData = APIUtil.getJSONResponse("https://api.hypixel.net/v2/resources/skyblock/items") + try { - val itemsData = APIUtil.getJSONResponse("https://api.hypixel.net/v2/resources/skyblock/items") val motesPrice = mutableMapOf() - for (element in itemsData["items"].asJsonArray) { - val jsonObject = element.asJsonObject - val hypixelId = jsonObject["id"].asString - jsonObject["npc_sell_price"]?.let { - val neuItemId = NEUItems.transHypixelNameToInternalName(hypixelId) - list[neuItemId] = it.asDouble + for (item in itemsData.getAsJsonArray("items")) { + val itemData = item.asJsonObject + + val hypixelId = itemData.getStringOrNull("id") ?: continue + val neuItemId = NEUItems.transHypixelNameToInternalName(hypixelId) + + itemData.getDoubleOrNull("npc_sell_price")?.let { + list[neuItemId] = it } - jsonObject["motes_sell_price"]?.let { - val neuItemId = NEUItems.transHypixelNameToInternalName(hypixelId) - motesPrice[neuItemId] = it.asDouble + itemData.getDoubleOrNull("motes_sell_price")?.let { + motesPrice[neuItemId] = it } } RiftAPI.motesPrice = motesPrice } catch (e: Throwable) { - e.printStackTrace() - ChatUtils.error("Error while trying to read bazaar item list from api: " + e.message) + ErrorManager.logErrorWithData( + e, "Error getting npc sell prices", + "hypixelApiResponse" to itemsData + ) } return list } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt index e9d755fab..6cbba135b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt @@ -37,3 +37,27 @@ fun JsonObject.getStringOrValue(key: String, alternative: String): String { alternative } } + +fun JsonObject.getStringOrNull(key: String): String? { + return if (has(key)) { + try { + get(key).asString + } catch (_: Exception) { + null + } + } else { + null + } +} + +fun JsonObject.getDoubleOrNull(key: String): Double? { + return if (has(key)) { + try { + get(key).asDouble + } catch (_: Exception) { + null + } + } else { + null + } +} -- cgit