Holders are singletons

This commit is contained in:
bluefireoly
2020-10-12 17:37:17 +02:00
parent bb1596e95a
commit 25d4b71031
3 changed files with 9 additions and 10 deletions

View File

@@ -3,7 +3,6 @@
package net.axay.kspigot.inventory
import net.axay.kspigot.event.listen
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.Inventory
@@ -25,7 +24,7 @@ fun HumanEntity.openGUI(gui: InventoryGUI<*>, page: Int? = null): InventoryView?
// GUI HOLDER
class InventoryGUIHolder : AutoCloseable {
object InventoryGUIHolder : AutoCloseable {
private val registered = HashSet<InventoryGUI<ForInventory>>()
@@ -107,14 +106,14 @@ abstract class InventoryGUI<T : ForInventory>(
* (KSpigot will listen for actions in the inventory.)
*/
@Suppress("UNCHECKED_CAST")
fun register() = KSpigotMainInstance.inventoryGUIHolder.register(this as InventoryGUI<ForInventory>)
fun register() = InventoryGUIHolder.register(this as InventoryGUI<ForInventory>)
/**
* Stops KSpigot from listening to actions in this
* InventoryGUI anymore.
*/
@Suppress("UNCHECKED_CAST")
fun unregister() = KSpigotMainInstance.inventoryGUIHolder.unregister(this as InventoryGUI<ForInventory>)
fun unregister() = InventoryGUIHolder.unregister(this as InventoryGUI<ForInventory>)
/**
* Loads the specified page in order to display it in the GUI.

View File

@@ -21,8 +21,8 @@ import org.bukkit.plugin.java.JavaPlugin
abstract class KSpigot : JavaPlugin() {
// lazy properties
private val kRunnableHolderProperty = lazy { KRunnableHolder() }
private val inventoryGUIHolderProperty = lazy { InventoryGUIHolder() }
private val kRunnableHolderProperty = lazy { KRunnableHolder }
private val inventoryGUIHolderProperty = lazy { InventoryGUIHolder }
internal val kRunnableHolder by kRunnableHolderProperty
internal val inventoryGUIHolder by inventoryGUIHolderProperty

View File

@@ -6,7 +6,7 @@ import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.Bukkit
import org.bukkit.scheduler.BukkitRunnable
class KRunnableHolder : AutoCloseable {
internal object KRunnableHolder : AutoCloseable {
/**
* [BukkitRunnable] for tracking the responsible runnable.
@@ -86,16 +86,16 @@ fun task(
if (isCancelled) {
if (safe || ranOut)
KSpigotMainInstance.kRunnableHolder.activate(this)
KRunnableHolder.activate(this)
else
KSpigotMainInstance.kRunnableHolder.remove(this)
KRunnableHolder.remove(this)
}
}
}
if (endCallback != null) KSpigotMainInstance.kRunnableHolder.add(bukkitRunnable, endCallback, safe)
if (endCallback != null) KRunnableHolder.add(bukkitRunnable, endCallback, safe)
if (sync)
bukkitRunnable.runTaskTimer(KSpigotMainInstance, delay, period ?: 20)