diff --git a/uranos-api/src/main/kotlin/space/uranos/Scheduler.kt b/uranos-api/src/main/kotlin/space/uranos/Scheduler.kt index 3cef3a7..1e9f3ab 100644 --- a/uranos-api/src/main/kotlin/space/uranos/Scheduler.kt +++ b/uranos-api/src/main/kotlin/space/uranos/Scheduler.kt @@ -4,6 +4,13 @@ import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.withContext interface Scheduler { + /** + * Only safe to access from the scheduler thread. + * + * Will overflow after about 5 years. + */ + val currentTickNumber: Int + /** * Executes [block] after [delay] ticks passed. * diff --git a/uranos-server/src/main/kotlin/space/uranos/UranosScheduler.kt b/uranos-server/src/main/kotlin/space/uranos/UranosScheduler.kt index caab31b..6a9b19b 100644 --- a/uranos-server/src/main/kotlin/space/uranos/UranosScheduler.kt +++ b/uranos-server/src/main/kotlin/space/uranos/UranosScheduler.kt @@ -21,6 +21,8 @@ class UranosScheduler : Scheduler { val logger = Logger("Scheduler", false) + override var currentTickNumber = Int.MIN_VALUE + inner class Task( val fn: suspend () -> R, val interval: Long?, @@ -29,8 +31,6 @@ class UranosScheduler : Scheduler { @Volatile var cancelled: Boolean = false - val creationStackTrace = Thread.currentThread().stackTrace - override fun cancel() { tasks.remove(this) cancelled = true @@ -68,6 +68,8 @@ class UranosScheduler : Scheduler { val duration = System.currentTimeMillis() - startTime if (duration > interval) logger.warn("Last tick took ${duration}ms, but should only take ${interval}ms") } + + currentTickNumber += 1 }, 0, interval, TimeUnit.MILLISECONDS) } diff --git a/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt b/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt index 200d0d6..d8eaa34 100644 --- a/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt +++ b/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt @@ -128,15 +128,12 @@ class UranosServer internal constructor() : Server() { } private fun startTicking() { - var index = 0 scheduler.executeRepeating(1, 0) { runInServerThread { players.forEach { it.container.tick() } internalEntities.forEach { it.tick() } - sessions.forEach { it.tick(index) } + sessions.forEach { it.tick() } } - - index++ } } diff --git a/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt b/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt index 09d0d61..e0d939a 100644 --- a/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt +++ b/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt @@ -2,6 +2,7 @@ package space.uranos.net import io.netty.buffer.ByteBuf import kotlinx.coroutines.* +import space.uranos.Uranos import space.uranos.UranosServer import space.uranos.chat.ChatColor import space.uranos.chat.ChatComponent @@ -169,8 +170,8 @@ class UranosSession(val channel: io.netty.channel.Channel, val server: UranosSer fun sendKeepAlivePacket() = send(OutgoingKeepAlivePacket(System.currentTimeMillis())) - suspend fun tick(index: Int) { - if (earlyPlayer != null && index % KEEP_ALIVE_PACKET_INTERVAL == 0L) sendKeepAlivePacket() + suspend fun tick() { + if (earlyPlayer != null && Uranos.scheduler.currentTickNumber % KEEP_ALIVE_PACKET_INTERVAL == 0L) sendKeepAlivePacket() packetsAdapter.tick() }