Skip to content

Commit 531b325

Browse files
committed
Make GUI ticking fixed rate
1 parent ceb142f commit 531b325

File tree

9 files changed

+45
-25
lines changed

9 files changed

+45
-25
lines changed

common/src/main/kotlin/com/lambda/core/TimerManager.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,38 @@ package com.lambda.core
1919

2020
import com.lambda.event.EventFlow.post
2121
import com.lambda.event.events.ClientEvent
22+
import com.lambda.event.events.TickEvent
23+
import com.lambda.event.listener.SafeListener.Companion.listenOnce
24+
import java.util.*
25+
import kotlin.concurrent.fixedRateTimer
2226

2327
object TimerManager : Loadable {
2428
var lastTickLength: Float = 50f
2529

2630
override fun load() = "Loaded Timer Manager"
2731

32+
private const val TICK_DELAY = 50L
33+
private var start = 0L
34+
val fixedTickDelta get() = (System.currentTimeMillis() - start).mod(TICK_DELAY).toDouble() / TICK_DELAY
35+
36+
init {
37+
listenOnce<TickEvent.Pre, Timer> {
38+
start = System.currentTimeMillis()
39+
fixedRateTimer(
40+
daemon = true,
41+
name = "Scheduler-Lambda-Tick",
42+
initialDelay = TICK_DELAY,
43+
period = TICK_DELAY
44+
) {
45+
ClientEvent.FixedTick(this).post()
46+
}
47+
}
48+
}
49+
2850
fun getLength(): Float {
2951
var length = 50f
3052

31-
ClientEvent.Timer(1.0).post {
53+
ClientEvent.TimerUpdate(1.0).post {
3254
length /= speed.toFloat()
3355
}
3456

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ 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
2322
import com.lambda.event.listener.Listener
2423
import com.lambda.threading.runConcurrent
2524
import com.lambda.threading.runSafe
2625
import kotlinx.coroutines.*
2726
import kotlinx.coroutines.channels.BufferOverflow
2827
import kotlinx.coroutines.flow.*
29-
import kotlin.concurrent.fixedRateTimer
28+
import java.util.*
3029

3130

3231
/**
@@ -78,17 +77,6 @@ object EventFlow {
7877
*/
7978
val concurrentListeners = Subscriber()
8079

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-
9280
fun Any.unsubscribe() {
9381
syncListeners.unsubscribe(this)
9482
concurrentListeners.unsubscribe(this)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package com.lambda.event.events
1919

2020
import com.lambda.event.Event
21+
import com.lambda.event.EventFlow
2122
import com.lambda.event.callback.Cancellable
2223
import com.lambda.event.callback.ICancellable
2324
import net.minecraft.client.sound.SoundInstance
25+
import java.util.TimerTask
2426

2527
sealed class ClientEvent {
2628
/**
@@ -38,12 +40,20 @@ sealed class ClientEvent {
3840
*
3941
* @property speed The speed of the timer.
4042
*/
41-
data class Timer(var speed: Double) : Event
43+
data class TimerUpdate(var speed: Double) : Event
4244

4345
/**
4446
* Triggered before playing a sound
4547
*/
4648
data class Sound(val sound: SoundInstance) : ICancellable by Cancellable()
4749

48-
class FixedTick : Event
50+
/**
51+
* Represents a fixed tick event in the application.
52+
*
53+
* A fixed tick can be used to execute a specific task consistently at regular intervals, based on the provided
54+
* timer task. This event is part of the event system and can be subscribed to for handling the specified timer task.
55+
*
56+
* @property timerTask The task that is executed during this fixed tick event.
57+
*/
58+
data class FixedTick(val timerTask: TimerTask) : Event
4959
}

common/src/main/kotlin/com/lambda/graphics/animation/Animation.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
package com.lambda.graphics.animation
1919

20-
import com.lambda.Lambda.mc
21-
import com.lambda.util.extension.partialTicks
20+
import com.lambda.core.TimerManager
2221
import com.lambda.util.math.lerp
2322
import kotlin.math.abs
2423
import kotlin.reflect.KProperty
@@ -28,7 +27,7 @@ class Animation(initialValue: Double, val update: (Double) -> Double) {
2827
private var currValue = initialValue
2928

3029
operator fun getValue(thisRef: Any?, property: KProperty<*>) =
31-
lerp(mc.partialTicks, prevValue, currValue)
30+
lerp(TimerManager.fixedTickDelta, prevValue, currValue)
3231

3332
operator fun setValue(thisRef: Any?, property: KProperty<*>, valueIn: Double) = setValue(valueIn)
3433

common/src/main/kotlin/com/lambda/graphics/renderer/esp/impl/ESPRenderer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.graphics.renderer.esp.impl
1919

2020
import com.lambda.Lambda.mc
21+
import com.lambda.core.TimerManager
2122
import com.lambda.graphics.buffer.VertexPipeline
2223
import com.lambda.graphics.buffer.vertex.attributes.VertexAttrib
2324
import com.lambda.graphics.buffer.vertex.attributes.VertexMode
@@ -47,7 +48,7 @@ abstract class ESPRenderer(tickedMode: Boolean) {
4748

4849
fun render() {
4950
shader.use()
50-
shader["u_TickDelta"] = mc.partialTicks
51+
shader["u_TickDelta"] = TimerManager.fixedTickDelta
5152
shader["u_CameraPosition"] = mc.gameRenderer.camera.pos
5253

5354
withFaceCulling(faces::render)

common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package com.lambda.gui.api
1919

2020
import com.lambda.Lambda.mc
2121
import com.lambda.event.Muteable
22+
import com.lambda.event.events.ClientEvent
2223
import com.lambda.event.events.RenderEvent
23-
import com.lambda.event.events.TickEvent
2424
import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.graphics.animation.AnimationTicker
2626
import com.lambda.gui.api.component.core.IComponent
@@ -56,7 +56,7 @@ abstract class LambdaGui(
5656
onEvent(GuiEvent.Render())
5757
}
5858

59-
listen<TickEvent.Pre> {
59+
listen<ClientEvent.FixedTick> {
6060
animation.tick()
6161
onEvent(GuiEvent.Tick())
6262
}

common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ object Speed : Module(
113113
lastDistance = player.moveDelta
114114
}
115115

116-
listen<ClientEvent.Timer> {
116+
listen<ClientEvent.TimerUpdate> {
117117
if (mode != Mode.NCP_STRAFE) return@listen
118118
if (!shouldWork() || !isInputting) return@listen
119119
it.speed = ncpTimerBoost

common/src/main/kotlin/com/lambda/module/modules/movement/TickShift.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ object TickShift : Module(
101101
}
102102
}
103103

104-
listen<ClientEvent.Timer> {
104+
listen<ClientEvent.TimerUpdate> {
105105
if (!isActive) {
106106
poolPackets()
107107
return@listen

common/src/main/kotlin/com/lambda/module/modules/movement/Timer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object Timer : Module(
3030
private val timer by setting("Timer", 1.0, 0.0..10.0, 0.01)
3131

3232
init {
33-
listen<ClientEvent.Timer> {
33+
listen<ClientEvent.TimerUpdate> {
3434
it.speed = timer.coerceAtLeast(0.05)
3535
}
3636
}

0 commit comments

Comments
 (0)