diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-12-05 15:47:21 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-12-05 15:47:21 +0100 |
commit | c4dfb229e7eaaffefe053afc90ac1371db24936e (patch) | |
tree | d32ea7501e2ecd0e62b9923aef3a4fa0d380e248 /src/main/java/at | |
parent | 4750fec1fe3a3cb2a748df627c8d2343b6fd969d (diff) | |
download | skyhanni-c4dfb229e7eaaffefe053afc90ac1371db24936e.tar.gz skyhanni-c4dfb229e7eaaffefe053afc90ac1371db24936e.tar.bz2 skyhanni-c4dfb229e7eaaffefe053afc90ac1371db24936e.zip |
GardenNextJacobContest now uses SimpleTimeMark. SimpleTimeMark is storable in the config and compareable
Diffstat (limited to 'src/main/java/at')
4 files changed, 40 insertions, 22 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt index 9025703f5..04e01b565 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt @@ -14,6 +14,8 @@ import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName import at.hannibal2.skyhanni.utils.NEUItems +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import at.hannibal2.skyhanni.utils.tracker.SkyHanniTracker import com.google.gson.GsonBuilder import com.google.gson.JsonObject @@ -121,6 +123,15 @@ class ConfigManager { return TrackerDisplayMode.valueOf(reader.nextString()) } }.nullSafe()) + .registerTypeAdapter(SimpleTimeMark::class.java, object : TypeAdapter<SimpleTimeMark>() { + override fun write(out: JsonWriter, value: SimpleTimeMark) { + out.value(value.toMillis()) + } + + override fun read(reader: JsonReader): SimpleTimeMark { + return reader.nextString().toLong().asTimeMark() + } + }.nullSafe()) .enableComplexMapKeySerialization() .create() diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/JacobContestsJson.java b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/JacobContestsJson.java index 0eb31c5fd..96b21ae6a 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/JacobContestsJson.java +++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/local/JacobContestsJson.java @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.data.jsonobjects.local; import at.hannibal2.skyhanni.features.garden.CropType; +import at.hannibal2.skyhanni.utils.SimpleTimeMark; import com.google.gson.annotations.Expose; import java.util.HashMap; @@ -9,5 +10,5 @@ import java.util.Map; public class JacobContestsJson { @Expose - public Map<Long, List<CropType>> contestTimes = new HashMap<>(); + public Map<SimpleTimeMark, List<CropType>> contestTimes = new HashMap<>(); } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt index 2531c7cb1..f6da72d88 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt @@ -16,6 +16,8 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.RenderUtils.renderSingleLineWithItems import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import at.hannibal2.skyhanni.utils.SoundUtils import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor @@ -31,28 +33,27 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import org.lwjgl.opengl.Display import java.awt.event.MouseAdapter import java.awt.event.MouseEvent -import java.time.Instant import javax.swing.JButton import javax.swing.JFrame import javax.swing.JOptionPane import javax.swing.UIManager +import kotlin.time.Duration import kotlin.time.Duration.Companion.days +import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds -import kotlin.time.DurationUnit -import kotlin.time.toDuration object GardenNextJacobContest { private var dispatcher = Dispatchers.IO private var display = emptyList<Any>() private var simpleDisplay = emptyList<String>() - private var contests = mutableMapOf<Long, FarmingContest>() + private var contests = mutableMapOf<SimpleTimeMark, FarmingContest>() private var inCalendar = false private val patternDay = "§aDay (?<day>.*)".toPattern() private val patternMonth = "(?<month>.*), Year (?<year>.*)".toPattern() private val patternCrop = "§(e○|6☘) §7(?<crop>.*)".toPattern() private const val maxContestsPerYear = 124 - private const val contestDuration = 1_000 * 60 * 20 + private val contestDuration = 20.minutes private var lastWarningTime = 0L private var loadedContestsYear = -1 private var nextContestsAvailableAt = -1L @@ -127,7 +128,7 @@ object GardenNextJacobContest { private fun readCalendar(items: Collection<ItemStack>, year: Int, month: Int) { if (contests.isNotEmpty() && loadedContestsYear != year) { val endTime = contests.values.first().endTime - val lastYear = SkyBlockTime.fromInstant(Instant.ofEpochMilli(endTime)).year + val lastYear = endTime.toSkyBlockTime().year if (year != lastYear) { contests.clear() } @@ -153,7 +154,7 @@ object GardenNextJacobContest { val name = item.name ?: continue val day = patternDay.matchMatcher(name) { group("day").toInt() } ?: continue - val startTime = SkyBlockTime(year, month, day).toMillis() + val startTime = SkyBlockTime(year, month, day).asTimeMark() val crops = mutableListOf<CropType>() for (line in lore) { @@ -188,7 +189,7 @@ object GardenNextJacobContest { val currentYear = SkyBlockTime.now().year for (contest in contests.values) { - val contestYear = (SkyBlockTime.fromInstant(Instant.ofEpochMilli(contest.endTime))).year + val contestYear = (contest.endTime.toSkyBlockTime()).year // Ensure all stored contests are really from the current year if (contestYear != currentYear) continue @@ -203,7 +204,7 @@ object GardenNextJacobContest { val year = savedContests.firstNotNullOfOrNull { val endTime = it.key - SkyBlockTime.fromInstant(Instant.ofEpochMilli(endTime)).year + endTime.toSkyBlockTime().year } // Clear contests if from previous year @@ -236,7 +237,7 @@ object GardenNextJacobContest { } } - class FarmingContest(val endTime: Long, val crops: List<CropType>) + class FarmingContest(val endTime: SimpleTimeMark, val crops: List<CropType>) private fun update() { nextContestCrops.clear() @@ -274,7 +275,7 @@ object GardenNextJacobContest { } val nextContest = - contests.filter { it.value.endTime > System.currentTimeMillis() }.toSortedMap() + contests.filter { !it.value.endTime.isInPast() }.toSortedMap() .firstNotNullOfOrNull { it.value } // Show next contest if (nextContest != null) return drawNextContest(nextContest, list) @@ -295,9 +296,8 @@ object GardenNextJacobContest { nextContest: FarmingContest, list: MutableList<Any>, ): MutableList<Any> { - var duration = nextContest.endTime - System.currentTimeMillis() - val durationObj = duration.toDuration(DurationUnit.MILLISECONDS) - if (durationObj > 4.days) { + var duration = nextContest.endTime.timeUntil() + if (duration > 4.days) { list.add("§New SB Year, wait a bit.") return list } @@ -336,9 +336,9 @@ object GardenNextJacobContest { return null } - private fun warn(timeInMillis: Long, crops: List<CropType>, boostedCrop: CropType?) { + private fun warn(duration: Duration, crops: List<CropType>, boostedCrop: CropType?) { if (!config.warn) return - if (config.warnTime <= timeInMillis / 1000) return + if (config.warnTime.seconds <= duration) return if (System.currentTimeMillis() < lastWarningTime) return lastWarningTime = System.currentTimeMillis() + 60_000 * 40 @@ -348,7 +348,8 @@ object GardenNextJacobContest { LorenzUtils.sendTitle("§eFarming Contest!", 5.seconds) SoundUtils.playBeepSound() - val cropTextNoColor = crops.joinToString(", ") { if (it == boostedCrop) "<b>${it.cropName}</b>" else it.cropName } + val cropTextNoColor = + crops.joinToString(", ") { if (it == boostedCrop) "<b>${it.cropName}</b>" else it.cropName } if (config.warnPopup && !Display.isActive()) { SkyHanniMod.coroutineScope.launch { openPopupWindow( @@ -449,12 +450,13 @@ object GardenNextJacobContest { val url = "https://api.elitebot.dev/contests/at/now" val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject - val newContests = mutableMapOf<Long, FarmingContest>() + val newContests = mutableMapOf<SimpleTimeMark, FarmingContest>() val complete = result["complete"].asBoolean if (complete) { for (entry in result["contests"].asJsonObject.entrySet()) { var timestamp = entry.key.toLongOrNull() ?: continue + val timeMark = (timestamp * 1000).asTimeMark() timestamp *= 1_000 // Seconds to milliseconds val crops = entry.value.asJsonArray.map { @@ -463,7 +465,7 @@ object GardenNextJacobContest { if (crops.size != 3) continue - newContests[timestamp + contestDuration] = FarmingContest(timestamp + contestDuration, crops) + newContests[timeMark + contestDuration] = FarmingContest(timeMark + contestDuration, crops) } } else { LorenzUtils.chat("This years contests aren't available to fetch automatically yet, please load them from your calender or wait 10 minutes!") @@ -500,7 +502,7 @@ object GardenNextJacobContest { val formatted = mutableMapOf<Long, List<String>>() for ((endTime, contest) in contests) { - formatted[endTime / 1000] = contest.crops.map { + formatted[endTime.toMillis() / 1000] = contest.crops.map { it.cropName } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt index 3e30f300c..66629d11d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt @@ -6,7 +6,7 @@ import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds @JvmInline -value class SimpleTimeMark(private val millis: Long) { +value class SimpleTimeMark(private val millis: Long) : Comparable<SimpleTimeMark> { operator fun minus(other: SimpleTimeMark) = (millis - other.millis).milliseconds @@ -24,6 +24,8 @@ value class SimpleTimeMark(private val millis: Long) { fun isFarPast() = millis == 0L + override fun compareTo(other: SimpleTimeMark): Int = millis.compareTo(other.millis) + override fun toString(): String { if (millis == 0L) return "The Far Past" return Instant.ofEpochMilli(millis).toString() @@ -31,6 +33,8 @@ value class SimpleTimeMark(private val millis: Long) { fun toMillis() = millis + fun toSkyBlockTime() = SkyBlockTime.fromInstant(Instant.ofEpochMilli(millis)) + companion object { fun now() = SimpleTimeMark(System.currentTimeMillis()) fun farPast() = SimpleTimeMark(0) |