diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-02-29 06:17:29 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-28 20:17:29 +0100 |
commit | 05e5232694e26558b8ebd36c6d337854f9c6c192 (patch) | |
tree | 6299fff36a8548b18e955d4729b1ed986293d68b /src/main | |
parent | fbadabdb0ddb0bba18aa7421ffb09795463ea07f (diff) | |
download | skyhanni-05e5232694e26558b8ebd36c6d337854f9c6c192.tar.gz skyhanni-05e5232694e26558b8ebd36c6d337854f9c6c192.tar.bz2 skyhanni-05e5232694e26558b8ebd36c6d337854f9c6c192.zip |
Fixed Farming Weight Display sometimes not showing when joining the Garden. (#1073)
Diffstat (limited to 'src/main')
3 files changed, 99 insertions, 78 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt b/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt index b81e09640..274cf24e0 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt @@ -3,12 +3,9 @@ package at.hannibal2.skyhanni.data import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.SackData import at.hannibal2.skyhanni.config.Storage -import at.hannibal2.skyhanni.data.HypixelData import at.hannibal2.skyhanni.events.ConfigLoadEvent import at.hannibal2.skyhanni.events.HypixelJoinEvent -import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.events.LorenzTickEvent -import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.events.ProfileJoinEvent import at.hannibal2.skyhanni.events.TabListUpdateEvent import at.hannibal2.skyhanni.utils.ChatUtils @@ -16,10 +13,9 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.UtilsPatterns -import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern -import kotlin.time.Duration.Companion.seconds import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration.Companion.seconds object ProfileStorageData { @@ -28,14 +24,6 @@ object ProfileStorageData { var loaded = false private var noTabListTime = SimpleTimeMark.farPast() - private var nextProfile: String? = null - - private val patternGroup = RepoPattern.group("data.profile") - private val profileSwitchPattern by patternGroup.pattern( - "switch", - "§7Switching to profile (?<name>.*)\\.\\.\\." - ) - private var sackPlayers: SackData.PlayerSpecific? = null var sackProfiles: SackData.ProfileSpecific? = null diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/EliteBotJson.kt b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/EliteBotJson.kt new file mode 100644 index 000000000..3cdcfa512 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/EliteBotJson.kt @@ -0,0 +1,30 @@ +package at.hannibal2.skyhanni.data.jsonobjects.other + +import com.google.gson.annotations.Expose +import com.google.gson.annotations.SerializedName + +data class EliteWeightJson( + @Expose val selectedProfileId: String, + @Expose val profiles: List<WeightProfile> +) + +data class WeightProfile( + @Expose val profileId: String, + @Expose val profileName: String, + @Expose val totalWeight: Double +) + +data class EliteLeaderboardJson( + @Expose val data: EliteLeaderboard +) + +data class EliteLeaderboard( + @Expose val rank: Int, + @Expose val upcomingRank: Int, + @Expose val upcomingPlayers: List<UpcomingLeaderboardPlayer> +) + +data class UpcomingLeaderboardPlayer( + @Expose @SerializedName("ign") val name: String, + @Expose @SerializedName("amount") val weight: Double +) diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt index 190a9b14c..e7b1679cd 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt @@ -1,9 +1,13 @@ package at.hannibal2.skyhanni.features.garden.farming import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.data.HypixelData import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.data.jsonobjects.other.EliteLeaderboardJson +import at.hannibal2.skyhanni.data.jsonobjects.other.EliteWeightJson +import at.hannibal2.skyhanni.data.jsonobjects.other.UpcomingLeaderboardPlayer import at.hannibal2.skyhanni.events.GardenToolChangeEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.LorenzTickEvent @@ -12,6 +16,7 @@ import at.hannibal2.skyhanni.events.ProfileJoinEvent import at.hannibal2.skyhanni.features.garden.CropType import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.features.garden.farming.GardenCropSpeed.getSpeed +import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.LorenzUtils @@ -21,10 +26,11 @@ import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.StringUtils import at.hannibal2.skyhanni.utils.TimeUtils +import at.hannibal2.skyhanni.utils.fromJson import at.hannibal2.skyhanni.utils.renderables.Renderable -import kotlinx.coroutines.Dispatchers +import com.google.gson.JsonObject +import com.google.gson.reflect.TypeToken import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.time.Duration.Companion.seconds @@ -55,7 +61,7 @@ class FarmingWeightDisplay { fun onProfileJoin(event: ProfileJoinEvent) { display = emptyList() profileId = "" - weight = -2.0 + weight = -1.0 nextPlayers.clear() rankGoal = -1 @@ -69,7 +75,6 @@ class FarmingWeightDisplay { SkyHanniMod.coroutineScope.launch { getCropWeights() - hasFetchedCropWeights = true } } @@ -80,11 +85,7 @@ class FarmingWeightDisplay { event.move(3, "garden.eliteFarmingWeightPos", "garden.eliteFarmingWeights.pos") event.move(3, "garden.eliteFarmingWeightLeaderboard", "garden.eliteFarmingWeights.leaderboard") event.move(3, "garden.eliteFarmingWeightOvertakeETA", "garden.eliteFarmingWeights.overtakeETA") - event.move( - 3, - "garden.eliteFarmingWeightOffScreenDropMessage", - "garden.eliteFarmingWeights.offScreenDropMessage" - ) + event.move(3, "garden.eliteFarmingWeightOffScreenDropMessage", "garden.eliteFarmingWeights.offScreenDropMessage") event.move(3, "garden.eliteFarmingWeightOvertakeETAAlways", "garden.eliteFarmingWeights.overtakeETAAlways") event.move(3, "garden.eliteFarmingWeightETAGoalRank", "garden.eliteFarmingWeights.ETAGoalRank") event.move(3, "garden.eliteFarmingWeightIgnoreLow", "garden.eliteFarmingWeights.ignoreLow") @@ -95,14 +96,13 @@ class FarmingWeightDisplay { private val config get() = GardenAPI.config.eliteFarmingWeights private val localCounter = mutableMapOf<CropType, Long>() - private var dispatcher = Dispatchers.IO private var display = emptyList<Renderable>() private var profileId = "" private var lastLeaderboardUpdate = 0L private var apiError = false private var leaderboardPosition = -1 - private var weight = -2.0 + private var weight = -1.0 private var localWeight = 0.0 private var weightPerSecond = -1.0 private var dirtyCropWeight = false @@ -110,7 +110,7 @@ class FarmingWeightDisplay { private var isLoadingLeaderboard = false private var rankGoal = -1 - private var nextPlayers = mutableListOf<UpcomingPlayer>() + private var nextPlayers = mutableListOf<UpcomingLeaderboardPlayer>() private val nextPlayer get() = nextPlayers.firstOrNull() private val recalculate by lazy { @@ -149,10 +149,6 @@ class FarmingWeightDisplay { display = errorMessage return } - if (weight == -2.0) { - display = Renderable.singeltonString("§6Farming Weight§7: §eLoading..") - return - } if (weight == -1.0) { if (!isLoadingWeight) { @@ -204,7 +200,7 @@ class FarmingWeightDisplay { } return if (leaderboardPosition != -1) { - val format = LorenzUtils.formatInteger(leaderboardPosition) + val format = leaderboardPosition.addSeparators() " §7[§b#$format§7]" } else { if (isLoadingLeaderboard) " §7[§b#?§7]" else "" @@ -410,7 +406,7 @@ class FarmingWeightDisplay { ) } - private suspend fun loadLeaderboardPosition() = try { + private fun loadLeaderboardPosition(): Int { val uuid = LorenzUtils.getPlayerUuid() val includeUpcoming = if (isEtaEnabled()) "?includeUpcoming=true" else "" @@ -418,70 +414,71 @@ class FarmingWeightDisplay { val atRank = if (isEtaEnabled() && goalRank != 10001) "&atRank=$goalRank" else "" val url = "https://api.elitebot.dev/leaderboard/rank/farmingweight/$uuid/$profileId$includeUpcoming$atRank" - val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject - - if (isEtaEnabled()) { - nextPlayers.clear() - // Array of 0-5 upcoming players (or possibly null) - result["upcomingPlayers"]?.asJsonArray?.let { - for (player in it) { - val playerData = player.asJsonObject - nextPlayers.add(UpcomingPlayer(playerData["ign"].asString, playerData["amount"].asDouble)) - } + val apiResponse = APIUtil.getJSONResponse(url) + + try { + val apiData = toEliteLeaderboardJson(apiResponse).data + + if (isEtaEnabled()) { + nextPlayers.clear() + apiData.upcomingPlayers.forEach { nextPlayers.add(it) } } + + return apiData.rank + } catch (e: Exception) { + ErrorManager.logErrorWithData( + e, "Error getting weight leaderboard position", + "url" to url, + "apiResponse" to apiResponse + ) + error() } + return -1 + } - result["rank"].asInt - } catch (e: Exception) { - error() - e.printStackTrace() - -1 + private fun toEliteLeaderboardJson(obj: JsonObject): EliteLeaderboardJson { + val jsonObject = JsonObject() + jsonObject.add("data", obj) + return ConfigManager.gson.fromJson<EliteLeaderboardJson>(jsonObject) } - private suspend fun loadWeight(localProfile: String) { + private fun loadWeight(localProfile: String) { val uuid = LorenzUtils.getPlayerUuid() val url = "https://api.elitebot.dev/weight/$uuid" + val apiResponse = APIUtil.getJSONResponse(url) try { - val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject - val selectedProfileId = result["selectedProfileId"].asString - val profileEntries = result["profiles"].asJsonArray - - var selectedProfileEntry = profileEntries.find { - it.asJsonObject["profileId"].asString == selectedProfileId - }?.asJsonObject - - // If the selected profile is not found or if the cute name doesn't match look for a different profile - // While it's not optimal to loop twice, this shouldn't happen often - if (selectedProfileEntry == null || selectedProfileEntry["profileName"].asString.lowercase() != localProfile) { - selectedProfileEntry = profileEntries.find { - it.asJsonObject["profileName"].asString.lowercase() == localProfile - }?.asJsonObject + val apiData = ConfigManager.gson.fromJson<EliteWeightJson>(apiResponse) + + val selectedProfileId = apiData.selectedProfileId + var selectedProfileEntry = apiData.profiles.find { it.profileId == selectedProfileId } + + if (selectedProfileEntry == null || selectedProfileEntry.profileName.lowercase() != localProfile) { + selectedProfileEntry = apiData.profiles.find { it.profileName.lowercase() == localProfile } } if (selectedProfileEntry != null) { - profileId = selectedProfileEntry["profileId"].asString - weight = selectedProfileEntry["totalWeight"].asDouble + profileId = selectedProfileEntry.profileId + weight = selectedProfileEntry.totalWeight localCounter.clear() dirtyCropWeight = true - return } - println("localProfile: '$localProfile'") - println("url: '$url'") - println("result: '$result'") } catch (e: Exception) { - println("url: '$url'") - e.printStackTrace() + ErrorManager.logErrorWithData( + e, "Error loading user farming weight", + "url" to url, + "apiResponse" to apiResponse + ) } error() } private fun error() { apiError = true - ChatUtils.error( + ErrorManager.skyHanniError( "Loading the farming weight data from elitebot.dev failed!\n" + "§eYou can re-enter the garden to try to fix the problem.\n" + "§cIf this message repeats, please report it on Discord!", @@ -539,16 +536,24 @@ class FarmingWeightDisplay { private var attemptingCropWeightFetch = false private var hasFetchedCropWeights = false - private suspend fun getCropWeights() { + private fun getCropWeights() { if (attemptingCropWeightFetch || hasFetchedCropWeights) return attemptingCropWeightFetch = true - val url = "https://api.elitebot.dev/weights" - val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject + val apiResponse = APIUtil.getJSONResponse(url) - for (crop in result.entrySet()) { - val cropType = CropType.getByName(crop.key) - factorPerCrop[cropType] = crop.value.asDouble + try { + val apiData = ConfigManager.gson.fromJson<Map<String, Double>>(apiResponse, object : TypeToken<Map<String, Double>>() {}.type) + for (crop in apiData) { + val cropType = CropType.getByName(crop.key) + factorPerCrop[cropType] = crop.value + } + hasFetchedCropWeights = true + } catch (e: Exception) { + ErrorManager.logErrorWithData( + e, "Error getting crop weights from elitebot.dev", + "apiResponse" to apiResponse + ) } } @@ -567,7 +572,5 @@ class FarmingWeightDisplay { CropType.CACTUS to 177_254.45, ) } - - class UpcomingPlayer(val name: String, val weight: Double) } } |