aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-01-31 08:18:55 +1100
committerGitHub <noreply@github.com>2024-01-30 22:18:55 +0100
commitb3876ec962f85b118cee83d53288c2e96804220a (patch)
tree3d16a7685edad663e9d7e200dc270eb41338c253
parenta20f7d1f2242aedcf54f5c2b8ebeca9a1d81fd93 (diff)
downloadskyhanni-b3876ec962f85b118cee83d53288c2e96804220a.tar.gz
skyhanni-b3876ec962f85b118cee83d53288c2e96804220a.tar.bz2
skyhanni-b3876ec962f85b118cee83d53288c2e96804220a.zip
small api changes (#966)
Fewer election API updates, fewer election API errors. #966
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt30
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt15
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 {