-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a mcSyncLaunch extension function
- Loading branch information
Showing
2 changed files
with
36 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/MinecraftSync.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.axay.fabrik.core.task | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* A [CoroutineDispatcher] which executes code synchronously to the | ||
* MinecraftServer main thread. | ||
*/ | ||
lateinit var mcCoroutineDispatcher: CoroutineDispatcher | ||
internal set | ||
|
||
/** | ||
* A [CoroutineScope] using the current MinecraftServer | ||
* as the Dispatcher. | ||
*/ | ||
lateinit var mcCoroutineScope: CoroutineScope | ||
internal set | ||
|
||
/** | ||
* Does the same as [launch], but the dispatcher defaults to [mcCoroutineDispatcher]. | ||
* | ||
* This way, you can execute code synchronously (to the MinecraftServer main thread) | ||
* very easily. | ||
* | ||
* ```kotlin | ||
* coroutineScope { | ||
* mcSyncLaunch { | ||
* // suspending and sync now | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
fun CoroutineScope.mcSyncLaunch(block: suspend CoroutineScope.() -> Unit) = | ||
launch(mcCoroutineDispatcher, block = block) |