summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/util/FunctionHelper.kt
blob: ea166528e5290a8d2637147e29fda96314074fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package io.dico.parcels2.util

import io.dico.parcels2.ParcelsPlugin
import kotlinx.coroutines.experimental.*
import org.bukkit.scheduler.BukkitTask
import kotlin.coroutines.experimental.CoroutineContext

@Suppress("NOTHING_TO_INLINE")
class FunctionHelper(val plugin: ParcelsPlugin) {
    val mainThreadDispatcher: MainThreadDispatcher = MainThreadDispatcherImpl()

    fun <T> deferLazilyOnMainThread(block: suspend CoroutineScope.() -> T): Deferred<T> {
        return async(context = mainThreadDispatcher, start = CoroutineStart.LAZY, block = block)
    }

    fun <T> deferUndispatchedOnMainThread(block: suspend CoroutineScope.() -> T): Deferred<T> {
        return async(context = mainThreadDispatcher, start = CoroutineStart.UNDISPATCHED, block = block)
    }

    fun launchLazilyOnMainThread(block: suspend CoroutineScope.() -> Unit): Job {
        return launch(context = mainThreadDispatcher, start = CoroutineStart.LAZY, block = block)
    }

    inline fun schedule(noinline task: () -> Unit) = schedule(0, task)

    fun schedule(delay: Int, task: () -> Unit): BukkitTask {
        return plugin.server.scheduler.runTaskLater(plugin, task, delay.toLong())
    }

    fun scheduleRepeating(delay: Int, interval: Int, task: () -> Unit): BukkitTask {
        return plugin.server.scheduler.runTaskTimer(plugin, task, delay.toLong(), interval.toLong())
    }

    abstract class MainThreadDispatcher : CoroutineDispatcher() {
        abstract val mainThread: Thread
        abstract fun runOnMainThread(task: Runnable)
    }

    private inner class MainThreadDispatcherImpl : MainThreadDispatcher() {
        override val mainThread: Thread = Thread.currentThread()

        override fun dispatch(context: CoroutineContext, block: Runnable) {
            runOnMainThread(block)
        }

        @Suppress("OVERRIDE_BY_INLINE")
        override inline fun runOnMainThread(task: Runnable) {
            if (Thread.currentThread() === mainThread) task.run()
            else plugin.server.scheduler.runTaskLater(plugin, task, 0)
        }
    }

}