Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CalMWolfs committed Oct 2, 2024
1 parent ffb1ced commit eb362de
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions src/main/java/at/hannibal2/skyhanni/utils/ComputerTimeOffset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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()
}
}
}

Expand All @@ -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()
Expand All @@ -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")
Expand Down

0 comments on commit eb362de

Please sign in to comment.