From c8471f6629aabcc887b58947706ee79d6b89bab9 Mon Sep 17 00:00:00 2001 From: Walker Selby Date: Thu, 26 Oct 2023 15:42:45 +0100 Subject: Internal Change: Avoid hardcoded dispatcher (#589) --- src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt | 3 ++- .../hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt | 5 +++-- .../skyhanni/features/garden/farming/FarmingWeightDisplay.kt | 7 ++++--- src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt index 2508391d9..d4ad57e2d 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt @@ -15,6 +15,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class MayorElection { private var lastUpdate = 0L + private var dispatcher = Dispatchers.IO companion object { var rawMayorData: MayorJson? = null @@ -40,7 +41,7 @@ class MayorElection { lastUpdate = System.currentTimeMillis() SkyHanniMod.coroutineScope.launch { val url = "https://api.hypixel.net/resources/skyblock/election" - val jsonObject = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) } + val jsonObject = withContext(dispatcher) { APIUtil.getJSONResponse(url) } rawMayorData = ConfigManager.gson.fromJson(jsonObject, MayorJson::class.java) val data = rawMayorData ?: return@launch val map = mutableMapOf() diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt index e1f069cdf..3254e685b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt @@ -37,6 +37,7 @@ import javax.swing.UIManager import kotlin.time.Duration.Companion.seconds object GardenNextJacobContest { + private var dispatcher = Dispatchers.IO private var display = emptyList() private var simpleDisplay = emptyList() private var contests = mutableMapOf() @@ -421,7 +422,7 @@ object GardenNextJacobContest { private suspend fun fetchUpcomingContests() { try { val url = "https://api.elitebot.dev/contests/at/now" - val result = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject + val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject val newContests = mutableMapOf() @@ -482,7 +483,7 @@ object GardenNextJacobContest { val url = "https://api.elitebot.dev/contests/at/now" val body = Gson().toJson(formatted) - val result = withContext(Dispatchers.IO) { APIUtil.postJSONIsSuccessful(url, body) } + val result = withContext(dispatcher) { APIUtil.postJSONIsSuccessful(url, body) } if (result) { LorenzUtils.chat("§e[SkyHanni] Successfully submitted this years upcoming contests, thank you for helping everyone out!") 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 38a21b1d8..e8491ae96 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 @@ -90,6 +90,7 @@ class FarmingWeightDisplay { companion object { private val config get() = SkyHanniMod.feature.garden.eliteFarmingWeights private val localCounter = mutableMapOf() + private var dispatcher = Dispatchers.IO private var display = emptyList() private var profileId = "" @@ -399,7 +400,7 @@ 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(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject + val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject if (isEtaEnabled()) { nextPlayers.clear() @@ -424,7 +425,7 @@ class FarmingWeightDisplay { val url = "https://api.elitebot.dev/weight/$uuid" try { - val result = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject + val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject for (profileEntry in result["profiles"].asJsonArray) { val profile = profileEntry.asJsonObject val profileName = profile["profileName"].asString.lowercase() @@ -504,7 +505,7 @@ class FarmingWeightDisplay { attemptingCropWeightFetch = true val url = "https://api.elitebot.dev/weights" - val result = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject + val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject for (crop in result.entrySet()) { val cropType = CropType.getByName(crop.key) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt index 035259a1a..31008d9b4 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt @@ -14,6 +14,7 @@ import java.awt.datatransfer.UnsupportedFlavorException import kotlin.time.Duration.Companion.milliseconds object ClipboardUtils { + private var dispatcher = Dispatchers.IO private var lastClipboardAccessTime = SimpleTimeMark.farPast() private fun canAccessClipboard(): Boolean { @@ -55,7 +56,7 @@ object ClipboardUtils { suspend fun readFromClipboard(step: Int = 0): String? { try { return try { - withContext(Dispatchers.IO) { + withContext(dispatcher) { getClipboard()?.getData(DataFlavor.stringFlavor)?.toString() } } catch (e: UnsupportedFlavorException) { -- cgit