Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,33 @@
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.
*
* Note that GTNHLib is required for this dispatcher.
*
* 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)
}

override fun isDispatchNeeded(context: CoroutineContext): Boolean {
return !ServerThreadUtil.isCallingFromMinecraftThread()
}
}