diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-02-23 07:43:57 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-22 21:43:57 +0100 |
commit | 56cb5ef4697acdb2acb9ff1bcbc0e63043b46b92 (patch) | |
tree | b492f8272488e1309576d4d193048c49b22ac30b /src | |
parent | a9263a24a55396739e4bbfd7af7119863dc1e953 (diff) | |
download | skyhanni-56cb5ef4697acdb2acb9ff1bcbc0e63043b46b92.tar.gz skyhanni-56cb5ef4697acdb2acb9ff1bcbc0e63043b46b92.tar.bz2 skyhanni-56cb5ef4697acdb2acb9ff1bcbc0e63043b46b92.zip |
Make future npc price fetch errors better debuggable. #1036
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt | 31 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt | 24 |
2 files changed, 43 insertions, 12 deletions
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<NEUInternalName, Double> { val list = mutableMapOf<NEUInternalName, Double>() + 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<NEUInternalName, Double>() - 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 + } +} |