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
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ configurations {
}

dependencies {
compileOnly("com.github.GTNewHorizons:GTNHLib:0.6.36:dev")
shadowImplementation("org.jetbrains.kotlin:kotlin-stdlib:2.1.10")
shadowImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.1.10")
shadowImplementation("org.jetbrains.kotlin:kotlin-reflect:2.1.10")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.shadowfacts.forgelin.coroutines

import com.gtnewhorizon.gtnhlib.util.ServerThreadUtil
import cpw.mods.fml.common.Loader
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Runnable
import kotlin.coroutines.CoroutineContext

internal val isGTNHLibLoaded by lazy { Loader.isModLoaded("gtnhlib") }

/**
* The dispatcher that runs the block in the server main thread.
*
* It's based on the implementation of GTNHLib in [ServerThreadUtil]. Thus GTNHLib is required for this dispatcher.
* The coroutine is invoked immediately if the caller is on the server thread; otherwise, it's invoked in the next tick before the pre-tick phase.
*
* Also be aware that if you dispatch coroutines when the server isn't running, it throws [IllegalStateException].
* For the details when the dispatcher is ready, see [ServerThreadUtil].
*/
@Suppress("UnusedReceiverParameter", "unused") // use the receiver to prevent polluting the top-level functions
val Dispatchers.MinecraftServer: CoroutineDispatcher
get() = if(isGTNHLibLoaded) MinecraftServerDispatcher
else error("GTNHLib is required for Dispatchers.MinecraftServer.")

internal object MinecraftServerDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
ServerThreadUtil.addScheduledTask(block)
}
}