Holders are singletons
This commit is contained in:
@@ -3,7 +3,6 @@
|
|||||||
package net.axay.kspigot.inventory
|
package net.axay.kspigot.inventory
|
||||||
|
|
||||||
import net.axay.kspigot.event.listen
|
import net.axay.kspigot.event.listen
|
||||||
import net.axay.kspigot.main.KSpigotMainInstance
|
|
||||||
import org.bukkit.entity.HumanEntity
|
import org.bukkit.entity.HumanEntity
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent
|
import org.bukkit.event.inventory.InventoryClickEvent
|
||||||
import org.bukkit.inventory.Inventory
|
import org.bukkit.inventory.Inventory
|
||||||
@@ -25,7 +24,7 @@ fun HumanEntity.openGUI(gui: InventoryGUI<*>, page: Int? = null): InventoryView?
|
|||||||
|
|
||||||
// GUI HOLDER
|
// GUI HOLDER
|
||||||
|
|
||||||
class InventoryGUIHolder : AutoCloseable {
|
object InventoryGUIHolder : AutoCloseable {
|
||||||
|
|
||||||
private val registered = HashSet<InventoryGUI<ForInventory>>()
|
private val registered = HashSet<InventoryGUI<ForInventory>>()
|
||||||
|
|
||||||
@@ -107,14 +106,14 @@ abstract class InventoryGUI<T : ForInventory>(
|
|||||||
* (KSpigot will listen for actions in the inventory.)
|
* (KSpigot will listen for actions in the inventory.)
|
||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST")
|
@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
|
* Stops KSpigot from listening to actions in this
|
||||||
* InventoryGUI anymore.
|
* InventoryGUI anymore.
|
||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST")
|
@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.
|
* Loads the specified page in order to display it in the GUI.
|
||||||
|
@@ -21,8 +21,8 @@ import org.bukkit.plugin.java.JavaPlugin
|
|||||||
abstract class KSpigot : JavaPlugin() {
|
abstract class KSpigot : JavaPlugin() {
|
||||||
|
|
||||||
// lazy properties
|
// lazy properties
|
||||||
private val kRunnableHolderProperty = lazy { KRunnableHolder() }
|
private val kRunnableHolderProperty = lazy { KRunnableHolder }
|
||||||
private val inventoryGUIHolderProperty = lazy { InventoryGUIHolder() }
|
private val inventoryGUIHolderProperty = lazy { InventoryGUIHolder }
|
||||||
|
|
||||||
internal val kRunnableHolder by kRunnableHolderProperty
|
internal val kRunnableHolder by kRunnableHolderProperty
|
||||||
internal val inventoryGUIHolder by inventoryGUIHolderProperty
|
internal val inventoryGUIHolder by inventoryGUIHolderProperty
|
||||||
|
@@ -6,7 +6,7 @@ import net.axay.kspigot.main.KSpigotMainInstance
|
|||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
import org.bukkit.scheduler.BukkitRunnable
|
import org.bukkit.scheduler.BukkitRunnable
|
||||||
|
|
||||||
class KRunnableHolder : AutoCloseable {
|
internal object KRunnableHolder : AutoCloseable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [BukkitRunnable] for tracking the responsible runnable.
|
* [BukkitRunnable] for tracking the responsible runnable.
|
||||||
@@ -86,16 +86,16 @@ fun task(
|
|||||||
|
|
||||||
if (isCancelled) {
|
if (isCancelled) {
|
||||||
if (safe || ranOut)
|
if (safe || ranOut)
|
||||||
KSpigotMainInstance.kRunnableHolder.activate(this)
|
KRunnableHolder.activate(this)
|
||||||
else
|
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)
|
if (sync)
|
||||||
bukkitRunnable.runTaskTimer(KSpigotMainInstance, delay, period ?: 20)
|
bukkitRunnable.runTaskTimer(KSpigotMainInstance, delay, period ?: 20)
|
||||||
|
Reference in New Issue
Block a user