From 7960d2ad27ac68ea9dcfaf848bd611b13db0af87 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Fri, 12 Apr 2024 23:17:26 +1000 Subject: Backend: Update farming weight api links and format (#1308) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../java/at/hannibal2/skyhanni/utils/NEUItems.kt | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index 87a265c84..3d6f0e74e 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.config.ConfigManager +import at.hannibal2.skyhanni.data.jsonobjects.other.HypixelApiTrophyFish import at.hannibal2.skyhanni.data.jsonobjects.other.HypixelPlayerApiJson import at.hannibal2.skyhanni.data.jsonobjects.repo.MultiFilterJson import at.hannibal2.skyhanni.events.NeuProfileDataLoadedEvent @@ -11,9 +12,14 @@ import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.ItemBlink.checkBlinkItem import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName +import at.hannibal2.skyhanni.utils.NumberUtil.isInt import at.hannibal2.skyhanni.utils.StringUtils.removeColor import com.google.gson.JsonObject import com.google.gson.JsonPrimitive +import com.google.gson.TypeAdapter +import com.google.gson.stream.JsonReader +import com.google.gson.stream.JsonToken +import com.google.gson.stream.JsonWriter import io.github.moulberry.notenoughupdates.NEUManager import io.github.moulberry.notenoughupdates.NEUOverlay import io.github.moulberry.notenoughupdates.NotEnoughUpdates @@ -43,6 +49,37 @@ object NEUItems { private val recipesCache = mutableMapOf>() private val ingredientsCache = mutableMapOf>() + private val hypixelApiGson by lazy { + ConfigManager.createBaseGsonBuilder() + .registerTypeAdapter(HypixelApiTrophyFish::class.java, object : TypeAdapter() { + override fun write(out: JsonWriter, value: HypixelApiTrophyFish) {} + + override fun read(reader: JsonReader): HypixelApiTrophyFish { + val trophyFish = mutableMapOf() + var totalCaught = 0 + reader.beginObject() + while (reader.hasNext()) { + val key = reader.nextName() + if (key == "total_caught") { + totalCaught = reader.nextInt() + continue + } + if (reader.peek() == JsonToken.NUMBER) { + val valueAsString = reader.nextString() + if (valueAsString.isInt()) { + trophyFish[key] = valueAsString.toInt() + continue + } + } + reader.skipValue() + } + reader.endObject() + return HypixelApiTrophyFish(totalCaught, trophyFish) + } + }.nullSafe()) + .create() + } + var allItemsCache = mapOf() // item name -> internal name val allInternalNames = mutableListOf() val ignoreItemsFilter = MultiFilter() @@ -70,7 +107,7 @@ object NEUItems { fun onProfileDataLoaded(event: ProfileDataLoadedEvent) { val apiData = event.data ?: return try { - val playerData = ConfigManager.gson.fromJson(apiData) + val playerData = hypixelApiGson.fromJson(apiData) NeuProfileDataLoadedEvent(playerData).postAndCatch() } catch (e: Exception) { -- cgit