diff --git a/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt b/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt index acbb9c3..16645ec 100644 --- a/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt +++ b/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt @@ -5,6 +5,7 @@ import net.kyori.adventure.text.Component import org.bukkit.Bukkit import org.bukkit.entity.Player import org.bukkit.event.inventory.InventoryClickEvent +import org.bukkit.event.inventory.InventoryCloseEvent import org.bukkit.event.inventory.InventoryType import org.bukkit.inventory.ItemStack @@ -26,6 +27,7 @@ class GUI(val title: Component, val size: Int, val type: InventoryType, val cont private val keySlot = mutableMapOf>() private val slotAction = mutableMapOf Unit>() + private val onClose = mutableListOf Unit>() lateinit var viewer: Player @@ -35,6 +37,12 @@ class GUI(val title: Component, val size: Int, val type: InventoryType, val cont slotAction[slot]?.invoke(this) } + private val closeEvent = event { + if (inventory != this@GUI.inventory) return@event + onClose.forEach { it.invoke(this) } + remove() + } + fun pattern(vararg pattern: String) { for ((index, value) in pattern.joinToString("").withIndex()) { keySlot.getOrPut(value) { mutableListOf() }.add(index) @@ -48,6 +56,13 @@ class GUI(val title: Component, val size: Int, val type: InventoryType, val cont slotAction[-1] = action } + /** + * Set the action to be executed when the inventory is closed + */ + fun onClose(action: InventoryCloseEvent.() -> Unit) { + onClose.add(action) + } + @JvmName("setSlot") fun set(slot: Int, item: ItemStack, action: InventoryClickEvent.() -> Unit = {}) { inventory.setItem(slot, item) @@ -74,6 +89,7 @@ class GUI(val title: Component, val size: Int, val type: InventoryType, val cont slotAction.clear() inventory.clear() clickEvent.unregister() + closeEvent.unregister() } companion object {