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.kt14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt b/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt
index b1d18ab..ce1bf0f 100644
--- a/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt
+++ b/src/main/kotlin/io/dico/parcels2/util/MainThreadDispatcher.kt
@@ -1,11 +1,14 @@
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() {
+abstract class MainThreadDispatcher : CoroutineDispatcher(), Delay {
abstract val mainThread: Thread
abstract fun runOnMainThread(task: Runnable)
}
@@ -27,5 +30,14 @@ fun MainThreadDispatcher(plugin: Plugin): MainThreadDispatcher {
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 = TimeUnit.MILLISECONDS.convert(time, unit)
+ plugin.server.scheduler.runTaskLater(plugin, task, (millis + 25) / 50 - 1)
+ }
}
} \ No newline at end of file