Class Gui

java.lang.Object
me.despical.inventoryframework.Gui
All Implemented Interfaces:
org.bukkit.inventory.InventoryHolder

public class Gui extends Object implements org.bukkit.inventory.InventoryHolder
The base class of all GUIs
Since:
1.0.1

Created at 04.09.2020

Author:
Despical
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The gui state
  • Constructor Summary

    Constructors
    Constructor
    Description
    Gui(@NotNull org.bukkit.plugin.Plugin plugin, int rows, @NotNull String title)
    Constructs a new GUI
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPane(@NotNull Pane pane)
    Adds a pane to this gui
    void
    Closes the inventory in the next server tick for all viewers.
    void
    Closes the inventory for all viewers.
    protected @NotNull HumanEntityCache
    Gets the human entity cache used for this gui
    @NotNull org.bukkit.inventory.Inventory
     
    static org.bukkit.inventory.Inventory
    getInventory(org.bukkit.inventory.InventoryView view, int rawSlot)
     
    Gets all the items in all underlying panes
    @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent>
    Gets the bottom click event assigned to this gui, or null if there is no bottom click assigned.
    @Nullable Consumer<org.bukkit.event.inventory.InventoryCloseEvent>
    Gets the on close event assigned to this gui, or null if no close event is assigned.
    @Nullable Consumer<org.bukkit.event.inventory.InventoryDragEvent>
    Gets the global click event assigned to this gui, or null if there is no global click assigned.
    @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent>
    Gets the global click event assigned to this gui, or null if there is no global click assigned.
    @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent>
    Gets the outside click event assigned to this gui, or null if there is no outside click assigned.
    @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent>
    Gets the top click event assigned to this gui, or null if there is no top click assigned.
    @NotNull List<Pane>
    Gets all the panes in this gui, this includes child panes from other panes
    int
    Returns the amount of rows this gui currently has
    @NotNull Gui.State
    Gets the state of this gui
    @NotNull String
    Returns the title of this gui
    int
    Gets the count of HumanEntity instances that are currently viewing this GUI.
    @NotNull List<org.bukkit.entity.HumanEntity>
    Gets a mutable snapshot of the current HumanEntity viewers of this GUI.
    boolean
    Gets whether this gui is being updated, as invoked by update().
    static @Nullable Gui
    load(@NotNull org.bukkit.plugin.Plugin plugin, @NotNull Object instance, @NotNull InputStream inputStream)
    Loads a Gui from a given input stream.
    static @NotNull Gui
    loadOrThrow(@NotNull org.bukkit.plugin.Plugin plugin, @NotNull Object instance, @NotNull InputStream inputStream)
    Loads a Gui from a given input stream.
    static @NotNull Pane
    loadPane(@NotNull Object instance, @NotNull Node node)
    Loads a pane by the given instance and node
    static void
    registerPane(@NotNull String name, @NotNull BiFunction<Object,Element,Pane> biFunction)
    Registers a name that can be used inside an XML file to add custom panes
    static void
    registerProperty(@NotNull String attributeName, @NotNull Function<String,Object> function)
    Registers a property that can be used inside an XML file to add additional new properties.
    void
    Removes all the panes stored in this gui instance.
    void
    setOnBottomClick(@Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onBottomClick)
    Set the consumer that should be called whenever the inventory is clicked in.
    void
    setOnClose(@Nullable Consumer<org.bukkit.event.inventory.InventoryCloseEvent> onClose)
    Set the consumer that should be called whenever this gui is closed.
    void
    setOnDrag(@Nullable Consumer<org.bukkit.event.inventory.InventoryDragEvent> onDrag)
    Set the consumer that should be called whenever player drags item in this gui.
    void
    setOnGlobalClick(@Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onGlobalClick)
    Set the consumer that should be called whenever this gui or inventory is clicked in.
    void
    setOnLocalClick(@NotNull Consumer<org.bukkit.event.inventory.InventoryClickEvent> onLocalClick)
    Set the consumer that should be called whenever this gui is clicked in.
    void
    setOnOutsideClick(@Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onOutsideClick)
    Set the consumer that should be called whenever a player clicks outside the gui.
    void
    setOnTopClick(@Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onTopClick)
    Set the consumer that should be called whenever this gui is clicked in.
    void
    setRows(int rows)
    Sets the amount of rows for this inventory.
    void
    setState(@NotNull Gui.State state)
    Calling this method will set the state of this gui.
    void
    setTitle(@NotNull String title)
    Sets the title for this inventory.
    void
    show(@NotNull org.bukkit.entity.HumanEntity humanEntity)
    Shows a gui to a player
    void
    Update the gui for everyone

    Methods inherited from class java.lang.Object

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

    • Gui

      public Gui(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, int rows, @NotNull @NotNull String title)
      Constructs a new GUI
      Parameters:
      plugin - the main plugin.
      rows - the amount of rows this gui should contain, in range 1..6.
      title - the title/name of this gui.
  • Method Details

    • load

      @Nullable public static @Nullable Gui load(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, @NotNull @NotNull Object instance, @NotNull @NotNull InputStream inputStream)
      Loads a Gui from a given input stream. Returns null instead of throwing an exception in case of a failure.
      Parameters:
      plugin - the main plugin
      instance - the class instance for all reflection lookups
      inputStream - the file
      Returns:
      the gui or null if the loading failed
      See Also:
    • loadOrThrow

      @NotNull public static @NotNull Gui loadOrThrow(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, @NotNull @NotNull Object instance, @NotNull @NotNull InputStream inputStream)
      Loads a Gui from a given input stream. Throws a RuntimeException instead of returning null in case of a failure.
      Parameters:
      plugin - the main plugin
      instance - the class instance for all reflection lookups
      inputStream - the file
      Returns:
      the gui
      See Also:
    • getInventory

      public static org.bukkit.inventory.Inventory getInventory(org.bukkit.inventory.InventoryView view, int rawSlot)
    • registerProperty

      public static void registerProperty(@NotNull @NotNull String attributeName, @NotNull @NotNull Function<String,Object> function)
      Registers a property that can be used inside an XML file to add additional new properties.
      Parameters:
      attributeName - the name of the property. This is the same name you'll be using to specify the property type in the XML file.
      function - how the property should be processed. This converts the raw text input from the XML node value into the correct object type.
      Throws:
      IllegalArgumentException - when a property with this name is already registered.
    • registerPane

      public static void registerPane(@NotNull @NotNull String name, @NotNull @NotNull BiFunction<Object,Element,Pane> biFunction)
      Registers a name that can be used inside an XML file to add custom panes
      Parameters:
      name - the name of the pane to be used in the XML file
      biFunction - how the pane loading should be processed
      Throws:
      IllegalArgumentException - when a pane with this name is already registered
    • loadPane

      @NotNull public static @NotNull Pane loadPane(@NotNull @NotNull Object instance, @NotNull @NotNull Node node)
      Loads a pane by the given instance and node
      Parameters:
      instance - the instance
      node - the node
      Returns:
      the pane
    • addPane

      public void addPane(@NotNull @NotNull Pane pane)
      Adds a pane to this gui
      Parameters:
      pane - the pane to add
    • show

      public void show(@NotNull @NotNull org.bukkit.entity.HumanEntity humanEntity)
      Shows a gui to a player
      Parameters:
      humanEntity - the human entity to show the gui to
    • close

      public void close()
      Closes the inventory in the next server tick for all viewers.
    • closeInstantly

      public void closeInstantly()
      Closes the inventory for all viewers.
    • getViewerCount

      @Contract(pure=true) public int getViewerCount()
      Gets the count of HumanEntity instances that are currently viewing this GUI.
      Returns:
      the count of viewers
      Since:
      1.0.1
    • getViewers

      @NotNull @Contract(pure=true) public @NotNull List<org.bukkit.entity.HumanEntity> getViewers()
      Gets a mutable snapshot of the current HumanEntity viewers of this GUI. This is a snapshot (copy) and not a view, therefore modifications aren't visible.
      Returns:
      a snapshot of the current viewers
      Since:
      1.0.1
      See Also:
    • getPanes

      @NotNull @Contract(pure=true) public @NotNull List<Pane> getPanes()
      Gets all the panes in this gui, this includes child panes from other panes
      Returns:
      all panes
    • removePanes

      public void removePanes()
      Removes all the panes stored in this gui instance.
    • getItems

      @NotNull @Contract(pure=true) public @NotNull Collection<GuiItem> getItems()
      Gets all the items in all underlying panes
      Returns:
      all items
    • update

      public void update()
      Update the gui for everyone
    • getState

      @NotNull @Contract(pure=true) public @NotNull Gui.State getState()
      Gets the state of this gui
      Returns:
      the state
      Since:
      1.0.1
    • setState

      public void setState(@NotNull @NotNull Gui.State state)
      Calling this method will set the state of this gui. If this state is set to top state, it will restore all the stored inventories of the players and will assume no pane extends into the bottom inventory part. If the state is set to bottom state it will assume one or more panes overflow into the bottom half of the inventory and will store all players' inventories and clear those.

      Do not call this method if you just want the player's inventory to be cleared.

      Parameters:
      state - the new gui state
      Since:
      1.0.1
    • getHumanEntityCache

      @NotNull @Contract(pure=true) protected @NotNull HumanEntityCache getHumanEntityCache()
      Gets the human entity cache used for this gui
      Returns:
      the human entity cache
      Since:
      1.0.1
      See Also:
    • getOnTopClick

      @Nullable @Contract(pure=true) public @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> getOnTopClick()
      Gets the top click event assigned to this gui, or null if there is no top click assigned.
      Returns:
      the top click
      Since:
      1.0.1
    • setOnTopClick

      public void setOnTopClick(@Nullable @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onTopClick)
      Set the consumer that should be called whenever this gui is clicked in.
      Parameters:
      onTopClick - the consumer that gets called
    • getOnBottomClick

      @Nullable @Contract(pure=true) public @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> getOnBottomClick()
      Gets the bottom click event assigned to this gui, or null if there is no bottom click assigned.
      Returns:
      the bottom click
      Since:
      1.0.1
    • setOnBottomClick

      public void setOnBottomClick(@Nullable @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onBottomClick)
      Set the consumer that should be called whenever the inventory is clicked in.
      Parameters:
      onBottomClick - the consumer that gets called
    • getOnGlobalClick

      @Nullable @Contract(pure=true) public @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> getOnGlobalClick()
      Gets the global click event assigned to this gui, or null if there is no global click assigned.
      Returns:
      the global click
      Since:
      1.0.1
    • setOnGlobalClick

      public void setOnGlobalClick(@Nullable @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onGlobalClick)
      Set the consumer that should be called whenever this gui or inventory is clicked in.
      Parameters:
      onGlobalClick - the consumer that gets called
    • getOnOutsideClick

      @Nullable @Contract(pure=true) public @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> getOnOutsideClick()
      Gets the outside click event assigned to this gui, or null if there is no outside click assigned.
      Returns:
      the outside click
      Since:
      1.0.1
    • setOnOutsideClick

      public void setOnOutsideClick(@Nullable @Nullable Consumer<org.bukkit.event.inventory.InventoryClickEvent> onOutsideClick)
      Set the consumer that should be called whenever a player clicks outside the gui.
      Parameters:
      onOutsideClick - the consumer that gets called
      Since:
      1.0.1
    • setOnLocalClick

      public void setOnLocalClick(@NotNull @NotNull Consumer<org.bukkit.event.inventory.InventoryClickEvent> onLocalClick)
      Set the consumer that should be called whenever this gui is clicked in.
      Parameters:
      onLocalClick - the consumer that gets called
    • getOnDrag

      @Nullable @Contract(pure=true) public @Nullable Consumer<org.bukkit.event.inventory.InventoryDragEvent> getOnDrag()
      Gets the global click event assigned to this gui, or null if there is no global click assigned.
      Returns:
      the global click
      Since:
      1.0.6
    • setOnDrag

      public void setOnDrag(@Nullable @Nullable Consumer<org.bukkit.event.inventory.InventoryDragEvent> onDrag)
      Set the consumer that should be called whenever player drags item in this gui.
      Parameters:
      onDrag - the consumer that gets called
    • getOnClose

      @Nullable @Contract(pure=true) public @Nullable Consumer<org.bukkit.event.inventory.InventoryCloseEvent> getOnClose()
      Gets the on close event assigned to this gui, or null if no close event is assigned.
      Returns:
      the on close event
      Since:
      1.0.1
    • setOnClose

      public void setOnClose(@Nullable @Nullable Consumer<org.bukkit.event.inventory.InventoryCloseEvent> onClose)
      Set the consumer that should be called whenever this gui is closed.
      Parameters:
      onClose - the consumer that gets called
    • getRows

      public int getRows()
      Returns the amount of rows this gui currently has
      Returns:
      the amount of rows
    • setRows

      public void setRows(int rows)
      Sets the amount of rows for this inventory. This will (unlike most other methods) directly update itself in order to ensure all viewers will still be viewing the new inventory as well.
      Parameters:
      rows - the amount of rows in range 1..6.
    • getTitle

      @NotNull @Contract(pure=true) public @NotNull String getTitle()
      Returns the title of this gui
      Returns:
      the title
    • setTitle

      public void setTitle(@NotNull @NotNull String title)
      Sets the title for this inventory. This will (unlike most other methods) directly update itself in order to ensure all viewers will still be viewing the new inventory as well.
      Parameters:
      title - the title
    • getInventory

      @NotNull public @NotNull org.bukkit.inventory.Inventory getInventory()
      Specified by:
      getInventory in interface org.bukkit.inventory.InventoryHolder
    • isUpdating

      @Contract(pure=true) public boolean isUpdating()
      Gets whether this gui is being updated, as invoked by update(). This returns true if this is the case and false otherwise.
      Returns:
      whether this gui is being updated
      Since:
      1.0.1