diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-08-15 12:18:10 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-08-15 12:18:10 +0200 |
commit | fd356d688683176dd45d41bddc0f58be5304ccef (patch) | |
tree | 0a1cc1d29acbfa36d3a9ef47a256d91af9745f02 /src/main/java/at/hannibal2 | |
parent | 75a80664c2034fc4a68a115a1b686676a53949d5 (diff) | |
download | skyhanni-fd356d688683176dd45d41bddc0f58be5304ccef.tar.gz skyhanni-fd356d688683176dd45d41bddc0f58be5304ccef.tar.bz2 skyhanni-fd356d688683176dd45d41bddc0f58be5304ccef.zip |
Using SimpleTimeMark in AccountUpgradeReminder
Diffstat (limited to 'src/main/java/at/hannibal2')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/fame/AccountUpgradeReminder.kt | 36 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt | 6 |
2 files changed, 31 insertions, 11 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/fame/AccountUpgradeReminder.kt b/src/main/java/at/hannibal2/skyhanni/features/fame/AccountUpgradeReminder.kt index 91efb365e..81c76f9f5 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fame/AccountUpgradeReminder.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fame/AccountUpgradeReminder.kt @@ -5,12 +5,25 @@ import at.hannibal2.skyhanni.data.ProfileStorageData import at.hannibal2.skyhanni.events.* import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration +import kotlin.time.Duration.Companion.days +import kotlin.time.Duration.Companion.seconds class AccountUpgradeReminder { private var inInventory = false - private var durationDays = -1 - private var lastReminderSend = 0L + private var duration: Duration? = null + private var lastReminderSend = SimpleTimeMark.farPast() + + // TODO: find a way to save SimpleTimeMark directly in the config + private var nextCompletionTime: SimpleTimeMark? + get() = ProfileStorageData.playerSpecific?.nextAccountUpgradeCompletionTime?.asTimeMark() + set(value) { + ProfileStorageData.playerSpecific?.nextAccountUpgradeCompletionTime = value?.toMillis() + } + @SubscribeEvent fun onTick(event: LorenzTickEvent) { @@ -28,10 +41,11 @@ class AccountUpgradeReminder { if (LorenzUtils.skyBlockArea == "Community Center") return val upgrade = playerSpecific.currentAccountUpgrade ?: return - if (System.currentTimeMillis() <= playerSpecific.nextAccountUpgradeCompletionTime ) return + val nextCompletionTime = nextCompletionTime ?: return + if (!nextCompletionTime.isInPast()) return - if (lastReminderSend + 30_000 > System.currentTimeMillis()) return - lastReminderSend = System.currentTimeMillis() + if (lastReminderSend.passedSince() < 30.seconds) return + lastReminderSend = SimpleTimeMark.now() LorenzUtils.clickableChat( "§e[SkyHanni] The §a$upgrade §eupgrade has completed! §c(Click to disable these reminders)", @@ -56,9 +70,9 @@ class AccountUpgradeReminder { if (!inInventory) return val clickedItemLore = event.slot?.stack?.getLore() ?: return if (clickedItemLore.getOrNull(0) != "§8Account Upgrade") return - durationDays = clickedItemLore.firstNotNullOf { + duration = clickedItemLore.firstNotNullOf { durationRegex.matchEntire(it) - }.groups[1]!!.value.toInt() + }.groups[1]!!.value.toInt().days } @SubscribeEvent @@ -72,23 +86,23 @@ class AccountUpgradeReminder { } private fun startUpgrade(upgrade: String) { - if (durationDays == -1) return + val duration = duration ?: return val playerSpecific = ProfileStorageData.playerSpecific ?: return playerSpecific.currentAccountUpgrade = upgrade - playerSpecific.nextAccountUpgradeCompletionTime = System.currentTimeMillis() + durationDays.toLong() * MILLIS_IN_DAY + + nextCompletionTime = SimpleTimeMark.now() + duration } private fun clearUpgrade() { val playerSpecific = ProfileStorageData.playerSpecific ?: return playerSpecific.currentAccountUpgrade = null - playerSpecific.nextAccountUpgradeCompletionTime = -1L + nextCompletionTime = SimpleTimeMark.farPast() } companion object { private val durationRegex = "§8Duration: (\\d{1,3})d".toRegex() private val startedRegex = "§eYou started the §r§a(.+) §r§eupgrade!".toRegex() private val claimedRegex = "§eYou claimed the §r§a.+ §r§eupgrade!".toRegex() - private const val MILLIS_IN_DAY = 1000 * 60 * 60 * 24L private fun isEnabled() = SkyHanniMod.feature.misc.accountUpgradeReminder diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt index 977a558f4..581585c9a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt @@ -14,13 +14,19 @@ value class SimpleTimeMark(private val millis: Long) { fun passedSince() = if (millis == 0L) Duration.INFINITE else now() - this + fun isInPast() = !passedSince().isNegative() + override fun toString(): String { if (millis == 0L) return "The Far Past" return Instant.ofEpochMilli(millis).toString() } + fun toMillis() = millis + companion object { fun now() = SimpleTimeMark(System.currentTimeMillis()) fun farPast() = SimpleTimeMark(0) + + fun Long.asTimeMark() = SimpleTimeMark(this) } }
\ No newline at end of file |