Class GUI

java.lang.Object
me.block2block.hubparkour.api.gui.GUI

public abstract class GUI extends Object
Represents a graphical user interface (GUI) that can be displayed to a player in a Minecraft environment. This abstract class handles the creation, management, and interaction with GUI items and inventory slots.

This GUI framework relies on an implementing listener that handles inventory clicks, closes and opens.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Player
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    GUI(Player player, String name, int rows, boolean cancelEvent)
    Constructs a new GUI instance with the specified parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    border(String name, List<String> lore)
     
    boolean
    Determines whether events, such as clicks, within the GUI should be cancelled.
    int
    Calculates the total size of the GUI's inventory based on the number of rows.
    boolean
    Checks if the GUI is currently open.
    abstract void
    onClick(int row, int column, ItemStack item, ClickType clickType)
    Abstract method invoked when a click action occurs within the GUI at a specified row and column.
    void
    Opens the GUI for the player associated with this instance, initializing its content based on the provided configuration.
    void
    setItem(int row, int column, GUIItem item)
    Sets a GUIItem at the specified row and column in the GUI.
    void
    updateItem(int row, int column, GUIItem item)
    Updates the item in the specified row and column of the GUI.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • player

      protected final Player player
  • Constructor Details

    • GUI

      public GUI(Player player, String name, int rows, boolean cancelEvent)
      Constructs a new GUI instance with the specified parameters. The GUI is designed to represent a graphical interface for players, comprised of rows, columns, and optional event cancellation behavior. Rows must be within the range of 0 to 5 inclusive.
      Parameters:
      player - The player who will interact with the GUI.
      name - The display name of the GUI.
      rows - The number of rows in the GUI, which must be between 0 and 5 inclusive.
      cancelEvent - Determines whether the click events in the GUI will be cancelled.
      Throws:
      InvalidRowException - If the number of rows is outside the valid range (0 to 5).
  • Method Details

    • setItem

      public void setItem(int row, int column, GUIItem item)
      Sets a GUIItem at the specified row and column in the GUI. The row and column indices must be within their respective valid ranges. This will not update the GUI if it is already open.
      Parameters:
      row - The row index where the item will be placed. Must be between 0 and 5 inclusive.
      column - The column index where the item will be placed. Must be between 0 and 8 inclusive.
      item - The GUIItem to be placed at the specified position. Can be null to clear the slot.
      Throws:
      InvalidRowException - If the row index is not within the range 0-5.
      InvalidColumnException - If the column index is not within the range 0-8.
    • updateItem

      public void updateItem(int row, int column, GUIItem item)
      Updates the item in the specified row and column of the GUI. If the provided item is null, the slot in the inventory is cleared. Otherwise, the item is set in the specified location within the inventory. This will update the GUI if its open.
      Parameters:
      row - The row index to update. Must be between 0 and 5 inclusive.
      column - The column index to update. Must be between 0 and 8 inclusive.
      item - The GUIItem to set at the specified position. If null, the corresponding slot in the inventory will be cleared.
    • open

      public void open()
      Opens the GUI for the player associated with this instance, initializing its content based on the provided configuration. If the player already has a GUI open, it will close the existing one before opening the new GUI.

      The method creates an inventory with dimensions determined by the rows and columns defined in this instance. Each slot of the inventory is populated with items from the internal inventory data structure, retaining the layout specified during the GUI's construction or modification.

      Once the inventory is prepared, it is displayed to the player and registered for tracking in the HubParkourAPI. If the player's open inventory is null after attempting to open, the method exits early.

    • getSize

      public int getSize()
      Calculates the total size of the GUI's inventory based on the number of rows. The size is determined as the product of the rows and 9, where 9 represents the number of columns per row in the GUI.
      Returns:
      The total number of slots in the GUI, calculated as rows * 9.
    • onClick

      public abstract void onClick(int row, int column, ItemStack item, ClickType clickType)
      Abstract method invoked when a click action occurs within the GUI at a specified row and column. This method is intended to handle specific user interactions with items present in the GUI. This is automatically called by the plugin.
      Parameters:
      row - The row index of the clicked slot. Must be between 0 and 5 inclusive.
      column - The column index of the clicked slot. Must be between 0 and 8 inclusive.
      item - The ItemStack present in the clicked slot. Can be null if the slot is empty.
      clickType - The type of click action performed (e.g., left-click, right-click, shift-click).
    • cancelEvent

      public boolean cancelEvent()
      Determines whether events, such as clicks, within the GUI should be cancelled.
      Returns:
      true if GUI events should be cancelled; false otherwise.
    • border

      public void border(String name, List<String> lore)
    • isOpen

      public boolean isOpen()
      Checks if the GUI is currently open.
      Returns:
      true if the GUI is marked as open.