diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-12 05:48:46 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-12 05:48:46 +0100 |
commit | 5fa745e48252f443c8ca199959ab0adecf368adc (patch) | |
tree | fb469bdf843e3fb4ca285a2cf69c07f8f15b138f /src/main/java | |
parent | 210ddb127a1aa4bbb80c07156e78af8d9c5e7d4e (diff) | |
download | skyhanni-5fa745e48252f443c8ca199959ab0adecf368adc.tar.gz skyhanni-5fa745e48252f443c8ca199959ab0adecf368adc.tar.bz2 skyhanni-5fa745e48252f443c8ca199959ab0adecf368adc.zip |
Added farming weight leaderboard support
Diffstat (limited to 'src/main/java')
3 files changed, 101 insertions, 33 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java b/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java index 3de40bad0..f826ef313 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java @@ -330,6 +330,13 @@ public class Garden { public Position eliteFarmingWeightPos = new Position(-370, -167, false, true); @Expose + @ConfigOption(name = "Leaderboard Ranking", desc = "Show your position in the farming weight leaderboard. " + + "Only if your farming weight is high enough! Updates every 10 minutes.") + @ConfigEditorBoolean + @ConfigAccordionId(id = 10) + public boolean eliteFarmingWeightLeaderboard = false; + + @Expose @ConfigOption(name = "Plot Price", desc = "Show the price of the plot in coins when inside the Configure Plots inventory.") @ConfigEditorBoolean public boolean plotPrice = true; diff --git a/src/main/java/at/hannibal2/skyhanni/data/HyPixelData.kt b/src/main/java/at/hannibal2/skyhanni/data/HyPixelData.kt index d906be51a..b98664a2a 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/HyPixelData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/HyPixelData.kt @@ -26,7 +26,7 @@ class HyPixelData { var stranded = false var bingo = false - var profile = "" + var profileName = "" fun readSkyBlockArea(): String { return ScoreboardData.sidebarLinesFormatted @@ -64,13 +64,13 @@ class HyPixelData { val message = event.message.removeColor().lowercase() if (message.startsWith("your profile was changed to:")) { val newProfile = message.replace("your profile was changed to:", "").replace("(co-op)", "").trim() - profile = newProfile + profileName = newProfile ProfileJoinEvent(newProfile).postAndCatch() } if (message.startsWith("you are playing on profile:")) { val newProfile = message.replace("you are playing on profile:", "").replace("(co-op)", "").trim() - if (profile == newProfile) return - profile = newProfile + if (profileName == newProfile) return + profileName = newProfile ProfileJoinEvent(newProfile).postAndCatch() } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/EliteFarmingWeight.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/EliteFarmingWeight.kt index 8508e0b6c..ab47fe365 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/EliteFarmingWeight.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/EliteFarmingWeight.kt @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.api.CollectionAPI import at.hannibal2.skyhanni.data.HyPixelData import at.hannibal2.skyhanni.events.CollectionUpdateEvent import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.ProfileJoinEvent import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.round @@ -23,7 +24,7 @@ class EliteFarmingWeight { @SubscribeEvent fun onCollectionUpdate(event: CollectionUpdateEvent) { if (isEnabled()) { - breakBlocksToTryAgain = false + farmCropsToUpdate = false update() } } @@ -39,73 +40,133 @@ class EliteFarmingWeight { fun onWorldChange(event: WorldEvent.Load) { if (apiError) { apiError = false - breakBlocksToTryAgain = true + farmCropsToUpdate = true if (config.eliteFarmingWeightDisplay) { update() } } } + @SubscribeEvent + fun onProfileJoin(event: ProfileJoinEvent) { + weightApiCache = null + lbApiCache = null + display = "§6Farming Weight§7: §cFarm crops to update!" + } + companion object { + private val LB_POSTION_UPDATE_INTERVAL = 600_000 // 10 minutes + private val config get() = SkyHanniMod.feature.garden private val extraCollection = mutableMapOf<String, Long>() + private var display = "§6Farming Weight§7: §eLoading.." + private var profileId = "" + private var lastPositionUpdate = 0L + private var weightApiCache: JsonObject? = null + private var lbApiCache: JsonObject? = null + private var lbApiDirty = false + private var apiError = false + private var farmCropsToUpdate = false private fun isEnabled() = GardenAPI.inGarden() && config.eliteFarmingWeightDisplay fun addCrop(crop: String, diff: Int) { val old = extraCollection[crop] ?: 0L extraCollection[crop] = old + diff - breakBlocksToTryAgain = false + farmCropsToUpdate = false if (isEnabled()) { update() } } private fun update() = SkyHanniMod.coroutineScope.launch { - display = "§6Farming Weight§7: " + if (apiError) { - "§cAPI error!" - } else if (breakBlocksToTryAgain) { - "§cBreak blocks to try again!" - } else { - val collectionWeight = calculateCollectionWeight() - val cropWeight = collectionWeight.values.sum() - - try { - val totalWeight = cropWeight + getBonusWeight() - val format = DecimalFormat("#,##0.00").format(totalWeight) - "§e$format" - } catch (e: Exception) { - apiError = true - LorenzUtils.error("[SkyHanni] Failed to load farming weight data from elitebot.dev! please report this on discord!") - e.printStackTrace() - "§cAPI error!" + val weight = getWeight() + val leaderBoard = if (config.eliteFarmingWeightLeaderboard) getLeaderBoard() else "" + + display = "§6Farming Weight§7: $weight$leaderBoard" + } + + private suspend fun getLeaderBoard() = if (apiError || farmCropsToUpdate) { + "" + } else { + try { + val position = getLBPosition() + if (position != -1) { + val format = DecimalFormat("###,##0").format(position) + " §7[§b#$format§7]" + } else { + "" } + } catch (e: Exception) { + apiError = true + LorenzUtils.error("[SkyHanni] Failed to load farming weight data from elitebot.dev! please report this on discord!") + e.printStackTrace() + "" + } + } + + private suspend fun getLBPosition(): Int { + if (System.currentTimeMillis() > lastPositionUpdate + LB_POSTION_UPDATE_INTERVAL) { + lbApiDirty = true + lastPositionUpdate = System.currentTimeMillis() + } + + return getLBPositionAPI()["rank"].asInt + } + + private suspend fun getLBPositionAPI(): JsonObject { + if (!lbApiDirty) { + lbApiCache?.let { return it } + } + lbApiDirty = false + + val uuid = Minecraft.getMinecraft().thePlayer.uniqueID.toString().replace("-", "") + val url = "https://elitebot.dev/api/leaderboard/rank/weight/farming/$uuid/$profileId" + val result = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject + lbApiCache = result + return result + } + + private suspend fun getWeight() = if (apiError) { + "§cAPI error!" + } else if (farmCropsToUpdate) { + "§cFarm crops to update!" + } else { + val collectionWeight = calculateCollectionWeight() + val cropWeight = collectionWeight.values.sum() + + try { + val totalWeight = cropWeight + getBonusWeight() + val format = DecimalFormat("#,##0.00").format(totalWeight) + "§e$format" + } catch (e: Exception) { + apiError = true + LorenzUtils.error("[SkyHanni] Failed to load farming weight data from elitebot.dev! please report this on discord!") + e.printStackTrace() + "§cAPI error!" } } private suspend fun getBonusWeight(): Int { - for (profileEntry in getApiResult()["profiles"].asJsonObject.entrySet()) { + for (profileEntry in getWeightApi()["profiles"].asJsonObject.entrySet()) { val profile = profileEntry.value.asJsonObject val profileName = profile["cute_name"].asString.lowercase() - if (profileName == HyPixelData.profile) { + if (profileName == HyPixelData.profileName) { + profileId = profileEntry.key return profile["farming"].asJsonObject["bonus"].asInt } } return 0 } - private var apiCache: JsonObject? = null - private var apiError = false - private var breakBlocksToTryAgain = false - - private suspend fun getApiResult(): JsonObject { - apiCache?.let { return it } + private suspend fun getWeightApi(): JsonObject { + weightApiCache?.let { return it } val uuid = Minecraft.getMinecraft().thePlayer.uniqueID.toString().replace("-", "") val url = "https://elitebot.dev/api/weight/$uuid" val result = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject - apiCache = result + weightApiCache = result return result } |