Skip to content

Commit

Permalink
Merge pull request #78 from KingsMMA/cancel-by-default
Browse files Browse the repository at this point in the history
Added the option to use a default click event in GUIs
  • Loading branch information
stephendotgg authored Dec 16, 2024
2 parents dfcaf7c + 9169639 commit 0392266
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/gui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<InventoryClickEvent> {
if (inventory == this@GUI.inventory) slotAction[-1]?.invoke(this)
if (clickedInventory != this@GUI.inventory) return@event
slotAction[slot]?.invoke(this)
}
Expand All @@ -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)
Expand Down

0 comments on commit 0392266

Please sign in to comment.