ru.astrainteractive.astralibs.async.BukkitMainDispatcher.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-bukkit Show documentation
Show all versions of core-bukkit Show documentation
Core utilities for spigot development
The newest version!
package ru.astrainteractive.astralibs.async
import kotlinx.coroutines.CoroutineDispatcher
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitScheduler
import kotlin.coroutines.CoroutineContext
/**
* Bukkit main dispatcher implementation which will dispatch on [BukkitScheduler.runTask]
*/
class BukkitMainDispatcher(private val plugin: Plugin) : CoroutineDispatcher() {
override fun isDispatchNeeded(context: CoroutineContext): Boolean {
return !plugin.server.isPrimaryThread && plugin.isEnabled
}
override fun dispatch(context: CoroutineContext, block: Runnable) {
if (!plugin.isEnabled) {
return
}
val timedRunnable = context[AsyncComponent.Key]
if (timedRunnable == null) {
plugin.server.scheduler.runTask(plugin, block)
} else {
timedRunnable.queue.add(block)
plugin.server.scheduler.runTask(plugin, timedRunnable)
}
}
}