blob: 4642124b7003bdfaae6aedde23c887e0d951c9d2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package at.hannibal2.skyhanni.utils
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.CollectionUtils.drainTo
import net.minecraft.client.Minecraft
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.Executor
import kotlin.time.Duration
object DelayedRun {
private val tasks = mutableListOf<Pair<() -> Any, SimpleTimeMark>>()
private val futureTasks = ConcurrentLinkedQueue<Pair<() -> Any, SimpleTimeMark>>()
fun runDelayed(duration: Duration, run: () -> Unit): SimpleTimeMark {
val time = SimpleTimeMark.now() + duration
futureTasks.add(Pair(run, time))
return time
}
/** Runs in the next full Tick so the delay is between 50ms to 100ms**/
fun runNextTick(run: () -> Unit) {
futureTasks.add(Pair(run, SimpleTimeMark.farPast()))
}
fun checkRuns() {
tasks.removeIf { (runnable, time) ->
val inPast = time.isInPast()
if (inPast) {
try {
runnable()
} catch (e: Exception) {
ErrorManager.logErrorWithData(e, "DelayedRun task crashed while executing")
}
}
inPast
}
futureTasks.drainTo(tasks)
}
@JvmField
val onThread = Executor {
val mc = Minecraft.getMinecraft()
if (mc.isCallingFromMinecraftThread) {
it.run()
} else {
Minecraft.getMinecraft().addScheduledTask(it)
}
}
}
|