Skip to content

Commit ceb142f

Browse files
committed
Fixed rate timer based TickEvent
1 parent 5ce94e9 commit ceb142f

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

common/src/main/kotlin/com/lambda/event/EventFlow.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ package com.lambda.event
1919

2020
import com.lambda.context.SafeContext
2121
import com.lambda.event.callback.ICancellable
22+
import com.lambda.event.events.ClientEvent
2223
import com.lambda.event.listener.Listener
2324
import com.lambda.threading.runConcurrent
2425
import com.lambda.threading.runSafe
2526
import kotlinx.coroutines.*
2627
import kotlinx.coroutines.channels.BufferOverflow
2728
import kotlinx.coroutines.flow.*
29+
import kotlin.concurrent.fixedRateTimer
2830

2931

3032
/**
@@ -76,6 +78,17 @@ object EventFlow {
7678
*/
7779
val concurrentListeners = Subscriber()
7880

81+
init {
82+
fixedRateTimer(
83+
daemon = true,
84+
name = "Scheduler-Lambda-Tick",
85+
initialDelay = 50L,
86+
period = 50L
87+
) {
88+
ClientEvent.FixedTick().post()
89+
}
90+
}
91+
7992
fun Any.unsubscribe() {
8093
syncListeners.unsubscribe(this)
8194
concurrentListeners.unsubscribe(this)

common/src/main/kotlin/com/lambda/event/events/ClientEvent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ sealed class ClientEvent {
4444
* Triggered before playing a sound
4545
*/
4646
data class Sound(val sound: SoundInstance) : ICancellable by Cancellable()
47+
48+
class FixedTick : Event
4749
}

common/src/main/kotlin/com/lambda/event/listener/SafeListener.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.event.Event
2222
import com.lambda.event.EventFlow
2323
import com.lambda.event.Muteable
2424
import com.lambda.threading.runConcurrent
25+
import com.lambda.threading.runGameScheduled
2526
import com.lambda.threading.runSafe
2627
import com.lambda.util.Pointer
2728
import com.lambda.util.selfReference
@@ -121,7 +122,7 @@ class SafeListener<T : Event>(
121122
noinline function: SafeContext.(T) -> Unit = {},
122123
): SafeListener<T> {
123124
val listener = SafeListener<T>(priority, this, alwaysListen) { event ->
124-
function(event)
125+
runGameScheduled { function(event) }
125126
}
126127

127128
EventFlow.syncListeners.subscribe(listener)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.module.modules.debug
19+
20+
import com.lambda.event.events.ClientEvent
21+
import com.lambda.event.listener.SafeListener.Companion.listen
22+
import com.lambda.event.listener.SafeListener.Companion.listenConcurrently
23+
import com.lambda.module.Module
24+
import com.lambda.module.tag.ModuleTag
25+
import com.lambda.util.Communication.info
26+
import com.lambda.util.KeyCode
27+
import net.minecraft.block.Blocks
28+
import net.minecraft.util.math.BlockPos
29+
import java.awt.Color
30+
31+
object TimerTest : Module(
32+
name = "TimerTest",
33+
defaultTags = setOf(ModuleTag.DEBUG)
34+
) {
35+
private var last = 0L
36+
37+
init {
38+
listen<ClientEvent.FixedTick> {
39+
val now = System.currentTimeMillis()
40+
info("${now - last} - Fixed Tick on game thread")
41+
last = now
42+
}
43+
44+
listenConcurrently<ClientEvent.FixedTick> {
45+
// info("${System.currentTimeMillis()} - Fixed Tick Concurrently (but not on mc game thread)")
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)