-
Notifications
You must be signed in to change notification settings - Fork 6
3. Event Handlers
The InventoryDragEvent similarly known as GUIDragItemEvent is currently an experimental feature that is not recommended for production use, it is still in a semi-stable state however, it is still not recommended.
You may use the feature by initializing the onDragItem property.
onDragItem = { player, items -> false }You are allowed to toggle the follows event handler settings in the GUI:
- allowItemPlacement
- allowItemPickup
- allowItemDrag (Experimental)
- allowHotBarSwap
Within your UIs you may sometimes want to allow players to place an ItemStack into the GUI.
The GUI allows for us to simply assign allowItemPlacement to be true so players can place ItemStack's into the GUI.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Chest(rows = 3)
) {
allowItemPlacement = true
}
}Now that players are allowed to place items into empty slots of the UI we can check for those placements by creating a GUIItemPlaceEvent listener as follows.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Chest(rows = 3)
) {
allowItemPlacement = true
onPlaceItem = { player, item, slot ->
/* Listener code. */
false
}
}
}We are given some standard properties on this lambda function such as player, item and slot. But the lambda is also referencing the InventoryClickEvent so we can use any properties from that event as well in it.
You may also see that we have to return false in our lambda, this simply signifies that the event should not be cancelled. If we instead return true the action will be cancelled.
Within your UIs you may sometimes want to allow players to pickup items from the GUI.
The GUI allows for us to simply assign allowItemPickup to be true so players pickup items from the GUI.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Chest(rows = 3)
) {
allowItemPickup = true
}
}Now that players are allowed to pickup items from the GUI we can check for those pickups by creating a GUIItemPickupEvent listener as follows.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Chest(rows = 3)
) {
allowItemPickup = true
onPickupItem = { player, item, slot ->
/* Listener code. */
false
}
}
}You may also see that we have to return false in our lambda, this simply signifies that the event should not be cancelled. If we instead return true the action will be cancelled.
Picking up items however, has some special properties compared to placing items into the UI, by default players are not allowed to pickup the GUIComponent of a GUISlot however, we are allowed to make a GUISlot therefore the GUIComponent able to be picked up.
However, we are allowed to make a GUISlot able to be picked up.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Chest(rows = 3)
) {
allowItemPickup = true
slot(1, 1) {
item = item(Material.STONE)
allowPickup = true
}
onPickupItem = { player, item, slot ->
/* Listener code. */
false
}
}
}Simply adding allowPickup to our GUISlot will allow for it to be picked up from the GUI.
We can easily preform an action once a player leaves an inventory by assigning a listener.
fun render(): GUI {
return gui(
plugin = instance,
title = Component.text("Rendered UI"),
type = GUIType.Chest(rows = 3)
) {
onCloseInventory = { player ->
/* Listener code. */
}
}
}There is no way to cancel an inventory from being closed so you will need to re-open the GUI for the player in order to prevent it from being closed if your situation requires it.