aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-04-12 22:31:06 +0200
committerhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-04-12 22:31:06 +0200
commit70dfeb1e6b3f241731063d7014207b02b074bd8e (patch)
tree6486c17c272cb72108676b9804753c979c8905c4 /src/main/java/at/hannibal2/skyhanni/data
parent1a824ba175cbb3b31af86892e571fed9dc9ccf1b (diff)
downloadskyhanni-70dfeb1e6b3f241731063d7014207b02b074bd8e.tar.gz
skyhanni-70dfeb1e6b3f241731063d7014207b02b074bd8e.tar.bz2
skyhanni-70dfeb1e6b3f241731063d7014207b02b074bd8e.zip
Added mayor election data and added Finnegan Farming Simulator support for crop milestone display
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/MayorElectionData.kt83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/MayorElectionData.kt b/src/main/java/at/hannibal2/skyhanni/data/MayorElectionData.kt
new file mode 100644
index 000000000..2dd8e19cd
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/data/MayorElectionData.kt
@@ -0,0 +1,83 @@
+package at.hannibal2.skyhanni.data
+
+import at.hannibal2.skyhanni.SkyHanniMod
+import at.hannibal2.skyhanni.utils.APIUtil
+import at.hannibal2.skyhanni.utils.LorenzUtils
+import at.hannibal2.skyhanni.utils.LorenzUtils.sorted
+import at.hannibal2.skyhanni.utils.MayorData
+import at.hannibal2.skyhanni.utils.MayorData.Candidate
+import com.google.gson.GsonBuilder
+import io.github.moulberry.moulconfig.observer.PropertyTypeAdapterFactory
+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 net.minecraftforge.fml.common.gameevent.TickEvent
+
+class MayorElectionData {
+ private var tick = 0
+ private var lastUpdate = 0L
+
+ private val gson = GsonBuilder().setPrettyPrinting()
+ .registerTypeAdapterFactory(PropertyTypeAdapterFactory())
+ .create()
+
+ companion object {
+ var rawMayorData: MayorData? = null
+ var candidates = mapOf<Int, Candidate>()
+ var currentCandidate: Candidate? = null
+
+ fun isPerkActive(mayor: String, perk: String): Boolean {
+ return currentCandidate?.let { currentCandidate ->
+ currentCandidate.name == mayor && currentCandidate.perks.any { it.name == perk }
+ } ?: false
+ }
+
+ }
+
+ @SubscribeEvent
+ fun onTick(event: TickEvent.ClientTickEvent) {
+ if (event.phase != TickEvent.Phase.START) return
+ if (!LorenzUtils.onHypixel) return
+
+ tick++
+
+ if (tick % 60 == 0) {
+ check()
+ }
+ }
+
+ private fun check() {
+ if (System.currentTimeMillis() > lastUpdate + 60_000 * 5) {
+ lastUpdate = System.currentTimeMillis()
+ SkyHanniMod.coroutineScope.launch {
+ val url = "https://api.hypixel.net/resources/skyblock/election"
+ val jsonObject = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }
+ rawMayorData = gson.fromJson(jsonObject, MayorData::class.java).also {
+ candidates = mapOf(it.mayor.election.getPairs(), it.current.getPairs())
+ }
+ }
+ }
+
+ checkCurrentMayor()
+ }
+
+ private fun checkCurrentMayor() {
+ var currentYear = SkyBlockTime.now().year
+
+ // The time in the current skyblock year when the election circle will restart
+ val month = 3 // Late Spring
+ val nextMayorTime = SkyBlockTime(currentYear, month, day = 27).toMillis()
+
+ // Is it still the major from old sb year?
+ if (nextMayorTime > System.currentTimeMillis()) {
+ currentYear--
+ }
+ currentCandidate = candidates[currentYear]
+ }
+
+ private fun MayorData.Election.getPairs() = year + 1 to candidates.bestCandidate()
+
+ private fun List<MayorData.Candidate>.bestCandidate() = map { it to it.votes }.sorted().last().first
+} \ No newline at end of file