From eb362deb24304305220885427be276c2ec5434fb Mon Sep 17 00:00:00 2001 From: CalMWolfs Date: Wed, 2 Oct 2024 22:08:34 +1000 Subject: [PATCH] some changes --- .../skyhanni/utils/ComputerTimeOffset.kt | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ComputerTimeOffset.kt b/src/main/java/at/hannibal2/skyhanni/utils/ComputerTimeOffset.kt index 1ac0434d3362..cd09ed96eb08 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ComputerTimeOffset.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ComputerTimeOffset.kt @@ -3,7 +3,6 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.events.DebugDataCollectEvent import at.hannibal2.skyhanni.events.ProfileJoinEvent -import at.hannibal2.skyhanni.events.SecondPassedEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.TimeUtils.format @@ -14,22 +13,19 @@ import java.net.InetAddress import kotlin.concurrent.thread import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds -import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds @SkyHanniModule object ComputerTimeOffset { private var offsetMillis: Duration? = null - private var lastCheckTime = SimpleTimeMark.farPast() - private val config get() = SkyHanniMod.feature.misc.warnAboutPcTimeOffset private val offsetFixLinks by lazy { when { - OSUtils.isWindows -> - @Suppress("ktlint:standard:max-line-length") + OSUtils.isWindows -> { "https://support.microsoft.com/en-us/windows/how-to-set-your-time-and-time-zone-dfaa7122-479f-5b98-2a7b-fa0b6e01b261" + } OSUtils.isLinux -> "https://unix.stackexchange.com/a/79116" OSUtils.isMac -> "https://support.apple.com/guide/mac-help/set-the-date-and-time-automatically-mchlp2996/mac" @@ -38,16 +34,20 @@ object ComputerTimeOffset { } init { - checkOffset() - startMonitoringTimeChanges() + thread { + while (true) { + Thread.sleep(1000) + detectTimeChange() + } + } } private fun checkOffset() { SkyHanniMod.coroutineScope.launch { offsetMillis = getNtpOffset("time.google.com") offsetMillis?.let { - println("SkyHanni detected a time offset of $it.") - } ?: println("SkyHanni failed to detect a time offset.") + tryDisplayOffset() + } } } @@ -59,22 +59,15 @@ object ComputerTimeOffset { timeInfo.computeDetails() timeInfo.offset.milliseconds } catch (e: Exception) { - if (LorenzUtils.inSkyBlock && config) ErrorManager.logErrorWithData(e, "Failed to get NTP offset") + if (LorenzUtils.inSkyBlock && config) ErrorManager.logErrorWithData( + e, "Failed to get NTP offset", + "server" to ntpServer, + ) else e.printStackTrace() null } private var lastSystemTime = System.currentTimeMillis() - private var lastDetectedOffset = 1.seconds - - private fun startMonitoringTimeChanges() { - thread { - while (true) { - Thread.sleep(1000) - detectTimeChange() - } - } - } private fun detectTimeChange() { val currentSystemTime = System.currentTimeMillis() @@ -85,33 +78,31 @@ object ComputerTimeOffset { val deviation = timeDifference - expectedDuration if (deviation.absoluteValue > 1.seconds) { - lastCheckTime = SimpleTimeMark.farPast() + checkOffset() } - lastDetectedOffset = deviation } @SubscribeEvent fun onProfileJoin(event: ProfileJoinEvent) { - if (!config) return + DelayedRun.runDelayed(5.seconds) { + checkOffset() + } + } + + private fun tryDisplayOffset() { + if (!config || !LorenzUtils.onHypixel) return val offsetMillis = offsetMillis ?: return if (offsetMillis.absoluteValue < 5.seconds) return ChatUtils.clickableLinkChat( - "Your computer's clock is off by ${offsetMillis.absoluteValue.format()}. " + - "Please update your time settings. Click here for instructions.", + "Your computer's clock is off by ${offsetMillis.absoluteValue.format()}.\n" + + "§ePlease update your time settings. Many features may not function correctly until you do.\n" + + "§eClick here for instructions on how to fix your clock.", offsetFixLinks ?: return, prefixColor = "§c", ) } - @SubscribeEvent - fun onSecondPassed(event: SecondPassedEvent) { - if (lastCheckTime.passedSince() < 30.minutes) return - - lastCheckTime = SimpleTimeMark.now() - checkOffset() - } - @SubscribeEvent fun onDebugCollect(event: DebugDataCollectEvent) { event.title("Time Offset")