diff options
Diffstat (limited to 'src')
3 files changed, 51 insertions, 32 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt index 06bb732cc..7e26e900b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt @@ -12,6 +12,7 @@ import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.RenderUtils.renderString 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 @@ -20,16 +21,18 @@ import at.hannibal2.skyhanni.utils.TimeUtils import at.hannibal2.skyhanni.utils.TimeUtils.format import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.concurrent.fixedRateTimer -import kotlin.math.roundToLong +import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds +import kotlin.time.DurationUnit +import kotlin.time.toDuration class GardenVisitorTimer { private val config get() = SkyHanniMod.feature.garden.visitors.timer private val pattern = "§b§lVisitors: §r§f\\((?<time>.*)\\)".toPattern() private var render = "" - private var lastMillis = 0L - private var sixthVisitorArrivalTime: Long = 0 + private var lastMillis = 0.seconds + private var sixthVisitorArrivalTime = SimpleTimeMark.farPast() private var visitorJustArrived = false private var sixthVisitorReady = false private var lastTimerValue = "" @@ -37,11 +40,11 @@ class GardenVisitorTimer { //TODO nea? // private val visitorInterval by dynamic(GardenAPI::config, Storage.ProfileSpecific.GardenStorage::visitorInterval) - private var visitorInterval: Long? - get() = GardenAPI.storage?.visitorInterval + private var visitorInterval: Duration? + get() = GardenAPI.storage?.visitorInterval?.toDuration(DurationUnit.MILLISECONDS) set(value) { value?.let { - GardenAPI.storage?.visitorInterval = it + GardenAPI.storage?.visitorInterval = it.inWholeMilliseconds } } @@ -71,8 +74,8 @@ class GardenVisitorTimer { @SubscribeEvent fun onPreProfileSwitch(event: PreProfileSwitchEvent) { render = "" - lastMillis = 0 - sixthVisitorArrivalTime = 0 + lastMillis = 0.seconds + sixthVisitorArrivalTime = SimpleTimeMark.farPast() visitorJustArrived = false sixthVisitorReady = false } @@ -100,14 +103,14 @@ class GardenVisitorTimer { lastTimerUpdate = SimpleTimeMark.now() lastTimerValue = rawTime } - millis = TimeUtils.getMillis(rawTime) + millis = TimeUtils.getDuration(rawTime) } } if (lastVisitors != -1 && visitorsAmount - lastVisitors == 1) { if (!queueFull) { - visitorInterval = ((millis - 1) / 60_000L + 1) * 60_000L - GardenAPI.storage?.visitorInterval = visitorInterval + visitorInterval = millis + this.visitorInterval = visitorInterval } else { updateSixthVisitorArrivalTime() } @@ -119,10 +122,11 @@ class GardenVisitorTimer { visitorJustArrived = false sixthVisitorReady = false } - millis = sixthVisitorArrivalTime - System.currentTimeMillis() - GardenAPI.storage?.nextSixthVisitorArrival = - System.currentTimeMillis() + millis + (5 - visitorsAmount) * visitorInterval - if (isSixthVisitorEnabled() && millis < 0) { + millis = sixthVisitorArrivalTime.timeUntil() + + val nextSixthVisitorArrival = SimpleTimeMark.now() + millis + (visitorInterval * (5 - visitorsAmount)) + GardenAPI.storage?.nextSixthVisitorArrival = nextSixthVisitorArrival.toMillis() + if (isSixthVisitorEnabled() && millis.isNegative()) { visitorsAmount++ if (!sixthVisitorReady) { LorenzUtils.sendTitle("§a6th Visitor Ready", 5.seconds) @@ -135,27 +139,28 @@ class GardenVisitorTimer { val showGrayGuess = visitorsAmount < 5 && sinceLastTimerUpdate in 500.milliseconds..60.seconds val diff = lastMillis - millis - if (diff == 0L && visitorsAmount == lastVisitors && !showGrayGuess) return + if (diff == 0.seconds && visitorsAmount == lastVisitors && !showGrayGuess) return lastMillis = millis lastVisitors = visitorsAmount val formatColor = if (queueFull) "6" else "e" - val extraSpeed = if (diff in 2000..10_000) { - val factor = diff / 1000.0 - "§7/§$formatColor" + TimeUtils.formatDuration((millis / factor).roundToLong()) + val extraSpeed = if (diff in 2.seconds..10.seconds) { + val factor = diff.inWholeSeconds.toDouble() + val duration = millis / factor + "§7/§$formatColor" + duration.format() } else "" - if (config.newVisitorPing && millis < 10000) { + if (config.newVisitorPing && millis < 10.seconds) { SoundUtils.playBeepSound() } val formatDuration = if (showGrayGuess) { val oneMinute = 60.seconds - val min = TimeUtils.formatDuration(millis - oneMinute.inWholeMilliseconds, maxUnits = 1) + val min = TimeUtils.formatDuration(millis - oneMinute, maxUnits = 1) val sec = (oneMinute - sinceLastTimerUpdate).format(maxUnits = 1) "$min §7$sec" } else TimeUtils.formatDuration(millis) - val next = if (queueFull && (!isSixthVisitorEnabled() || millis < 0)) "§cQueue Full!" else { + val next = if (queueFull && (!isSixthVisitorEnabled() || millis.isNegative())) "§cQueue Full!" else { "Next in §$formatColor$formatDuration$extraSpeed" } val visitorLabel = if (visitorsAmount == 1) "visitor" else "visitors" @@ -173,21 +178,21 @@ class GardenVisitorTimer { fun onWorldChange(event: LorenzWorldChangeEvent) { lastVisitors = -1 GardenAPI.storage?.nextSixthVisitorArrival?.let { - sixthVisitorArrivalTime = it + sixthVisitorArrivalTime = it.asTimeMark() } sixthVisitorReady = false - lastMillis = sixthVisitorArrivalTime - System.currentTimeMillis() + lastMillis = sixthVisitorArrivalTime.timeUntil() } @SubscribeEvent fun onBlockBreak(event: CropClickEvent) { if (!isEnabled()) return - sixthVisitorArrivalTime -= 100 + sixthVisitorArrivalTime -= 100.milliseconds } private fun updateSixthVisitorArrivalTime() { visitorInterval?.let { - sixthVisitorArrivalTime = System.currentTimeMillis() + it + sixthVisitorArrivalTime = SimpleTimeMark.now() + it } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt index e79599cae..eebcc5f06 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt @@ -7,18 +7,28 @@ import kotlin.time.Duration.Companion.milliseconds @JvmInline value class SimpleTimeMark(private val millis: Long) { +// operator fun plus(other: SimpleTimeMark) = +// (millis + other.millis).milliseconds + operator fun minus(other: SimpleTimeMark) = (millis - other.millis).milliseconds operator fun plus(other: Duration) = SimpleTimeMark(millis + other.inWholeMilliseconds) + operator fun minus(other: Duration) = plus(-other) + +// operator fun minus(other: Duration) = +// SimpleTimeMark(millis - other.inWholeMilliseconds) + fun passedSince() = if (millis == 0L) Duration.INFINITE else now() - this fun timeUntil() = -passedSince() fun isInPast() = timeUntil().isNegative() + fun isFarPast() = millis == 0L + override fun toString(): String { if (millis == 0L) return "The Far Past" return Instant.ofEpochMilli(millis).toString() @@ -33,4 +43,4 @@ value class SimpleTimeMark(private val millis: Long) { fun Long.asTimeMark() = SimpleTimeMark(this) fun SkyBlockTime.asTimeMark() = SimpleTimeMark(toMillis()) } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt index 3a32b3de6..5bf6bfcf0 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt @@ -4,6 +4,8 @@ import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import io.github.moulberry.notenoughupdates.util.SkyBlockTime import kotlin.time.Duration +import kotlin.time.DurationUnit +import kotlin.time.toDuration object TimeUtils { private val pattern = @@ -73,8 +75,10 @@ object TimeUtils { return builder.toString().trim() } - // TODO: use kotlin Duration - fun getMillis(string: String) = getMillis_(string.replace("m", "m ").replace(" ", " ").trim()) + @Deprecated("Do no longer use long for time", ReplaceWith("getDuration()")) + fun getMillis(string: String) = getDuration(string).inWholeMilliseconds + + fun getDuration(string: String) = getMillis_(string.replace("m", "m ").replace(" ", " ").trim()) private fun getMillis_(string: String) = pattern.matchMatcher(string.lowercase().trim()) { val years = group("y")?.toLong() ?: 0L @@ -90,10 +94,10 @@ object TimeUtils { millis += days * 24 * 60 * 60 * 1000 millis += (years * 365.25 * 24 * 60 * 60 * 1000).toLong() - millis + millis.toDuration(DurationUnit.MILLISECONDS) } ?: tryAlternativeFormat(string) - private fun tryAlternativeFormat(string: String): Long { + private fun tryAlternativeFormat(string: String): Duration { val split = string.split(":") return when (split.size) { 3 -> { @@ -116,7 +120,7 @@ object TimeUtils { else -> { throw RuntimeException("Invalid format: '$string'") } - }.toLong() + }.toLong().toDuration(DurationUnit.MILLISECONDS) } fun SkyBlockTime.formatted(): String { |