Eliminated plugin and kSpigot parameters
This commit is contained in:
@@ -2,15 +2,14 @@
|
||||
|
||||
package net.axay.kspigot.runnables
|
||||
|
||||
import net.axay.kspigot.main.KSpigotMainInstance
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.plugin.Plugin
|
||||
|
||||
/*
|
||||
* Chainable bukkit runnable.
|
||||
*/
|
||||
|
||||
class ChainedRunnablePart<T, R>(
|
||||
val plugin: Plugin,
|
||||
val runnable: (T?) -> R,
|
||||
val sync: Boolean,
|
||||
var previous: ChainedRunnablePart<*, T>? = null,
|
||||
@@ -27,22 +26,22 @@ class ChainedRunnablePart<T, R>(
|
||||
next?.start(result)
|
||||
}
|
||||
if (sync)
|
||||
Bukkit.getScheduler().runTask(plugin, realRunnable)
|
||||
Bukkit.getScheduler().runTask(KSpigotMainInstance, realRunnable)
|
||||
else
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, realRunnable)
|
||||
Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, realRunnable)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// FIRST
|
||||
fun <R> Plugin.firstDo(sync: Boolean, runnable: (Unit?) -> R)
|
||||
= ChainedRunnablePart<Unit, R>(this, runnable, sync)
|
||||
fun <R> Plugin.firstSync(runnable: (Unit?) -> R) = firstDo(true, runnable)
|
||||
fun <R> Plugin.firstAsync(runnable: (Unit?) -> R) = firstDo(false, runnable)
|
||||
fun <R> firstDo(sync: Boolean, runnable: (Unit?) -> R)
|
||||
= ChainedRunnablePart<Unit, R>(runnable, sync)
|
||||
fun <R> firstSync(runnable: (Unit?) -> R) = firstDo(true, runnable)
|
||||
fun <R> firstAsync(runnable: (Unit?) -> R) = firstDo(false, runnable)
|
||||
|
||||
// THEN
|
||||
fun <T, R, U> ChainedRunnablePart<T, R>.thenDo(sync: Boolean, runnable: (R?) -> U): ChainedRunnablePart<R, U> {
|
||||
ChainedRunnablePart<R, U>(plugin, runnable, sync).apply {
|
||||
ChainedRunnablePart<R, U>(runnable, sync).apply {
|
||||
previous = this@thenDo
|
||||
this@thenDo.next = this
|
||||
return this
|
||||
|
@@ -2,9 +2,8 @@
|
||||
|
||||
package net.axay.kspigot.runnables
|
||||
|
||||
import net.axay.kspigot.main.KSpigot
|
||||
import net.axay.kspigot.main.KSpigotMainInstance
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.plugin.Plugin
|
||||
import org.bukkit.scheduler.BukkitRunnable
|
||||
|
||||
class KRunnableHolder : AutoCloseable {
|
||||
@@ -49,7 +48,7 @@ abstract class KSpigotRunnable(
|
||||
* @param endCallback code that should always be executed when the runnable ends
|
||||
* @param runnable the runnable which should be executed each repetition
|
||||
*/
|
||||
fun KSpigot.task(
|
||||
fun task(
|
||||
sync: Boolean = true,
|
||||
delay: Long = 0,
|
||||
period: Long? = null,
|
||||
@@ -87,32 +86,32 @@ fun KSpigot.task(
|
||||
|
||||
if (isCancelled) {
|
||||
if (safe || ranOut)
|
||||
kRunnableHolder.activate(this)
|
||||
KSpigotMainInstance.kRunnableHolder.activate(this)
|
||||
else
|
||||
kRunnableHolder.remove(this)
|
||||
KSpigotMainInstance.kRunnableHolder.remove(this)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (endCallback != null) kRunnableHolder.add(bukkitRunnable, endCallback, safe)
|
||||
if (endCallback != null) KSpigotMainInstance.kRunnableHolder.add(bukkitRunnable, endCallback, safe)
|
||||
|
||||
if (sync)
|
||||
bukkitRunnable.runTaskTimer(this, delay, period ?: 20)
|
||||
bukkitRunnable.runTaskTimer(KSpigotMainInstance, delay, period ?: 20)
|
||||
else
|
||||
bukkitRunnable.runTaskTimerAsynchronously(this, delay, period ?: 20)
|
||||
bukkitRunnable.runTaskTimerAsynchronously(KSpigotMainInstance, delay, period ?: 20)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a synchronous task.
|
||||
*/
|
||||
fun Plugin.sync(runnable: () -> Unit)
|
||||
= Bukkit.getScheduler().runTask(this, runnable)
|
||||
fun sync(runnable: () -> Unit)
|
||||
= Bukkit.getScheduler().runTask(KSpigotMainInstance, runnable)
|
||||
|
||||
/**
|
||||
* Starts an asynchronous task.
|
||||
*/
|
||||
fun Plugin.async(runnable: () -> Unit)
|
||||
= Bukkit.getScheduler().runTaskAsynchronously(this, runnable)
|
||||
fun async(runnable: () -> Unit)
|
||||
= Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, runnable)
|
Reference in New Issue
Block a user