Lazy update

This commit is contained in:
bluefireoly
2020-09-27 00:43:32 +02:00
parent ba914c0455
commit 66a03095b3
2 changed files with 12 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
package net.axay.kspigot.main
import net.axay.kspigot.kotlinextensions.closeIfInitialized
import net.axay.kspigot.runnables.KRunnableHolder
import org.bukkit.plugin.java.JavaPlugin
@@ -18,7 +19,10 @@ import org.bukkit.plugin.java.JavaPlugin
*/
abstract class KSpigot : JavaPlugin() {
val kRunnableHolder = KRunnableHolder()
// lazy properties
private val kRunnableHolderProperty = lazy { KRunnableHolder() }
val kRunnableHolder by kRunnableHolderProperty
/**
* Called when the plugin was loaded
@@ -44,8 +48,12 @@ abstract class KSpigot : JavaPlugin() {
}
final override fun onDisable() {
shutdown()
kRunnableHolder.shutdown()
// avoid unnecessary load of lazy properties
kRunnableHolderProperty.closeIfInitialized()
}
}

View File

@@ -7,7 +7,7 @@ import org.bukkit.Bukkit
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitRunnable
class KRunnableHolder {
class KRunnableHolder : AutoCloseable {
/**
* [BukkitRunnable] for tracking the responsible runnable.
@@ -17,7 +17,7 @@ class KRunnableHolder {
*/
private val runnableEndCallbacks = HashMap<BukkitRunnable, Pair<() -> Unit, Boolean>>()
fun shutdown() {
override fun close() {
runnableEndCallbacks.values.forEach { if (it.second) it.first.invoke() }
runnableEndCallbacks.clear()
}