From 0af0aa6de6495b4f5f7f1712e723b29fd276c9fd Mon Sep 17 00:00:00 2001 From: Tech Date: Wed, 28 Jun 2023 17:37:03 -0400 Subject: [PATCH] feat: implement on close event --- src/main/kotlin/me/tech/mcchestui/GUI.kt | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/me/tech/mcchestui/GUI.kt b/src/main/kotlin/me/tech/mcchestui/GUI.kt index a19663c..b01115f 100644 --- a/src/main/kotlin/me/tech/mcchestui/GUI.kt +++ b/src/main/kotlin/me/tech/mcchestui/GUI.kt @@ -8,6 +8,7 @@ package me.tech.mcchestui import net.kyori.adventure.text.Component import org.bukkit.Material +import org.bukkit.entity.HumanEntity import org.bukkit.entity.Player import org.bukkit.event.* import org.bukkit.event.inventory.InventoryClickEvent @@ -15,6 +16,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent import org.bukkit.event.inventory.InventoryDragEvent import org.bukkit.inventory.ItemStack import org.bukkit.plugin.java.JavaPlugin +import java.time.Duration fun toSlot(x: Int, y: Int, type: GUIType) = x + (y * type.slotsPerRow) fun fromSlot(s: Int, type: GUIType) = Pair(s % type.slotsPerRow, s / type.slotsPerRow) @@ -57,6 +59,11 @@ class GUI( */ var onDragItem: GUIDragItemEvent? = null + /** + * Event called when a [Player] exits a [GUI]. + */ + var onCloseInventory: GUICloseEvent? = null + var slots = arrayOfNulls(type.slotsPerRow * rows) val inventory = @@ -221,7 +228,9 @@ class GUI( ev.isCancelled = slot.cancelled - slot.onClick?.let { event -> event(ev, player) } + slot.onClick?.let { uiEvent -> + uiEvent(ev, player) + } } @EventHandler(priority = EventPriority.HIGH) @@ -277,6 +286,10 @@ class GUI( @EventHandler(priority = EventPriority.MONITOR) internal fun onInventoryClose(ev: InventoryCloseEvent) { + onCloseInventory?.let { uiEvent -> + uiEvent(ev, ev.player) + } + // don't unregister this. if(!automaticallyUnregisterListener) { return @@ -303,4 +316,9 @@ internal typealias GUIDragItemEvent = InventoryDragEvent.(player: Player, items: /** * Event when a [GUI.Slot] is clicked. */ -internal typealias GUISlotClickEvent = InventoryClickEvent.(player: Player) -> Unit \ No newline at end of file +internal typealias GUISlotClickEvent = InventoryClickEvent.(player: Player) -> Unit + +/** + * Event when a [GUI] is closed by a [Player]. + */ +internal typealias GUICloseEvent = InventoryCloseEvent.(player: HumanEntity) -> Unit \ No newline at end of file