Improve code style

This commit is contained in:
Jakob K
2021-05-12 14:17:44 +02:00
parent 3e9b243d3d
commit cf48510756
69 changed files with 144 additions and 609 deletions

View File

@@ -5,11 +5,9 @@ package net.axay.kspigot.runnables
import kotlin.reflect.KClass
abstract class ChainedRunnablePart<T, R>(
val sync: Boolean
val sync: Boolean,
) {
var next: ChainedRunnablePart<R, *>? = null
protected abstract fun invoke(data: T): R
/**
@@ -27,7 +25,7 @@ abstract class ChainedRunnablePart<T, R>(
*/
inline fun <reified E : Exception> executeCatching(
exceptionSync: Boolean = true,
noinline exceptionHandler: ((E) -> Unit)? = null
noinline exceptionHandler: ((E) -> Unit)? = null,
) {
executeCatchingImpl(E::class, exceptionSync, exceptionHandler)
}
@@ -74,42 +72,35 @@ abstract class ChainedRunnablePart<T, R>(
next?.startCatching(result, exceptionClass, exceptionSync, exceptionHandler)
}
}
}
class ChainedRunnablePartFirst<R>(
val runnable: () -> R,
sync: Boolean
sync: Boolean,
) : ChainedRunnablePart<Unit, R>(sync) {
override fun execute() = start(Unit)
override fun <E : Exception> executeCatchingImpl(
exceptionClass: KClass<E>,
exceptionSync: Boolean,
exceptionHandler: ((E) -> Unit)?
exceptionHandler: ((E) -> Unit)?,
) = startCatching(Unit, exceptionClass, exceptionSync, exceptionHandler)
override fun invoke(data: Unit) = runnable.invoke()
}
class ChainedRunnablePartThen<T, R>(
val runnable: (T) -> R,
sync: Boolean,
val previous: ChainedRunnablePart<*, T>
val previous: ChainedRunnablePart<*, T>,
) : ChainedRunnablePart<T, R>(sync) {
override fun execute() = previous.execute()
override fun <E : Exception> executeCatchingImpl(
exceptionClass: KClass<E>,
exceptionSync: Boolean,
exceptionHandler: ((E) -> Unit)?
exceptionHandler: ((E) -> Unit)?,
) = previous.executeCatchingImpl(exceptionClass, exceptionSync, exceptionHandler)
override fun invoke(data: T) = runnable.invoke(data)
}
// FIRST

View File

@@ -7,7 +7,6 @@ import org.bukkit.Bukkit
import org.bukkit.scheduler.BukkitRunnable
internal object KRunnableHolder : AutoCloseable {
/**
* [BukkitRunnable] for tracking the responsible runnable.
* [Pair] of callback for the endCallback code and [Boolean]
@@ -15,7 +14,6 @@ internal object KRunnableHolder : AutoCloseable {
* or not.
*/
private val runnableEndCallbacks = HashMap<BukkitRunnable, Pair<() -> Unit, Boolean>>()
override fun close() {
runnableEndCallbacks.values.forEach { if (it.second) it.first.invoke() }
runnableEndCallbacks.clear()
@@ -26,13 +24,12 @@ internal object KRunnableHolder : AutoCloseable {
fun remove(runnable: BukkitRunnable) = runnableEndCallbacks.remove(runnable)
fun activate(runnable: BukkitRunnable) = runnableEndCallbacks.remove(runnable)?.first?.invoke()
}
abstract class KSpigotRunnable(
var counterUp: Long? = null,
var counterDownToOne: Long? = null,
var counterDownToZero: Long? = null
var counterDownToZero: Long? = null,
) : BukkitRunnable()
/**
@@ -56,20 +53,14 @@ fun task(
howOften: Long? = null,
safe: Boolean = false,
endCallback: (() -> Unit)? = null,
runnable: ((KSpigotRunnable) -> Unit)? = null
runnable: ((KSpigotRunnable) -> Unit)? = null,
): KSpigotRunnable? {
if (howOften != null && howOften == 0L) return null
val bukkitRunnable = object : KSpigotRunnable() {
private var curCounter = 0L
override fun run() {
var ranOut = false
if (howOften != null) {
counterDownToOne = howOften - curCounter
counterDownToZero = counterDownToOne?.minus(1)
@@ -78,7 +69,6 @@ fun task(
ranOut = true
counterUp = curCounter
}
runnable?.invoke(this)
@@ -91,9 +81,7 @@ fun task(
else
KRunnableHolder.remove(this)
}
}
}
if (endCallback != null) KRunnableHolder.add(bukkitRunnable, endCallback, safe)
@@ -106,7 +94,6 @@ fun task(
else bukkitRunnable.runTaskLaterAsynchronously(KSpigotMainInstance, delay)
return bukkitRunnable
}
/**