From a287668ee994e0c5920aa961d67db0611bb1dbb7 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Fri, 24 May 2024 22:48:05 +1000 Subject: Backend: Make MayorJson be kotlin instead of java (#1772) --- .../java/at/hannibal2/skyhanni/data/MayorAPI.kt | 20 ++++--- src/main/java/at/hannibal2/skyhanni/data/Mayors.kt | 6 +- .../skyhanni/data/jsonobjects/local/MayorJson.java | 70 ---------------------- .../skyhanni/data/jsonobjects/other/MayorJson.kt | 32 ++++++++++ 4 files changed, 46 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/MayorJson.java create mode 100644 src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/MayorJson.kt (limited to 'src/main/java/at/hannibal2/skyhanni') diff --git a/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt index ef9850a4c..083e7bcdc 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt @@ -4,11 +4,13 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.data.Mayor.Companion.setAssumeMayor import at.hannibal2.skyhanni.data.Mayor.Companion.setAssumeMayorJson -import at.hannibal2.skyhanni.data.jsonobjects.local.MayorJson +import at.hannibal2.skyhanni.data.jsonobjects.other.MayorCandidate +import at.hannibal2.skyhanni.data.jsonobjects.other.MayorElection +import at.hannibal2.skyhanni.data.jsonobjects.other.MayorJson import at.hannibal2.skyhanni.events.ConfigLoadEvent import at.hannibal2.skyhanni.events.DebugDataCollectEvent import at.hannibal2.skyhanni.events.LorenzChatEvent -import at.hannibal2.skyhanni.events.LorenzTickEvent +import at.hannibal2.skyhanni.events.SecondPassedEvent import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.CollectionUtils.put import at.hannibal2.skyhanni.utils.ConditionalUtils.onToggle @@ -17,6 +19,7 @@ import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import at.hannibal2.skyhanni.utils.SkyBlockTime import at.hannibal2.skyhanni.utils.StringUtils.matches +import at.hannibal2.skyhanni.utils.fromJson import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -43,7 +46,7 @@ object MayorAPI { private var dispatcher = Dispatchers.IO private var rawMayorData: MayorJson? = null - var candidates = mapOf() + var candidates = mapOf() private set var currentMayor: Mayor? = null private set @@ -66,9 +69,8 @@ object MayorAPI { fun mayorNameWithColorCode(input: String) = mayorNameToColorCode(input) + input @SubscribeEvent - fun onTick(event: LorenzTickEvent) { + fun onSecondPassed(event: SecondPassedEvent) { if (!LorenzUtils.onHypixel) return - if (event.repeatSeconds(2)) { checkHypixelAPI() getTimeTillNextMayor() @@ -121,9 +123,9 @@ object MayorAPI { 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) + rawMayorData = ConfigManager.gson.fromJson(jsonObject) val data = rawMayorData ?: return@launch - val map = mutableMapOf() + val map = mutableMapOf() map put data.mayor.election.getPairs() data.current?.let { map put data.current.getPairs() @@ -133,9 +135,9 @@ object MayorAPI { } } - private fun MayorJson.Election.getPairs() = year + 1 to candidates.bestCandidate() + private fun MayorElection.getPairs() = year + 1 to candidates.bestCandidate() - private fun List.bestCandidate() = maxBy { it.votes } + private fun List.bestCandidate() = maxBy { it.votes } @SubscribeEvent fun onConfigReload(event: ConfigLoadEvent) { diff --git a/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt b/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt index 1be03c363..1d851661b 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt @@ -1,7 +1,7 @@ package at.hannibal2.skyhanni.data import at.hannibal2.skyhanni.data.MayorAPI.foxyExtraEventPattern -import at.hannibal2.skyhanni.data.jsonobjects.local.MayorJson +import at.hannibal2.skyhanni.data.jsonobjects.other.MayorPerk import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher @@ -43,7 +43,7 @@ enum class Mayor( fun getMayorFromName(name: String): Mayor? = entries.firstOrNull { it.mayorName == name } - fun setAssumeMayorJson(name: String, perksJson: ArrayList): Mayor? { + fun setAssumeMayorJson(name: String, perksJson: List): Mayor? { val mayor = getMayorFromName(name) if (mayor == null) { ErrorManager.logErrorStateWithData( @@ -72,7 +72,7 @@ enum class Mayor( } } - private fun MayorJson.Perk.renameIfFoxyExtraEventPerkFound(): String? { + private fun MayorPerk.renameIfFoxyExtraEventPerkFound(): String? { val foxyExtraEventPairs = mapOf( "Spooky Festival" to "Extra Event (Spooky)", "Mining Fiesta" to "Extra Event (Mining)", diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/MayorJson.java b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/MayorJson.java deleted file mode 100644 index 6525e59d3..000000000 --- a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/MayorJson.java +++ /dev/null @@ -1,70 +0,0 @@ -package at.hannibal2.skyhanni.data.jsonobjects.local; - -import com.google.gson.annotations.Expose; - -import java.util.ArrayList; - -public class MayorJson { - @Expose - public boolean success; - @Expose - public long lastUpdated; - @Expose - public Mayor mayor; - @Expose - public Election current; - - public class Candidate { - @Expose - public String key; - @Expose - public String name; - @Expose - public ArrayList perks; - @Expose - public int votes; - - @Override - public String toString() { - return "Candidate{" + - "key='" + key + '\'' + - ", name='" + name + '\'' + - ", perks=" + perks + - ", votes=" + votes + - '}'; - } - } - - public class Election { - @Expose - public int year; - @Expose - public ArrayList candidates; - } - - public class Mayor { - @Expose - public String key; - @Expose - public String name; - @Expose - public ArrayList perks; - @Expose - public Election election; - } - - public static class Perk { - @Expose - public String name; - @Expose - public String description; - - @Override - public String toString() { - return "Perk{" + - "name='" + name + '\'' + - ", description='" + description + '\'' + - '}'; - } - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/MayorJson.kt b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/MayorJson.kt new file mode 100644 index 000000000..d8f0df279 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/other/MayorJson.kt @@ -0,0 +1,32 @@ +package at.hannibal2.skyhanni.data.jsonobjects.other + +import com.google.gson.annotations.Expose + +data class MayorJson( + @Expose val mayor: MayorInfo, + @Expose val current: MayorElection?, +) + +data class MayorInfo( + @Expose val key: String, + @Expose val name: String, + @Expose val perks: List, + @Expose val election: MayorElection, +) + +data class MayorElection( + @Expose val year: Int, + @Expose val candidates: List, +) + +data class MayorCandidate( + @Expose val key: String, + @Expose val name: String, + @Expose val perks: List, + @Expose val votes: Int, +) + +data class MayorPerk( + @Expose val name: String, + @Expose val description: String, +) -- cgit