1+ package com.lambda
2+
3+ import com.lambda.context.ClientContext
4+ import com.lambda.event.EventFlow
5+ import com.lambda.context.SafeContext
6+ import kotlinx.coroutines.launch
7+
8+ /* *
9+ * Executes a block of code only if the context is safe. A context is considered safe when all the following properties are not null:
10+ * - [SafeContext.world]
11+ * - [SafeContext.player]
12+ * - [SafeContext.interaction]
13+ * - [SafeContext.connection]
14+ *
15+ * If the context is not safe, the function will return null and the block of code will not be executed.
16+ *
17+ * @param block The block of code to be executed within the safe context.
18+ * @return The result of the block execution if the context is safe, null otherwise.
19+ */
20+ inline fun <T > runSafe (block : SafeContext .() -> T ): T ? {
21+ return ClientContext ().toSafe()?.let { block(it) }
22+ }
23+
24+ /* *
25+ * This function is used to execute a block of code on a new thread running asynchronously to the game thread.
26+ * It should only be used when you need to perform read actions on the game data.
27+ *
28+ * Caution: Using this function to write to the game data can lead to race conditions. Therefore, it is recommended
29+ * to use this function only for read operations to avoid potential concurrency issues.
30+ *
31+ * @param block The block of code to be executed concurrently.
32+ */
33+ inline fun runConcurrent (crossinline block : suspend () -> Unit ) =
34+ EventFlow .lambdaScope.launch {
35+ block()
36+ }
37+
38+ /* *
39+ * This function is used to execute a block of code within a safe context on a new thread running asynchronously to the game thread.
40+ * A context is considered safe when all the following properties are not null:
41+ * - [SafeContext.world]
42+ * - [SafeContext.player]
43+ * - [SafeContext.interaction]
44+ * - [SafeContext.connection]
45+ *
46+ * If the context is not safe, the function will not execute the block of code.
47+ *
48+ * @param block The block of code to be executed within the safe context.
49+ */
50+ inline fun runSafeConcurrent (crossinline block : SafeContext .() -> Unit ) {
51+ EventFlow .lambdaScope.launch {
52+ runSafe { block() }
53+ }
54+ }
55+
56+ /* *
57+ * Executes a given task on the game's main thread.
58+ *
59+ * This function is used when a task needs to be performed on the game's main thread,
60+ * as certain operations are not safe to perform on other threads.
61+ * It uses the Minecraft client's `execute` method to schedule the task.
62+ *
63+ * Note: This function is non-blocking as the task is scheduled to be executed
64+ * on the game's main thread, but does not provide any feedback.
65+ *
66+ * @param block The task to be executed on the game's main thread.
67+ */
68+ inline fun runOnGameThread (crossinline block : () -> Unit ) {
69+ Lambda .mc.executeSync { block() }
70+ }
71+
72+ /* *
73+ * Executes a given task on the game's main thread within a safe context.
74+ * A context is considered safe when all the following properties are not null:
75+ * - [SafeContext.world]
76+ * - [SafeContext.player]
77+ * - [SafeContext.interaction]
78+ * - [SafeContext.connection]
79+ *
80+ * This function is used when a task needs to be performed on the game's main thread,
81+ * as certain operations are not safe to perform on other threads.
82+ * It uses the Minecraft client's `execute` method to schedule the task.
83+ *
84+ * Note: This function is non-blocking as the task is scheduled to be executed
85+ * on the game's main thread, but does not provide any feedback.
86+ *
87+ * @param block The task to be executed on the game's main thread within a safe context.
88+ */
89+ inline fun runSafeOnGameThread (crossinline block : SafeContext .() -> Unit ) {
90+ runOnGameThread { runSafe { block() } }
91+ }
0 commit comments