Skip to content

Commit

Permalink
feat: implement on close event
Browse files Browse the repository at this point in the history
  • Loading branch information
DebitCardz committed Jun 28, 2023
1 parent cab4138 commit 0af0aa6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/kotlin/me/tech/mcchestui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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
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)
Expand Down Expand Up @@ -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<Slot>(type.slotsPerRow * rows)

val inventory =
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
internal typealias GUISlotClickEvent = InventoryClickEvent.(player: Player) -> Unit

/**
* Event when a [GUI] is closed by a [Player].
*/
internal typealias GUICloseEvent = InventoryCloseEvent.(player: HumanEntity) -> Unit

0 comments on commit 0af0aa6

Please sign in to comment.