Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion common/src/main/kotlin/com/lambda/core/TimerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
1 change: 1 addition & 0 deletions common/src/main/kotlin/com/lambda/event/EventFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.lambda.threading.runSafe
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
import java.util.*


/**
Expand Down
14 changes: 13 additions & 1 deletion common/src/main/kotlin/com/lambda/event/events/ClientEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -121,7 +122,7 @@ class SafeListener<T : Event>(
noinline function: SafeContext.(T) -> Unit = {},
): SafeListener<T> {
val listener = SafeListener<T>(priority, this, alwaysListen) { event ->
function(event)
runGameScheduled { function(event) }
}

EventFlow.syncListeners.subscribe(listener)
Expand Down
1 change: 1 addition & 0 deletions common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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<ClientEvent.FixedTick> {
val now = System.currentTimeMillis()
info("${now - last} - Fixed Tick on game thread")
last = now
}

listenConcurrently<ClientEvent.FixedTick> {
// info("${System.currentTimeMillis()} - Fixed Tick Concurrently (but not on mc game thread)")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ object Speed : Module(
lastDistance = player.moveDelta
}

listen<ClientEvent.Timer> {
listen<ClientEvent.TimerUpdate> {
if (mode != Mode.NCP_STRAFE) return@listen
if (!shouldWork() || !isInputting) return@listen
it.speed = ncpTimerBoost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object TickShift : Module(
}
}

listen<ClientEvent.Timer> {
listen<ClientEvent.TimerUpdate> {
if (!isActive) {
poolPackets()
return@listen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Timer : Module(
private val timer by setting("Timer", 1.0, 0.0..10.0, 0.01)

init {
listen<ClientEvent.Timer> {
listen<ClientEvent.TimerUpdate> {
it.speed = timer.coerceAtLeast(0.05)
}
}
Expand Down
Loading