summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt')
-rw-r--r--src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt84
1 files changed, 42 insertions, 42 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt b/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt
index 3eb2e81..3904026 100644
--- a/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt
+++ b/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt
@@ -1,43 +1,43 @@
-package io.dico.parcels2.util
-
-import kotlinx.coroutines.CancellableContinuation
-import kotlinx.coroutines.CoroutineDispatcher
-import kotlinx.coroutines.Delay
-import kotlinx.coroutines.Runnable
-import kotlinx.coroutines.timeunit.TimeUnit
-import org.bukkit.plugin.Plugin
-import kotlin.coroutines.CoroutineContext
-
-abstract class MainThreadDispatcher : CoroutineDispatcher(), Delay {
- abstract val mainThread: Thread
- abstract fun runOnMainThread(task: Runnable)
-}
-
-@Suppress("FunctionName")
-fun MainThreadDispatcher(plugin: Plugin): MainThreadDispatcher {
- return object : MainThreadDispatcher() {
- override val mainThread: Thread = Thread.currentThread()
-
- override fun dispatch(context: CoroutineContext, block: Runnable) {
- doDispatch(block)
- }
-
- override fun runOnMainThread(task: Runnable) {
- doDispatch(task)
- }
-
- private fun doDispatch(task: Runnable) {
- if (Thread.currentThread() === mainThread) task.run()
- else plugin.server.scheduler.runTaskLater(plugin, task, 0)
- }
-
- override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) {
- val task = Runnable {
- with (continuation) { resumeUndispatched(Unit) }
- }
-
- val millis = unit.toMillis(time)
- plugin.server.scheduler.runTaskLater(plugin, task, (millis + 25) / 50 - 1)
- }
- }
+package io.dico.parcels2.util
+
+import kotlinx.coroutines.CancellableContinuation
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.Delay
+import kotlinx.coroutines.Runnable
+import kotlinx.coroutines.timeunit.TimeUnit
+import org.bukkit.plugin.Plugin
+import kotlin.coroutines.CoroutineContext
+
+abstract class MainThreadDispatcher : CoroutineDispatcher(), Delay {
+ abstract val mainThread: Thread
+ abstract fun runOnMainThread(task: Runnable)
+}
+
+@Suppress("FunctionName")
+fun MainThreadDispatcher(plugin: Plugin): MainThreadDispatcher {
+ return object : MainThreadDispatcher() {
+ override val mainThread: Thread = Thread.currentThread()
+
+ override fun dispatch(context: CoroutineContext, block: Runnable) {
+ doDispatch(block)
+ }
+
+ override fun runOnMainThread(task: Runnable) {
+ doDispatch(task)
+ }
+
+ private fun doDispatch(task: Runnable) {
+ if (Thread.currentThread() === mainThread) task.run()
+ else plugin.server.scheduler.runTaskLater(plugin, task, 0)
+ }
+
+ override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) {
+ val task = Runnable {
+ with (continuation) { resumeUndispatched(Unit) }
+ }
+
+ val millis = unit.toMillis(time)
+ plugin.server.scheduler.runTaskLater(plugin, task, (millis + 25) / 50 - 1)
+ }
+ }
} \ No newline at end of file