aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-02-24 10:17:22 +1100
committerGitHub <noreply@github.com>2024-02-24 00:17:22 +0100
commit4f3309f1f2d17e3cde4544dc9f6325b0d9bfd721 (patch)
tree78afbbcfeb7c7066a8522cf68b8dc714b29ad765 /src/main/java/at
parent8eac8a1a23221590d6bfb2a8ff918d9318661ef6 (diff)
downloadskyhanni-4f3309f1f2d17e3cde4544dc9f6325b0d9bfd721.tar.gz
skyhanni-4f3309f1f2d17e3cde4544dc9f6325b0d9bfd721.tar.bz2
skyhanni-4f3309f1f2d17e3cde4544dc9f6325b0d9bfd721.zip
Make hypixel items api response a proper json object #1039
Diffstat (limited to 'src/main/java/at')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/SkyblockItemsDataJson.kt14
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt28
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt24
3 files changed, 25 insertions, 41 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/SkyblockItemsDataJson.kt b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/SkyblockItemsDataJson.kt
new file mode 100644
index 000000000..0676a5e8e
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/SkyblockItemsDataJson.kt
@@ -0,0 +1,14 @@
+package at.hannibal2.skyhanni.data.jsonobjects.other
+
+import com.google.gson.annotations.Expose
+import com.google.gson.annotations.SerializedName
+
+data class SkyblockItemsDataJson(
+ @Expose val items: List<SkyblockItemData>
+)
+
+data class SkyblockItemData(
+ @Expose val id: String?,
+ @Expose @SerializedName("npc_sell_price") val npcPrice: Double?,
+ @Expose @SerializedName("motes_sell_price") val motesPrice: Double?
+)
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 44ce4b481..02e1f1446 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt
@@ -1,6 +1,8 @@
package at.hannibal2.skyhanni.features.bazaar
import at.hannibal2.skyhanni.SkyHanniMod
+import at.hannibal2.skyhanni.config.ConfigManager
+import at.hannibal2.skyhanni.data.jsonobjects.other.SkyblockItemsDataJson
import at.hannibal2.skyhanni.features.rift.RiftAPI
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.APIUtil
@@ -11,8 +13,7 @@ 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 at.hannibal2.skyhanni.utils.fromJson
import kotlinx.coroutines.launch
import kotlin.concurrent.fixedRateTimer
@@ -28,28 +29,21 @@ 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")
-
+ val apiResponse = APIUtil.getJSONResponse("https://api.hypixel.net/v2/resources/skyblock/items")
try {
- val motesPrice = mutableMapOf<NEUInternalName, Double>()
- for (item in itemsData.getAsJsonArray("items")) {
- val itemData = item.asJsonObject
+ val itemsData = ConfigManager.gson.fromJson<SkyblockItemsDataJson>(apiResponse)
- val hypixelId = itemData.getStringOrNull("id") ?: continue
- val neuItemId = NEUItems.transHypixelNameToInternalName(hypixelId)
-
- itemData.getDoubleOrNull("npc_sell_price")?.let {
- list[neuItemId] = it
- }
- itemData.getDoubleOrNull("motes_sell_price")?.let {
- motesPrice[neuItemId] = it
- }
+ val motesPrice = mutableMapOf<NEUInternalName, Double>()
+ for (item in itemsData.items) {
+ val neuItemId = NEUItems.transHypixelNameToInternalName(item.id ?: continue)
+ item.npcPrice?.let { list[neuItemId] = it }
+ item.motesPrice?.let { motesPrice[neuItemId] = it }
}
RiftAPI.motesPrice = motesPrice
} catch (e: Throwable) {
ErrorManager.logErrorWithData(
e, "Error getting npc sell prices",
- "hypixelApiResponse" to itemsData
+ "hypixelApiResponse" to apiResponse
)
}
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 6cbba135b..e9d755fab 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt
@@ -37,27 +37,3 @@ 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
- }
-}