mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-13 09:26:11 -04:00
connection ticker: don't recreate tasks
That allows "3rd party" tickers to be registered
This commit is contained in:
parent
afc7cbd579
commit
10c7d682d5
@ -32,6 +32,7 @@ class ConnectionTicker(private val connection: PlayConnection) {
|
|||||||
|
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
|
addDefault()
|
||||||
connection::state.observe(this) {
|
connection::state.observe(this) {
|
||||||
if (it != PlayConnectionStates.PLAYING) {
|
if (it != PlayConnectionStates.PLAYING) {
|
||||||
unregister()
|
unregister()
|
||||||
@ -43,16 +44,7 @@ class ConnectionTicker(private val connection: PlayConnection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun addDefault() {
|
||||||
private fun register() {
|
|
||||||
if (registered) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
lock.lock()
|
|
||||||
if (registered || connection.state != PlayConnectionStates.PLAYING) {
|
|
||||||
lock.unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tasks += RepeatedTask(INTERVAL, maxDelay = MAX_DELAY) {
|
tasks += RepeatedTask(INTERVAL, maxDelay = MAX_DELAY) {
|
||||||
connection.world.entities.tick()
|
connection.world.entities.tick()
|
||||||
}
|
}
|
||||||
@ -77,10 +69,23 @@ class ConnectionTicker(private val connection: PlayConnection) {
|
|||||||
connection.world.time = WorldTime(time + offset, connection.world.time.age + offset)
|
connection.world.time = WorldTime(time + offset, connection.world.time.age + offset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun register() {
|
||||||
|
if (registered) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lock.lock()
|
||||||
|
if (registered || connection.state != PlayConnectionStates.PLAYING) {
|
||||||
|
lock.unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for (task in tasks) {
|
for (task in tasks) {
|
||||||
TaskScheduler += task
|
TaskScheduler += task
|
||||||
}
|
}
|
||||||
|
|
||||||
registered = true
|
registered = true
|
||||||
lock.unlock()
|
lock.unlock()
|
||||||
}
|
}
|
||||||
@ -94,11 +99,22 @@ class ConnectionTicker(private val connection: PlayConnection) {
|
|||||||
for (task in tasks) {
|
for (task in tasks) {
|
||||||
TaskScheduler -= task
|
TaskScheduler -= task
|
||||||
}
|
}
|
||||||
tasks.clear()
|
|
||||||
registered = false
|
registered = false
|
||||||
lock.unlock()
|
lock.unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun register(runnable: Runnable) {
|
||||||
|
lock.lock()
|
||||||
|
val task = RepeatedTask(INTERVAL, maxDelay = MAX_DELAY, runnable = runnable)
|
||||||
|
this.tasks += task
|
||||||
|
if (registered) {
|
||||||
|
TaskScheduler += task
|
||||||
|
}
|
||||||
|
lock.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun plusAssign(runnable: Runnable) = register(runnable)
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val INTERVAL = ProtocolDefinition.TICK_TIME
|
const val INTERVAL = ProtocolDefinition.TICK_TIME
|
||||||
const val MAX_DELAY = INTERVAL / 2
|
const val MAX_DELAY = INTERVAL / 2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user