<commit_before>@file:Suppress("RedundantLambdaArrow")

package flavor.pie.kludge

import org.spongepowered.api.scheduler.Task
import java.util.concurrent.TimeUnit
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.resume
import kotlin.coroutines.startCoroutine
import kotlin.coroutines.suspendCoroutine

fun task(fn: suspend TaskCoroutineContext.Companion.() -> Unit) {
    fn.startCoroutine(TaskCoroutineContext.Companion, Continuation(EmptyCoroutineContext) {})
}

fun task(plugin: Any, fn: suspend TaskCoroutineContext.() -> Unit) {
    fn.startCoroutine(TaskCoroutineContext(plugin), Continuation(EmptyCoroutineContext) {})
}

class TaskCoroutineContext(val plugin: Any) {

    companion object {
        suspend fun desync() {
            suspendCoroutine<Unit> { cont ->
                Task.builder().async().execute { ->
                    cont.resume(Unit)
                }.submit(plugin)
            }
        }

        suspend fun resync() {
            if (!Server.isMainThread) {
                suspendCoroutine<Unit> { cont ->
                    Task.builder().execute { ->
                        cont.resume(Unit)
                    }.submit(plugin)
                }
            }
        }

        suspend fun delayTicks(ticks: Long) {
            if (Server.isMainThread) {
                suspendCoroutine<Unit> { cont ->
                    Task.builder().delayTicks(ticks).execute { ->
                        cont.resume(Unit)
                    }.submit(plugin)
                }
            } else {
                suspendCoroutine<Unit> { cont ->
                    Task.builder().async().delayTicks(ticks).execute { ->
                        cont.resume(Unit)
                    }.submit(plugin)
                }
            }
        }

        suspend fun delay(delay: Long, unit: TimeUnit) {
            if (Server.isMainThread) {
                suspendCoroutine<Unit> { cont ->
                    Task.builder().delay(delay, unit).execute { ->
                        cont.resume(Unit)
                    }.submit(plugin)
                }
            } else {
                suspendCoroutine<Unit> { cont ->
                    Task.builder().async().delay(delay, unit).execute { ->
                        cont.resume(Unit)
                    }.submit(plugin)
                }
            }
        }
    }

    suspend fun desync() {
        suspendCoroutine<Unit> { cont ->
            Task.builder().async().execute { ->
                cont.resume(Unit)
            }.submit(plugin)
        }
    }

    suspend fun resync() {
        if (!Server.isMainThread) {
            suspendCoroutine<Unit> { cont ->
                Task.builder().execute { -><commit_msg>These don't actually have to be namespaced<commit_after>@file:Suppress("RedundantLambdaArrow")

package flavor.pie.kludge

import org.spongepowered.api.scheduler.Task
import java.util.concurrent.TimeUnit
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.resume
import kotlin.coroutines.startCoroutine
import kotlin.coroutines.suspendCoroutine
import flavor.pie.kludge.plugin as plugin_

fun task(fn: suspend () -> Unit) {
    fn.startCoroutine(Continuation(EmptyCoroutineContext) {})
}

fun task(plugin: Any, fn: suspend TaskCoroutineContext.() -> Unit) {
    fn.startCoroutine(TaskCoroutineContext(plugin), Continuation(EmptyCoroutineContext) {})
}

suspend fun desync(plugin: Any = plugin_) {
    suspendCoroutine<Unit> { cont ->
        Task.builder().async().execute { ->
            cont.resume(Unit)
        }.submit(plugin)
    }
}

suspend fun resync(plugin: Any = plugin_) {
    if (!Server.isMainThread) {
        suspendCoroutine<Unit> { cont ->
            Task.builder().execute { ->
                cont.resume(Unit)
            }.submit(plugin)
        }
    }
}

suspend fun delayTicks(ticks: Long, plugin: Any = plugin_) {
    if (Server.isMainThread) {
        suspendCoroutine<Unit> { cont ->
            Task.builder().delayTicks(ticks).execute { ->
                cont.resume(Unit)
            }.submit(plugin)
        }
    } else {
        suspendCoroutine<Unit> { cont ->
            Task.builder().async().delayTicks(ticks).execute { ->
                cont.resume(Unit)
            }.submit(plugin)
        }
    }
}

suspend fun delay(delay: Long, unit: TimeUnit, plugin: Any = plugin_) {
    if (Server.isMainThread) {
        suspendCoroutine<Unit> { cont ->
            Task.builder().delay(delay, unit).execute { ->
                cont.resume(Unit)
            }.submit(plugin)
        }
    } else {
        suspendCoroutine<Unit> { cont ->
            Task.builder().async().delay(delay, unit).execute { ->
                cont.resume(Unit)
            }.submit(plugin)
        }
    }
}

class TaskCoroutineContext(private val plugin: Any) {

    suspend fun desync() {
        suspendCoroutine<Unit> { cont ->
            Task.builder().async().execute { ->
                cont.resume(Unit)
            }.submit(plugin)
        }
    }

    suspend fun resync() {
        if (!Server.isMainThread) {
            suspendCoroutine<Unit> { cont ->
                Task.builder().execute { ->
{'fraction_non_alphanumeric': 0.11778115501519756, 'fraction_numerical': 0.0, 'mean_word_length': 1.810998398291511, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7241791', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1499, 'n_words': 239}