Skip to content

Commit

Permalink
monitor time changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hannibal002 committed Oct 1, 2024
1 parent 9f3f464 commit bdbde75
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/utils/ComputerTimeOffset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.launch
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.apache.commons.net.ntp.NTPUDPClient
import java.net.InetAddress
import kotlin.concurrent.thread
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
Expand All @@ -22,6 +23,8 @@ object ComputerTimeOffset {

private var lastCheckTime = SimpleTimeMark.farPast()

private val config get() = SkyHanniMod.feature.misc.warnAboutPcTimeOffset

private val offsetFixLinks by lazy {
when {
OSUtils.isWindows ->
Expand All @@ -35,6 +38,7 @@ object ComputerTimeOffset {

init {
checkOffset()
startMonitoringTimeChanges()
}

private fun checkOffset() {
Expand All @@ -58,9 +62,35 @@ object ComputerTimeOffset {
null
}

var lastSystemTime = System.currentTimeMillis()
var lastDetectedOffset = 1.seconds

private fun startMonitoringTimeChanges() {
thread(start = true) {
while (true) {
Thread.sleep(1000)
detectTimeChange()
}
}
}

private fun detectTimeChange() {
val currentSystemTime = System.currentTimeMillis()
val timeDifference = (currentSystemTime - lastSystemTime).milliseconds
lastSystemTime = currentSystemTime

val expectedDuration = 1.seconds
val deviation = timeDifference - expectedDuration

if (deviation.absoluteValue > 100.milliseconds && config) {
ChatUtils.chat("Offset changed from ${lastDetectedOffset.format()} to ${deviation.format()}")
}
lastDetectedOffset = deviation
}

@SubscribeEvent
fun onProfileJoin(event: ProfileJoinEvent) {
if (!SkyHanniMod.feature.misc.warnAboutPcTimeOffset) return
if (!config) return
val offsetMillis = offsetMillis ?: return
if (offsetMillis.absoluteValue < 5.seconds) return

Expand All @@ -73,7 +103,7 @@ object ComputerTimeOffset {

@SubscribeEvent
fun onSecondPassed(event: SecondPassedEvent) {
if (lastCheckTime.passedSince() > 30.minutes) return
if (lastCheckTime.passedSince() < 30.minutes) return

lastCheckTime = SimpleTimeMark.now()
checkOffset()
Expand Down

0 comments on commit bdbde75

Please sign in to comment.