diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-01-31 08:18:55 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-30 22:18:55 +0100 |
commit | b3876ec962f85b118cee83d53288c2e96804220a (patch) | |
tree | 3d16a7685edad663e9d7e200dc270eb41338c253 /src/main/java/at/hannibal2/skyhanni/data | |
parent | a20f7d1f2242aedcf54f5c2b8ebeca9a1d81fd93 (diff) | |
download | skyhanni-b3876ec962f85b118cee83d53288c2e96804220a.tar.gz skyhanni-b3876ec962f85b118cee83d53288c2e96804220a.tar.bz2 skyhanni-b3876ec962f85b118cee83d53288c2e96804220a.zip |
small api changes (#966)
Fewer election API updates, fewer election API errors. #966
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt | 30 |
1 files changed, 16 insertions, 14 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() |