-
Notifications
You must be signed in to change notification settings - Fork 6
6. Single Instance GUIs
Single instance simply refers to there only ever needing to be one instance of a GUI stored somewhere that can be accessed at a later date without being unregistered if all players close the GUI.
By default when all viewers of a GUI leave the Bukkit Inventory the GUI will be unregistered and its Bukkit Listener which makes the entire UI function will be unregistered, the GUI should never be re-opened under any circumstance once unregistered as it will cause undefined behavior.
Without a GUI being marked as single instance this code will cease to function.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Hopper
) {
slot(3, 1) {
item = item(Material.STONE) {
name = Component.text("Unsafe Stone")
}
}
}
}
val storedUi = render()
fun openToAll() {
for(player in Bukkit.getOnlinePlayers()) {
player.openGUI(storedUi)
}
}Once all viewers of this GUI close it the GUI will be unregistered and will not be allowed to be opened again.
The simple fix is to add singleInstance = true to our GUI.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Hopper
) {
singleInstance = true
slot(3, 1) {
item = item(Material.STONE) {
name = Component.text("Safe Stone")
}
}
}
}This ensures that even if all the initial viewers of the GUI close the inventory if storedUi our variable storing our GUI is ever needed again it will not be unregistered.
And if we are ever completely done with our GUI we can unregister it ourselves by simply doing.
storedUi.unregister()