diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt | 30 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt | 15 |
2 files changed, 25 insertions, 20 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt index 231f059d1..646337a27 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt @@ -7,14 +7,16 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.put +import at.hannibal2.skyhanni.utils.SimpleTimeMark import io.github.moulberry.notenoughupdates.util.SkyBlockTime import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration.Companion.minutes class MayorElection { - private var lastUpdate = 0L + private var lastUpdate = SimpleTimeMark.farPast() private var dispatcher = Dispatchers.IO companion object { @@ -37,20 +39,20 @@ class MayorElection { } private fun check() { - if (System.currentTimeMillis() > lastUpdate + 60_000 * 5) { - lastUpdate = System.currentTimeMillis() - SkyHanniMod.coroutineScope.launch { - val url = "https://api.hypixel.net/v2/resources/skyblock/election" - val jsonObject = withContext(dispatcher) { APIUtil.getJSONResponse(url) } - rawMayorData = ConfigManager.gson.fromJson(jsonObject, MayorJson::class.java) - val data = rawMayorData ?: return@launch - val map = mutableMapOf<Int, MayorJson.Candidate>() - map put data.mayor.election.getPairs() - data.current?.let { - map put data.current.getPairs() - } - candidates = map + if (lastUpdate.passedSince() < 20.minutes) return + lastUpdate = SimpleTimeMark.now() + + SkyHanniMod.coroutineScope.launch { + val url = "https://api.hypixel.net/v2/resources/skyblock/election" + val jsonObject = withContext(dispatcher) { APIUtil.getJSONResponse(url) } + rawMayorData = ConfigManager.gson.fromJson(jsonObject, MayorJson::class.java) + val data = rawMayorData ?: return@launch + val map = mutableMapOf<Int, MayorJson.Candidate>() + map put data.mayor.election.getPairs() + data.current?.let { + map put data.current.getPairs() } + candidates = map } checkCurrentMayor() diff --git a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt index 1801e7802..43f262d5b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt @@ -71,9 +71,11 @@ object APIUtil { e.printStackTrace() } else { - ErrorManager.logError( - Error("$apiName error for url: '$urlString'", e), - "Failed to load data from $apiName" + ErrorManager.logErrorWithData( + e, "$apiName error for url: '$urlString'", + "apiName" to apiName, + "urlString" to urlString, + "returnedData" to retSrc ) } } @@ -83,9 +85,10 @@ object APIUtil { if (silentError) { throw throwable } else { - ErrorManager.logError( - Error("$apiName error for url: '$urlString'", throwable), - "Failed to load data from $apiName" + ErrorManager.logErrorWithData( + throwable, "$apiName error for url: '$urlString'", + "apiName" to apiName, + "urlString" to urlString, ) } } finally { |