Class InventoryGui

java.lang.Object
de.themoep.inventorygui.InventoryGui
All Implemented Interfaces:
org.bukkit.event.Listener

public class InventoryGui
extends Object
implements org.bukkit.event.Listener
The main library class that lets you create and manage your GUIs
  • Constructor Details

    • InventoryGui

      public InventoryGui​(org.bukkit.plugin.java.JavaPlugin plugin, org.bukkit.inventory.InventoryHolder owner, String title, String[] rows, GuiElement... elements)
      Create a new gui with a certain setup and some elements
      Parameters:
      plugin - Your plugin
      owner - The holder that owns this gui to retrieve it with get(InventoryHolder). Can be null.
      title - The name of the GUI. This will be the title of the inventory.
      rows - How your rows are setup. Each element is getting assigned to a character. Empty/missing ones get filled with the Filler.
      elements - The GuiElements that the gui should have. You can also use addElement(GuiElement) later.
      Throws:
      IllegalArgumentException - Thrown when the provided rows cannot be matched to an InventoryType
    • InventoryGui

      public InventoryGui​(org.bukkit.plugin.java.JavaPlugin plugin, String title, String[] rows, GuiElement... elements)
      The simplest way to create a new gui. It has no owner and elements are optional.
      Parameters:
      plugin - Your plugin
      title - The name of the GUI. This will be the title of the inventory.
      rows - How your rows are setup. Each element is getting assigned to a character. Empty/missing ones get filled with the Filler.
      elements - The GuiElements that the gui should have. You can also use addElement(GuiElement) later.
      Throws:
      IllegalArgumentException - Thrown when the provided rows cannot be matched to an InventoryType
    • InventoryGui

      public InventoryGui​(org.bukkit.plugin.java.JavaPlugin plugin, org.bukkit.inventory.InventoryHolder owner, String title, String[] rows, Collection<GuiElement> elements)
      Create a new gui that has no owner with a certain setup and some elements
      Parameters:
      plugin - Your plugin
      owner - The holder that owns this gui to retrieve it with get(InventoryHolder). Can be null.
      title - The name of the GUI. This will be the title of the inventory.
      rows - How your rows are setup. Each element is getting assigned to a character. Empty/missing ones get filled with the Filler.
      elements - The GuiElements that the gui should have. You can also use addElement(GuiElement) later.
      Throws:
      IllegalArgumentException - Thrown when the provided rows cannot be matched to an InventoryType
  • Method Details

    • addElement

      public void addElement​(GuiElement element)
      Add an element to the gui
      Parameters:
      element - The GuiElement to add
    • addElement

      public void addElement​(char slotChar, org.bukkit.inventory.ItemStack item, GuiElement.Action action, String... text)
      Create and add a StaticGuiElement in one quick method.
      Parameters:
      slotChar - The character to replace in the gui setup string
      item - The item that should be displayed
      action - The GuiElement.Action to run when the player clicks on this element
      text - The text to display on this element, placeholders are automatically replaced, see replaceVars(java.lang.String, java.lang.String...) for a list of the placeholder variables. Empty text strings are also filter out, use a single space if you want to add an empty line!
      If it's not set/empty the item's default name will be used
    • addElement

      public void addElement​(char slotChar, org.bukkit.inventory.ItemStack item, String... text)
      Create and add a StaticGuiElement that has no action.
      Parameters:
      slotChar - The character to replace in the gui setup string
      item - The item that should be displayed
      text - The text to display on this element, placeholders are automatically replaced, see replaceVars(java.lang.String, java.lang.String...) for a list of the placeholder variables. Empty text strings are also filter out, use a single space if you want to add an empty line!
      If it's not set/empty the item's default name will be used
    • addElement

      public void addElement​(char slotChar, org.bukkit.material.MaterialData materialData, GuiElement.Action action, String... text)
      Create and add a StaticGuiElement in one quick method.
      Parameters:
      slotChar - The character to replace in the gui setup string
      materialData - The MaterialData of the item of tihs element
      action - The GuiElement.Action to run when the player clicks on this element
      text - The text to display on this element, placeholders are automatically replaced, see replaceVars(java.lang.String, java.lang.String...) for a list of the placeholder variables. Empty text strings are also filter out, use a single space if you want to add an empty line!
      If it's not set/empty the item's default name will be used
    • addElement

      public void addElement​(char slotChar, org.bukkit.Material material, byte data, GuiElement.Action action, String... text)
      Create and add a StaticGuiElement
      Parameters:
      slotChar - The character to replace in the gui setup string
      material - The Material that the item should have
      data - The byte representation of the material data of this element
      action - The GuiElement.Action to run when the player clicks on this element
      text - The text to display on this element, placeholders are automatically replaced, see replaceVars(java.lang.String, java.lang.String...) for a list of the placeholder variables. Empty text strings are also filter out, use a single space if you want to add an empty line!
      If it's not set/empty the item's default name will be used
    • addElement

      public void addElement​(char slotChar, org.bukkit.Material material, GuiElement.Action action, String... text)
      Create and add a StaticGuiElement
      Parameters:
      slotChar - The character to replace in the gui setup string
      material - The Material that the item should have
      action - The GuiElement.Action to run when the player clicks on this element
      text - The text to display on this element, placeholders are automatically replaced, see replaceVars(java.lang.String, java.lang.String...) for a list of the placeholder variables. Empty text strings are also filter out, use a single space if you want to add an empty line!
      If it's not set/empty the item's default name will be used
    • addElements

      public void addElements​(GuiElement... elements)
      Add multiple elements to the gui
      Parameters:
      elements - The GuiElements to add
    • addElements

      public void addElements​(Collection<GuiElement> elements)
      Add multiple elements to the gui
      Parameters:
      elements - The GuiElements to add
    • removeElement

      public boolean removeElement​(GuiElement element)
      Remove a specific element from this gui.
      Parameters:
      element - The element to remove
      Returns:
      Whether or not the gui contained this element and if it was removed
    • removeElement

      public GuiElement removeElement​(char slotChar)
      Remove the element that is currently assigned to a specific slot char
      Parameters:
      slotChar - The char of the slot
      Returns:
      The element which was in that slot or null if there was none
    • setFiller

      public void setFiller​(org.bukkit.inventory.ItemStack item)
      Set the filler element for empty slots
      Parameters:
      item - The item for the filler element
    • getFiller

      public GuiElement getFiller()
      Get the filler element
      Returns:
      The filler element for empty slots
    • getPageNumber

      public int getPageNumber()
      Get the number of the page that this gui is on. zero indexed. Only affects group elements.
      Returns:
      The page number
    • setPageNumber

      public void setPageNumber​(int pageNumber)
      Set the number of the page that this gui is on. zero indexed. Only affects group elements.
      Parameters:
      pageNumber - The page number to set
    • getPageAmount

      public int getPageAmount()
      Get the amount of pages that this GUI has
      Returns:
      The amount of pages
    • show

      public void show​(org.bukkit.entity.HumanEntity player)
      Show this GUI to a player
      Parameters:
      player - The Player to show the GUI to
    • show

      public void show​(org.bukkit.entity.HumanEntity player, boolean checkOpen)
      Show this GUI to a player
      Parameters:
      player - The Player to show the GUI to
      checkOpen - Whether or not it should check if this gui is already open
    • build

      public void build()
      Build the gui
    • build

      public void build​(org.bukkit.inventory.InventoryHolder owner)
      Set the gui's owner and build it
      Parameters:
      owner - The InventoryHolder that owns the gui
    • draw

      public void draw()
      Draw the elements in the inventory. This can be used to manually refresh the gui.
    • draw

      public void draw​(org.bukkit.entity.HumanEntity who)
      Draw the elements in the inventory. This can be used to manually refresh the gui.
      Parameters:
      who - For who to draw the GUI
    • close

      public void close()
      Closes the GUI for everyone viewing it
    • close

      public void close​(boolean clearHistory)
      Close the GUI for everyone viewing it
      Parameters:
      clearHistory - Whether or not to close the GUI completely (by clearing the history)
    • destroy

      public void destroy()
      Destroy this GUI. This unregisters all listeners and removes it from the GUI_MAP
    • addHistory

      public static void addHistory​(org.bukkit.entity.HumanEntity player, InventoryGui gui)
      Add a new history entry to the end of the history
      Parameters:
      player - The player to add the history entry for
      gui - The GUI to add to the history
    • getHistory

      public static Deque<InventoryGui> getHistory​(org.bukkit.entity.HumanEntity player)
      Get the history of a player
      Parameters:
      player - The player to get the history for
      Returns:
      The history as a deque of InventoryGuis; returns an empty one and not null!
    • goBack

      public static boolean goBack​(org.bukkit.entity.HumanEntity player)
      Go back one entry in the history
      Parameters:
      player - The player to show the previous gui to
      Returns:
      true if there was a gui to show; false if not
    • clearHistory

      public static Deque<InventoryGui> clearHistory​(org.bukkit.entity.HumanEntity player)
      Clear the history of a player
      Parameters:
      player - The player to clear the history for
      Returns:
      The history
    • getElement

      public GuiElement getElement​(int slot)
      Get element in a certain slot
      Parameters:
      slot - The slot to get the element from
      Returns:
      The GuiElement or null if the slot is empty/there wasn't one
    • getElement

      public GuiElement getElement​(char c)
      Get an element by its character
      Parameters:
      c - The character to get the element by
      Returns:
      The GuiElement or null if there is no element for that character
    • getElements

      public Collection<GuiElement> getElements()
      Get all elements of this gui. This collection is immutable, use the addElement and removeElement methods to modify the elements in this gui.
      Returns:
      An immutable collection of all elements in this group
    • setOwner

      public void setOwner​(org.bukkit.inventory.InventoryHolder owner)
      Set the owner of this GUI. Will remove the previous assignment.
      Parameters:
      owner - The owner of the GUI
    • getOwner

      public org.bukkit.inventory.InventoryHolder getOwner()
      Get the owner of this GUI. Will be null if th GUI doesn't have one
      Returns:
      The InventoryHolder of this GUI
    • hasRealOwner

      public boolean hasRealOwner()
      Check whether or not the Owner of this GUI is real or fake
      Returns:
      true if the owner is a real world InventoryHolder; false if it is null
    • getOutsideAction

      public GuiElement.Action getOutsideAction()
      Get the Action that is run when clicked outside of the inventory
      Returns:
      The Action for when the player clicks outside the inventory; can be null
    • setOutsideAction

      public void setOutsideAction​(GuiElement.Action outsideAction)
      Set the Action that is run when clicked outside of the inventory
      Parameters:
      outsideAction - The Action for when the player clicks outside the inventory; can be null
    • getCloseAction

      public InventoryGui.CloseAction getCloseAction()
      Get the action that is run when this GUI is closed
      Returns:
      The action for when the player closes this inventory; can be null
    • setCloseAction

      public void setCloseAction​(InventoryGui.CloseAction closeAction)
      Set the action that is run when this GUI is closed; it should return true if the GUI should go back
      Parameters:
      closeAction - The action for when the player closes this inventory; can be null
    • isSilent

      public boolean isSilent()
      Get whether or not this GUI should make a sound when interacting with elements that make sound
      Returns:
      Whether or not to make a sound when interacted with
    • setSilent

      public void setSilent​(boolean silent)
      Set whether or not this GUI should make a sound when interacting with elements that make sound
      Parameters:
      silent - Whether or not to make a sound when interacted with
    • get

      public static InventoryGui get​(org.bukkit.inventory.InventoryHolder holder)
      Get the GUI registered to an InventoryHolder
      Parameters:
      holder - The InventoryHolder to get the GUI for
      Returns:
      The InventoryGui registered to it or null if none was registered to it
    • getOpen

      public static InventoryGui getOpen​(org.bukkit.entity.HumanEntity player)
      Get the GUI that a player has currently open
      Parameters:
      player - The Player to get the GUI for
      Returns:
      The InventoryGui that the player has open
    • getTitle

      public String getTitle()
      Get the title of the gui
      Returns:
      The title of the gui
    • setTitle

      public void setTitle​(String title)
      Set the title of the gui
      Parameters:
      title - The String that should be the title of the gui
    • playClickSound

      public void playClickSound()
      Play a click sound e.g. when an element acts as a button
    • setItemText

      public void setItemText​(org.bukkit.inventory.ItemStack item, String... text)
      Set the text of an item using the display name and the lore. Also replaces any placeholders in the text and filters out empty lines. Use a single space to create an emtpy line.
      Parameters:
      item - The ItemStack to set the text for
      text - The text lines to set
    • replaceVars

      public String replaceVars​(String text, String... replacements)
      Replace some placeholders in the with values regarding the gui's state. Replaced color codes.
      The placeholders are:
      %plugin% - The name of the plugin that this gui is from.
      %owner% - The name of the owner of this gui. Will be an empty string when the owner is null.
      %title% - The title of this GUI.
      %page% - The current page that this gui is on.
      %nextpage% - The next page. "none" if there is no next page.
      %prevpage% - The previous page. "none" if there is no previous page.
      %pages% - The amount of pages that this gui has.
      Parameters:
      text - The text to replace the placeholders in
      replacements - Additional repplacements. i = placeholder, i+1 = replacements
      Returns:
      The text with all placeholders replaced