diff --git a/README.md b/README.md index 3a4a643..a0b35c3 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,9 @@ You can, of course, also implement GUIs for other inventory types: val dropperGui = gui(Component.text("Title"), 9, InventoryType.DROPPER) { // You can also set multiple indexes at once set(listOf(1, 3, 4, 5, 7), ItemStack(Material.GRAY_STAINED_GLASS_PANE)) + + // You can also cancel all clicks that occur while the GUI is open + onClick { isCancelled = true } } player.openInventory(dropperGui) ``` diff --git a/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt b/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt index 407c640..acbb9c3 100644 --- a/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt +++ b/src/main/kotlin/gg/flyte/twilight/gui/GUI.kt @@ -30,6 +30,7 @@ class GUI(val title: Component, val size: Int, val type: InventoryType, val cont lateinit var viewer: Player private val clickEvent = event { + if (inventory == this@GUI.inventory) slotAction[-1]?.invoke(this) if (clickedInventory != this@GUI.inventory) return@event slotAction[slot]?.invoke(this) } @@ -40,6 +41,13 @@ class GUI(val title: Component, val size: Int, val type: InventoryType, val cont } } + /** + * Set the action to be executed when the player clicks on any slot while the GUI is open. + */ + fun onClick(action: InventoryClickEvent.() -> Unit) { + slotAction[-1] = action + } + @JvmName("setSlot") fun set(slot: Int, item: ItemStack, action: InventoryClickEvent.() -> Unit = {}) { inventory.setItem(slot, item)