Gui

class Gui

A class representing a GUI in the Colosseum plugin. This class is responsible for managing the inventory, handling clicks and drags, and refreshing the items in the GUI when necessary.

The GUI is built using a structure of AbstractItemProviders, which are responsible for providing the items to display in each slot of the inventory and handling clicks on those items. The GUI also supports virtual inventories.

To create a GUI, use the Builder class to define the structure of the GUI, set up item bindings and virtual inventory bindings, and then call buildAndOpen() to create and open the GUI for a player.

Parameters

title

The title of the GUI. This will be displayed at the top of the inventory when the GUI is open. If null, the default inventory title will be used.

size

The number of rows in the GUI. Each row contains 9 slots, so the total number of slots in the GUI will be size * 9. The maximum size is 6 rows

player

The player for whom the GUI will be opened. This is required to open the inventory and handle click events.

structure

An array of AbstractItemProviders that defines the items to display in each slot

virtualInventories

A map of virtual inventory IDs to their corresponding VirtualInventoryData, which contains the VirtualInventory instance and the list of providers associated with that virtual inventory. This is used to manage virtual inventories within the GUI and handle scrolling and refreshing of those inventories.

plugin

The instance of the ColosseumPlugin, used for registering listeners and scheduling tasks related to the GUI.

allowClose

A boolean flag that determines whether the GUI can be closed by the player.

Types

Link copied to clipboard
class Builder(val plugin: ColosseumPlugin)

A builder class for constructing a Gui instance. This class provides a fluent API for defining the structure of the GUI, setting up item bindings and virtual inventory bindings, and configuring other properties of the GUI before building and opening it for a player.

Link copied to clipboard
object Companion
Link copied to clipboard

A data class representing the data associated with a virtual inventory within the GUI.

Properties

Link copied to clipboard
val inventory: @NotNull Inventory

The inventory object representing the GUI. This is the actual Bukkit inventory that is opened for the player and contains the items displayed in the GUI. The inventory is created based on the specified size and title, and it is populated with items from the structure of AbstractItemProviders when the GUI is initialized. The inventory is also updated whenever providers are refreshed or when virtual inventories are scrolled.

Link copied to clipboard
val player: Player
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun canScroll(inventoryId: Int, scrollAmount: Int): Boolean

Checks if a virtual inventory can be scrolled by the specified amount. This method is used to prevent scrolling beyond the bounds of the virtual inventory, which could cause errors or unexpected behavior.

Link copied to clipboard
fun close()

Closes the GUI for the player by closing the inventory, unregistering the GUI listener, destroying any providers in the structure, and unregistering the GUI from any virtual inventories. This method is called when the player closes the inventory or when allowClose is true and the player attempts to close the GUI. It ensures that all resources associated with the GUI are properly cleaned up to prevent memory leaks or unintended behavior after the GUI is closed.

Link copied to clipboard
fun handleInventoryScroll(inventoryId: Int, scrollAmount: Int)

Handles scrolling for a virtual inventory. This method is called when a ScrollItemProvider is clicked, and it updates the slot of each provider in the virtual inventory by the specified scroll amount. It also refreshes the items in the GUI that are associated with the virtual inventory.

Link copied to clipboard
open fun onClick(clickEvent: InventoryClickEvent)

Handles click events in the inventory. When a slot is clicked, this method checks if the slot corresponds to a provider in the structure and calls the onClick() method of that provider. If the provider is a VirtualInventoryItemProvider, it creates a VirtualInventoryInteractEvent and passes it to the virtual inventory's handleClick() method. This allows for proper handling of clicks on both regular items and virtual inventory items within the GUI.

Link copied to clipboard
open fun onDrag(event: InventoryDragEvent)

Handles drag events in the inventory. Drag events can only involve a single virtual inventory, and if the drag involves any slots that are not associated with a virtual inventory, the event is cancelled.

Link copied to clipboard
fun open()

Opens the GUI for the player by registering the GUI listener and opening the inventory. This method is called when the GUI is initialized, and it ensures that the player can interact with the GUI and that click and drag events are properly handled by the listener.

Link copied to clipboard

Refreshes all items in the GUI by calling the getItem() method on each provider in the structure and updating the corresponding slot in the inventory.

Link copied to clipboard
fun refreshIndex(index: Int)

Refreshes a specific index in the GUI by calling the getItem() method on the provider at that index in the structure and updating the corresponding slot in the inventory.

Link copied to clipboard
fun refreshVirtualInventory(inventoryId: Int)

Refreshes all providers associated with a virtual inventory. This method is used to update the items displayed in the GUI for a virtual inventory, typically after scrolling or when the contents of the virtual inventory have changed. It calls the refresh() method on each provider associated with the specified virtual inventory, which will update the items in the GUI accordingly.

Link copied to clipboard
fun refreshVirtualInventorySlot(inventoryId: Int, slot: Int)

Refreshes a specific slot for a virtual inventory. This method is used to update a single slot in the GUI that is associated with a virtual inventory, typically after scrolling or when the contents of the virtual inventory have changed. It calls the refresh() method on the provider associated with the specified slot in the virtual inventory, which will update the item in the GUI for that slot accordingly.

Link copied to clipboard
fun virtualInvScrollingWindow(inventoryId: Int): Pair<Int, Int>?

Calculates the current scrolling window for a virtual inventory. This method returns a pair of integers representing the first and last slot indices of the virtual inventory that are currently being displayed in the GUI. This is used to determine which items from the virtual inventory should be shown based on the current scroll position.