Skip to content

Commit

Permalink
resolve reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
j10a1n15 committed Sep 30, 2024
1 parent 0359763 commit 72abdd0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,10 @@ public class MiscConfig {
@ConfigEditorBoolean
@FeatureToggle
public boolean userluckEnabled = true;

@Expose
@ConfigOption(name = "Yes please annoy me about pc time offset", desc ="i love annyoing chat warnins yes yes")
@ConfigEditorBoolean
@FeatureToggle
public boolean warnAboutPcTimeOffset = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,34 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.apache.commons.net.ntp.NTPUDPClient
import java.net.InetAddress
import kotlin.math.abs
import kotlin.math.absoluteValue
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit
import kotlin.time.toDuration

@SkyHanniModule
object TimeOffset {
var offsetMillis = 0L
object ComputerTimeOffset {
var offsetMillis: Duration? = null
private set

init {
SkyHanniMod.coroutineScope.launch {
offsetMillis = getNtpOffset("time.google.com") ?: 0
print("Time offset: $offsetMillis")
offsetMillis = getNtpOffset("time.google.com")
offsetMillis?.let {
print("SkyHanni detected a time offset of ${it.format()}.")
} ?: print("SkyHanni failed to detect a time offset.")
}
}

private fun getNtpOffset(ntpServer: String): Long? {
private fun getNtpOffset(ntpServer: String): Duration? {
return try {
val client = NTPUDPClient()
val address = InetAddress.getByName(ntpServer)
val timeInfo = client.getTime(address)

timeInfo.computeDetails()
timeInfo.offset
timeInfo.offset.toDuration(DurationUnit.MILLISECONDS)
} catch (e: Exception) {
e.printStackTrace()
null
Expand All @@ -42,14 +46,17 @@ object TimeOffset {

@SubscribeEvent
fun onProfileJoin(event: ProfileJoinEvent) {
if (abs(offsetMillis) > 10 * 1000) {
val formatted = offsetMillis.toDuration(DurationUnit.MILLISECONDS).format()
ChatUtils.userError("Your computer's clock is off by $formatted. Please update your time settings.")
if (!SkyHanniMod.feature.misc.warnAboutPcTimeOffset) return
offsetMillis?.let {
if (it.absoluteValue > 5.seconds) {
ChatUtils.userError("Your computer's clock is off by ${it.format()}. Please update your time settings.")
}
}
}

@SubscribeEvent
fun onDebugCollect(event: DebugDataCollectEvent) {
offsetMillis?.absoluteValue?.let { if (it < 100.milliseconds) return }
event.title("Time Offset")
event.addData("$offsetMillis ms")
}
Expand Down

0 comments on commit 72abdd0

Please sign in to comment.