diff --git a/common/src/main/kotlin/com/lambda/core/TimerManager.kt b/common/src/main/kotlin/com/lambda/core/TimerManager.kt index d370f0f79..ce68852f3 100644 --- a/common/src/main/kotlin/com/lambda/core/TimerManager.kt +++ b/common/src/main/kotlin/com/lambda/core/TimerManager.kt @@ -19,16 +19,33 @@ package com.lambda.core import com.lambda.event.EventFlow.post import com.lambda.event.events.ClientEvent +import kotlin.concurrent.fixedRateTimer object TimerManager : Loadable { var lastTickLength: Float = 50f override fun load() = "Loaded Timer Manager" + private const val TICK_DELAY = 50L + private var start = 0L + val fixedTickDelta get() = (System.currentTimeMillis() - start).mod(TICK_DELAY).toDouble() / TICK_DELAY + + init { + fixedRateTimer( + daemon = true, + name = "Scheduler-Lambda-Tick", + initialDelay = 0, + period = TICK_DELAY + ) { + if (start == 0L) start = System.currentTimeMillis() + ClientEvent.FixedTick(this).post() + } + } + fun getLength(): Float { var length = 50f - ClientEvent.Timer(1.0).post { + ClientEvent.TimerUpdate(1.0).post { length /= speed.toFloat() } diff --git a/common/src/main/kotlin/com/lambda/event/EventFlow.kt b/common/src/main/kotlin/com/lambda/event/EventFlow.kt index 894b6795c..5ee430de6 100644 --- a/common/src/main/kotlin/com/lambda/event/EventFlow.kt +++ b/common/src/main/kotlin/com/lambda/event/EventFlow.kt @@ -25,6 +25,7 @@ import com.lambda.threading.runSafe import kotlinx.coroutines.* import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.* +import java.util.* /** diff --git a/common/src/main/kotlin/com/lambda/event/events/ClientEvent.kt b/common/src/main/kotlin/com/lambda/event/events/ClientEvent.kt index 1468ff092..74047294f 100644 --- a/common/src/main/kotlin/com/lambda/event/events/ClientEvent.kt +++ b/common/src/main/kotlin/com/lambda/event/events/ClientEvent.kt @@ -18,9 +18,11 @@ package com.lambda.event.events import com.lambda.event.Event +import com.lambda.event.EventFlow import com.lambda.event.callback.Cancellable import com.lambda.event.callback.ICancellable import net.minecraft.client.sound.SoundInstance +import java.util.TimerTask sealed class ClientEvent { /** @@ -38,10 +40,20 @@ sealed class ClientEvent { * * @property speed The speed of the timer. */ - data class Timer(var speed: Double) : Event + data class TimerUpdate(var speed: Double) : Event /** * Triggered before playing a sound */ data class Sound(val sound: SoundInstance) : ICancellable by Cancellable() + + /** + * Represents a fixed tick event in the application. + * + * A fixed tick can be used to execute a specific task consistently at regular intervals, based on the provided + * timer task. This event is part of the event system and can be subscribed to for handling the specified timer task. + * + * @property timerTask The task that is executed during this fixed tick event. + */ + data class FixedTick(val timerTask: TimerTask) : Event } diff --git a/common/src/main/kotlin/com/lambda/event/listener/SafeListener.kt b/common/src/main/kotlin/com/lambda/event/listener/SafeListener.kt index 847d6ee1d..f1cf5942e 100644 --- a/common/src/main/kotlin/com/lambda/event/listener/SafeListener.kt +++ b/common/src/main/kotlin/com/lambda/event/listener/SafeListener.kt @@ -22,6 +22,7 @@ import com.lambda.event.Event import com.lambda.event.EventFlow import com.lambda.event.Muteable import com.lambda.threading.runConcurrent +import com.lambda.threading.runGameScheduled import com.lambda.threading.runSafe import com.lambda.util.Pointer import com.lambda.util.selfReference @@ -121,7 +122,7 @@ class SafeListener( noinline function: SafeContext.(T) -> Unit = {}, ): SafeListener { val listener = SafeListener(priority, this, alwaysListen) { event -> - function(event) + runGameScheduled { function(event) } } EventFlow.syncListeners.subscribe(listener) diff --git a/common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt b/common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt index e8f71a52d..21b421cce 100644 --- a/common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt +++ b/common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt @@ -19,6 +19,7 @@ package com.lambda.gui.api import com.lambda.Lambda.mc import com.lambda.event.Muteable +import com.lambda.event.events.ClientEvent import com.lambda.event.events.RenderEvent import com.lambda.event.events.TickEvent import com.lambda.event.listener.SafeListener.Companion.listen diff --git a/common/src/main/kotlin/com/lambda/module/modules/debug/TimerTest.kt b/common/src/main/kotlin/com/lambda/module/modules/debug/TimerTest.kt new file mode 100644 index 000000000..cfbd81e45 --- /dev/null +++ b/common/src/main/kotlin/com/lambda/module/modules/debug/TimerTest.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2025 Lambda + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lambda.module.modules.debug + +import com.lambda.event.events.ClientEvent +import com.lambda.event.listener.SafeListener.Companion.listen +import com.lambda.event.listener.SafeListener.Companion.listenConcurrently +import com.lambda.module.Module +import com.lambda.module.tag.ModuleTag +import com.lambda.util.Communication.info +import com.lambda.util.KeyCode +import net.minecraft.block.Blocks +import net.minecraft.util.math.BlockPos +import java.awt.Color + +object TimerTest : Module( + name = "TimerTest", + defaultTags = setOf(ModuleTag.DEBUG) +) { + private var last = 0L + + init { + listen { + val now = System.currentTimeMillis() + info("${now - last} - Fixed Tick on game thread") + last = now + } + + listenConcurrently { +// info("${System.currentTimeMillis()} - Fixed Tick Concurrently (but not on mc game thread)") + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt index ac7d10121..d08644129 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt @@ -113,7 +113,7 @@ object Speed : Module( lastDistance = player.moveDelta } - listen { + listen { if (mode != Mode.NCP_STRAFE) return@listen if (!shouldWork() || !isInputting) return@listen it.speed = ncpTimerBoost diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/TickShift.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/TickShift.kt index 873d89616..ea9b301bc 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/TickShift.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/TickShift.kt @@ -101,7 +101,7 @@ object TickShift : Module( } } - listen { + listen { if (!isActive) { poolPackets() return@listen diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/Timer.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/Timer.kt index abbd113f1..f71e5f6ff 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/Timer.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/Timer.kt @@ -30,7 +30,7 @@ object Timer : Module( private val timer by setting("Timer", 1.0, 0.0..10.0, 0.01) init { - listen { + listen { it.speed = timer.coerceAtLeast(0.05) } }