diff options
author | hannibal2 <24389977+hannibal002@users.noreply.github.com> | 2024-03-22 18:04:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 18:04:45 +0100 |
commit | a8973b851ad4d3edb0e6c605eb7aea29c6e66938 (patch) | |
tree | 4bb7e04fdc48022fb7d9bdcd42915078bf4f7952 /src/main/java/at | |
parent | 6231ce3132dd22b539ed76d5999d281d9109ae0b (diff) | |
download | skyhanni-a8973b851ad4d3edb0e6c605eb7aea29c6e66938.tar.gz skyhanni-a8973b851ad4d3edb0e6c605eb7aea29c6e66938.tar.bz2 skyhanni-a8973b851ad4d3edb0e6c605eb7aea29c6e66938.zip |
Improvement: Load Trophy Fishing from NEU (#1123)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Co-authored-by: Cal <cwolfson58@gmail.com>
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at')
7 files changed, 124 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt index 5b095fa74..4d98581c3 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.data.IslandType import at.hannibal2.skyhanni.data.jsonobjects.local.FriendsJson import at.hannibal2.skyhanni.data.jsonobjects.local.JacobContestsJson import at.hannibal2.skyhanni.data.jsonobjects.local.KnownFeaturesJson +import at.hannibal2.skyhanni.data.jsonobjects.other.HypixelApiTrophyFish import at.hannibal2.skyhanni.features.fishing.trophy.TrophyRarity import at.hannibal2.skyhanni.features.misc.update.UpdateManager import at.hannibal2.skyhanni.utils.KotlinTypeAdapterFactory @@ -15,6 +16,7 @@ import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName import at.hannibal2.skyhanni.utils.NEUItems +import at.hannibal2.skyhanni.utils.NumberUtil.isInt import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import at.hannibal2.skyhanni.utils.tracker.SkyHanniTracker @@ -22,6 +24,7 @@ import com.google.gson.GsonBuilder import com.google.gson.JsonObject 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.moulconfig.observer.PropertyTypeAdapterFactory import io.github.moulberry.moulconfig.processor.BuiltinMoulConfigGuis @@ -134,6 +137,32 @@ class ConfigManager { return reader.nextString().toLong().asTimeMark() } }.nullSafe()) + .registerTypeAdapter(HypixelApiTrophyFish::class.java, object : TypeAdapter<HypixelApiTrophyFish>() { + override fun write(out: JsonWriter, value: HypixelApiTrophyFish) {} + + override fun read(reader: JsonReader): HypixelApiTrophyFish { + val trophyFish = mutableMapOf<String, Int>() + 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()) .enableComplexMapKeySerialization() .create() diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/fishing/trophyfishing/TrophyFishingConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/fishing/trophyfishing/TrophyFishingConfig.java index 2b5195797..1e0ec92b3 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/fishing/trophyfishing/TrophyFishingConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/fishing/trophyfishing/TrophyFishingConfig.java @@ -39,4 +39,10 @@ public class TrophyFishingConfig { @ConfigEditorBoolean @FeatureToggle public boolean odgerLocation = true; + + @Expose + @ConfigOption(name = "Load from NEU PV", desc = "Load Trophy fishing data when opening NEU PV.") + @ConfigEditorBoolean + @FeatureToggle + public boolean loadFromNeuPV = true; } diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/HypixelPlayerApiJson.kt b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/HypixelPlayerApiJson.kt new file mode 100644 index 000000000..0a18ca28e --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/HypixelPlayerApiJson.kt @@ -0,0 +1,22 @@ +package at.hannibal2.skyhanni.data.jsonobjects.other + +import com.google.gson.annotations.Expose +import com.google.gson.annotations.SerializedName + +data class HypixelPlayerApiJson( + @Expose val profiles: List<HypixelApiProfile> +) + +data class HypixelApiProfile( + @Expose val members: Map<String, HypixelApiPlayer>, + @Expose @SerializedName("cute_name") val profileName: String +) + +data class HypixelApiPlayer( + @Expose @SerializedName("trophy_fish") val trophyFish: HypixelApiTrophyFish +) + +data class HypixelApiTrophyFish( + val totalCaught: Int, + val caught: Map<String, Int> +) diff --git a/src/main/java/at/hannibal2/skyhanni/events/NeuProfileDataLoadedEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/NeuProfileDataLoadedEvent.kt new file mode 100644 index 000000000..175271e69 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/events/NeuProfileDataLoadedEvent.kt @@ -0,0 +1,12 @@ +package at.hannibal2.skyhanni.events + +import at.hannibal2.skyhanni.data.HypixelData +import at.hannibal2.skyhanni.data.jsonobjects.other.HypixelPlayerApiJson +import at.hannibal2.skyhanni.utils.LorenzUtils + +class NeuProfileDataLoadedEvent(val playerData: HypixelPlayerApiJson) : LorenzEvent() { + fun getCurrentProfileData() = + playerData.profiles.firstOrNull { it.profileName.lowercase() == HypixelData.profileName } + + fun getCurrentPlayerData() = getCurrentProfileData()?.members?.get(LorenzUtils.getPlayerUuid()) +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt index 311661ed7..78fbf6731 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt @@ -1,10 +1,13 @@ package at.hannibal2.skyhanni.features.fishing.trophy +import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.data.ProfileStorageData import at.hannibal2.skyhanni.data.jsonobjects.repo.TrophyFishJson import at.hannibal2.skyhanni.data.jsonobjects.repo.TrophyFishJson.TrophyFishInfo +import at.hannibal2.skyhanni.events.NeuProfileDataLoadedEvent import at.hannibal2.skyhanni.events.RepositoryReloadEvent import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.StringUtils.splitLines import net.minecraft.event.HoverEvent @@ -13,6 +16,7 @@ import net.minecraft.util.ChatStyle import net.minecraftforge.fml.common.eventhandler.SubscribeEvent object TrophyFishManager { + private val config get() = SkyHanniMod.feature.fishing.trophyFishing @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { @@ -20,9 +24,40 @@ object TrophyFishManager { trophyFishInfo = data.trophy_fish } - val fishes: MutableMap<String, MutableMap<TrophyRarity, Int>>? + val fish: MutableMap<String, MutableMap<TrophyRarity, Int>>? get() = ProfileStorageData.profileSpecific?.crimsonIsle?.trophyFishes + private var loadedNeu = false + + @SubscribeEvent + fun onNeuProfileDataLoaded(event: NeuProfileDataLoadedEvent) { + if (loadedNeu || !config.loadFromNeuPV) return + + val caughtTrophyFish = event.getCurrentPlayerData()?.trophyFish?.caught ?: return + + loadedNeu = true + + val savedFish = fish ?: return + var changed = false + + for ((fishName, apiAmount) in caughtTrophyFish) { + val rarity = TrophyRarity.getByName(fishName) ?: continue + val name = fishName.split("_").dropLast(1).joinToString("") + + val savedFishData = savedFish.getOrPut(name) { mutableMapOf() } + + val currentSavedAmount = savedFishData[rarity] ?: 0 + if (apiAmount > currentSavedAmount) { + savedFishData[rarity] = apiAmount + ChatUtils.debug("Updated trophy fishing data from NEU PV: $name $rarity: $currentSavedAmount -> $apiAmount") + changed = true + } + } + if (changed) { + ChatUtils.chat("Updated Trophy Fishing data via NEU PV!") + } + } + private var trophyFishInfo = mapOf<String, TrophyFishInfo>() fun getInfo(internalName: String) = trophyFishInfo[internalName] diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishMessages.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishMessages.kt index 1a6bdfe66..c8880bdc2 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishMessages.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishMessages.kt @@ -4,7 +4,6 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.config.features.fishing.trophyfishing.ChatMessagesConfig.DesignFormat import at.hannibal2.skyhanni.events.LorenzChatEvent -import at.hannibal2.skyhanni.features.fishing.trophy.TrophyFishManager.fishes import at.hannibal2.skyhanni.features.fishing.trophy.TrophyFishManager.getTooltip import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut import at.hannibal2.skyhanni.utils.CollectionUtils.sumAllValues @@ -42,7 +41,7 @@ class TrophyFishMessages { val rawRarity = displayRarity.lowercase().removeColor() val rarity = TrophyRarity.getByName(rawRarity) ?: return - val trophyFishes = fishes ?: return + val trophyFishes = TrophyFishManager.fish ?: return val trophyFishCounts = trophyFishes.getOrPut(internalName) { mutableMapOf() } val amount = trophyFishCounts.addOrPut(rarity, 1) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index 67f01b531..467afe55f 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -1,7 +1,9 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.config.ConfigManager +import at.hannibal2.skyhanni.data.jsonobjects.other.HypixelPlayerApiJson import at.hannibal2.skyhanni.data.jsonobjects.repo.MultiFilterJson +import at.hannibal2.skyhanni.events.NeuProfileDataLoadedEvent import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent import at.hannibal2.skyhanni.events.RepositoryReloadEvent import at.hannibal2.skyhanni.features.inventory.bazaar.BazaarDataHolder @@ -15,6 +17,7 @@ import com.google.gson.JsonPrimitive import io.github.moulberry.notenoughupdates.NEUManager import io.github.moulberry.notenoughupdates.NEUOverlay import io.github.moulberry.notenoughupdates.NotEnoughUpdates +import io.github.moulberry.notenoughupdates.events.ProfileDataLoadedEvent import io.github.moulberry.notenoughupdates.overlays.AuctionSearchOverlay import io.github.moulberry.notenoughupdates.overlays.BazaarSearchOverlay import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe @@ -63,6 +66,21 @@ object NEUItems { allItemsCache = readAllNeuItems() } + @SubscribeEvent + fun onProfileDataLoaded(event: ProfileDataLoadedEvent) { + val apiData = event.data ?: return + try { + val playerData = ConfigManager.gson.fromJson<HypixelPlayerApiJson>(apiData) + NeuProfileDataLoadedEvent(playerData).postAndCatch() + + } catch (e: Exception) { + ErrorManager.logErrorWithData( + e, "Error reading hypixel player api data", + "data" to apiData + ) + } + } + @Deprecated("Use NEUInternalName rather than String", ReplaceWith("NEUInternalName.fromItemName(itemName)")) fun getRawInternalName(itemName: String): String = NEUInternalName.fromItemName(itemName).asString() |