Class GuiContext

java.lang.Object
io.github.toberocat.guiengine.context.GuiContext
All Implemented Interfaces:
GuiEventListener, GuiEvents

public final class GuiContext extends Object implements GuiEvents, GuiEventListener
The `GuiContext` class represents a GUI context that holds information about the GUI components and actions for a specific player.

This class is licensed under the GNU General Public License.

Since:
04/02/2023
Author:
Tobias Madlberger (Tobias)
  • Constructor Summary

    Constructors
    Constructor
    Description
    GuiContext(@NotNull GuiInterpreter interpreter, @NotNull String title, int width, int height)
    Constructs a new `GuiContext` with the provided interpreter, title, width, and height.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(@NotNull GuiEngineApi api, @NotNull GuiComponent component)
    Adds a GUI component to this context.
    void
    add(@NotNull GuiEngineApi api, @NotNull XmlComponent component)
    Adds an XML component to this context.
    void
    add(@NotNull GuiEngineApi api, @NotNull XmlComponent @NotNull ... components)
    Adds multiple XML components to this context.
    void
    clickedComponent(@NotNull org.bukkit.event.inventory.InventoryClickEvent event)
    Handles the event when a player clicks on a component in the GUI.
    void
    closedComponent(@NotNull org.bukkit.event.inventory.InventoryCloseEvent event)
    Handles the event when a player closes the GUI.
    @NotNull List<GuiComponent>
    Returns the list of GUI components in this context.
    Returns a stream of GUI components in ascending order of their render priority.
    Returns a stream of GUI components in descending order of their render priority.
    void
    draggedComponent(@NotNull org.bukkit.event.inventory.InventoryDragEvent event)
    Handles the event when a player drags an item in the GUI.
    void
    editXmlComponentById(@NotNull GuiEngineApi api, @NotNull String id, @NotNull Consumer<XmlComponent> editCallback)
    Edits an XML component by ID with the specified edit callback.
    boolean
    equals(@Nullable Object o)
    Checks if this `GuiContext` is equal to another object.
    @Nullable GuiComponent
    Finds a GUI component with the specified ID.
    <T extends GuiComponent>
    T
    findComponentById(@NotNull String id, @NotNull Class<T> clazz)
    Finds a GUI component with the specified ID and class type.
    @NotNull GuiContext
    Returns the current `GuiContext`.
    @NotNull UUID
    Returns the unique identifier of this `GuiContext`.
    @NotNull Set<io.github.toberocat.toberocore.action.Action>
    Returns the set of local actions associated with this context.
    int
    Generates the hash code for this `GuiContext`.
    int
    Returns the height of the GUI context.
    Returns the associated `GuiInterpreter` for this context.
    @Nullable org.bukkit.inventory.Inventory
    Returns the inventory associated with this GUI context.
    void
    removeById(@NotNull String id)
    Removes a GUI component with the specified ID.
    void
    Renders the GUI context by updating the inventory with the components' content.
    void
    setInventory(@Nullable org.bukkit.inventory.Inventory inventory)
    Sets the inventory for this GUI context.
    void
    setViewer(@Nullable org.bukkit.entity.Player viewer)
    Sets the viewer (player) for this GUI context.
    @NotNull String
    Returns the title of the GUI context.
    Converts this `GuiContext` object to a human-readable string representation.
    @Nullable org.bukkit.entity.Player
    Returns the viewer (player) associated with this GUI context.
    int
    Returns the width of the GUI context.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface io.github.toberocat.guiengine.event.GuiEventListener

    listen

    Methods inherited from interface io.github.toberocat.guiengine.event.GuiEvents

    onViewInit
  • Constructor Details

    • GuiContext

      public GuiContext(@NotNull @NotNull GuiInterpreter interpreter, @NotNull @NotNull String title, int width, int height)
      Constructs a new `GuiContext` with the provided interpreter, title, width, and height.
      Parameters:
      interpreter - The `GuiInterpreter` associated with this context.
      title - The title of the GUI context.
      width - The width of the GUI context.
      height - The height of the GUI context.
  • Method Details

    • componentsAscending

      @NotNull public @NotNull Stream<GuiComponent> componentsAscending()
      Returns a stream of GUI components in ascending order of their render priority.
      Returns:
      A stream of GUI components in ascending order of their render priority.
    • componentsDescending

      @NotNull public @NotNull Stream<GuiComponent> componentsDescending()
      Returns a stream of GUI components in descending order of their render priority.
      Returns:
      A stream of GUI components in descending order of their render priority.
    • findComponentById

      @Nullable public @Nullable GuiComponent findComponentById(@NotNull @NotNull String id)
      Finds a GUI component with the specified ID.
      Parameters:
      id - The ID of the GUI component to find.
      Returns:
      The GUI component with the specified ID, or null if not found.
    • findComponentById

      @Nullable public <T extends GuiComponent> T findComponentById(@NotNull @NotNull String id, @NotNull @NotNull Class<T> clazz)
      Finds a GUI component with the specified ID and class type.
      Type Parameters:
      T - The type of the GUI component.
      Parameters:
      id - The ID of the GUI component to find.
      clazz - The class type of the GUI component.
      Returns:
      The GUI component with the specified ID and class type, or null if not found or the class type does not match.
    • removeById

      public void removeById(@NotNull @NotNull String id)
      Removes a GUI component with the specified ID.
      Parameters:
      id - The ID of the GUI component to remove.
    • editXmlComponentById

      public void editXmlComponentById(@NotNull @NotNull GuiEngineApi api, @NotNull @NotNull String id, @NotNull @NotNull Consumer<XmlComponent> editCallback)
      Edits an XML component by ID with the specified edit callback.
      Parameters:
      api - The `GuiEngineApi` associated with the XML component.
      id - The ID of the XML component to edit.
      editCallback - The callback function to edit the XML component.
    • add

      public void add(@NotNull @NotNull GuiEngineApi api, @NotNull @NotNull GuiComponent component)
      Adds a GUI component to this context.
      Parameters:
      api - The `GuiEngineApi` associated with the GUI component.
      component - The GUI component to add.
    • add

      public void add(@NotNull @NotNull GuiEngineApi api, @NotNull @NotNull XmlComponent component)
      Adds an XML component to this context.
      Parameters:
      api - The `GuiEngineApi` associated with the XML component.
      component - The XML component to add.
    • add

      public void add(@NotNull @NotNull GuiEngineApi api, @NotNull @NotNull XmlComponent @NotNull ... components)
      Adds multiple XML components to this context.
      Parameters:
      api - The `GuiEngineApi` associated with the XML components.
      components - The XML components to add.
    • clickedComponent

      public void clickedComponent(@NotNull @NotNull org.bukkit.event.inventory.InventoryClickEvent event)
      Handles the event when a player clicks on a component in the GUI.
      Specified by:
      clickedComponent in interface GuiEvents
      Parameters:
      event - The `InventoryClickEvent` representing the click event.
    • draggedComponent

      public void draggedComponent(@NotNull @NotNull org.bukkit.event.inventory.InventoryDragEvent event)
      Handles the event when a player drags an item in the GUI.
      Specified by:
      draggedComponent in interface GuiEvents
      Parameters:
      event - The `InventoryDragEvent` representing the drag event.
    • closedComponent

      public void closedComponent(@NotNull @NotNull org.bukkit.event.inventory.InventoryCloseEvent event)
      Handles the event when a player closes the GUI.
      Specified by:
      closedComponent in interface GuiEvents
      Parameters:
      event - The `InventoryCloseEvent` representing the close event.
    • interpreter

      @NotNull public @NotNull GuiInterpreter interpreter()
      Returns the associated `GuiInterpreter` for this context.
      Returns:
      The associated `GuiInterpreter`.
    • title

      @NotNull public @NotNull String title()
      Returns the title of the GUI context.
      Returns:
      The title of the GUI context.
    • width

      public int width()
      Returns the width of the GUI context.
      Returns:
      The width of the GUI context.
    • height

      public int height()
      Returns the height of the GUI context.
      Returns:
      The height of the GUI context.
    • components

      @NotNull public @NotNull List<GuiComponent> components()
      Returns the list of GUI components in this context.
      Returns:
      The list of GUI components.
    • getLocalActions

      @NotNull public @NotNull Set<io.github.toberocat.toberocore.action.Action> getLocalActions()
      Returns the set of local actions associated with this context.
      Returns:
      The set of local actions.
    • viewer

      @Nullable public @Nullable org.bukkit.entity.Player viewer()
      Returns the viewer (player) associated with this GUI context.
      Returns:
      The viewer (player).
    • inventory

      @Nullable public @Nullable org.bukkit.inventory.Inventory inventory()
      Returns the inventory associated with this GUI context.
      Returns:
      The inventory.
    • setInventory

      public void setInventory(@Nullable @Nullable org.bukkit.inventory.Inventory inventory)
      Sets the inventory for this GUI context.
      Parameters:
      inventory - The inventory to set.
    • setViewer

      public void setViewer(@Nullable @Nullable org.bukkit.entity.Player viewer)
      Sets the viewer (player) for this GUI context.
      Parameters:
      viewer - The viewer (player) to set.
    • render

      public void render()
      Renders the GUI context by updating the inventory with the components' content.
    • equals

      public boolean equals(@Nullable @Nullable Object o)
      Checks if this `GuiContext` is equal to another object.
      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare with this `GuiContext`.
      Returns:
      `true` if the objects are equal, otherwise `false`.
    • hashCode

      public int hashCode()
      Generates the hash code for this `GuiContext`.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code value for this `GuiContext`.
    • toString

      public String toString()
      Converts this `GuiContext` object to a human-readable string representation.
      Overrides:
      toString in class Object
      Returns:
      A string representation of the `GuiContext`.
    • getContextId

      @NotNull public @NotNull UUID getContextId()
      Returns the unique identifier of this `GuiContext`.
      Returns:
      The unique identifier.
    • getContext

      @NotNull public @NotNull GuiContext getContext()
      Returns the current `GuiContext`.
      Specified by:
      getContext in interface GuiEventListener
      Returns:
      The current `GuiContext`.