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.Plugin plugin, InventoryGui.InventoryCreator creator, BiConsumer<org.bukkit.inventory.meta.ItemMeta,String> itemNameSetter, BiConsumer<org.bukkit.inventory.meta.ItemMeta,List<String>> itemLoreSetter, 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
      creator - A creator for the backing inventory
      itemNameSetter - Setter for item display names
      itemLoreSetter - Setter for item lores
      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.Plugin plugin, InventoryGui.InventoryCreator creator, 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
      creator - A creator for the backing inventory
      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.Plugin 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.Plugin 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.Plugin 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

    • setElement

      public void setElement(int slot, GuiElement element)
      Directly set the element in a specific slot
      Parameters:
      element - The GuiElement to add
      Throws:
      IllegalArgumentException - Thrown if the provided slot is below 0 or equal/above the available slot count
      IllegalStateException - Thrown if the element was already added to a gui
    • addElement

      public void addElement(GuiElement element)
      Add an element to the gui with its position directly based on the elements slot char and the gui setup string
      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 specify the elements position based on 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(org.bukkit.entity.HumanEntity, 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 specify the elements position based on 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(org.bukkit.entity.HumanEntity, 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 specify the elements position based on 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(org.bukkit.entity.HumanEntity, 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 specify the elements position based on 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(org.bukkit.entity.HumanEntity, 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 specify the elements position based on 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(org.bukkit.entity.HumanEntity, 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 with their position based on their slot character
      Parameters:
      elements - The GuiElements to add
    • addElements

      public void addElements(Collection<GuiElement> elements)
      Add multiple elements to the gui with their position based on their slot character
      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 from all slots in the gui
      Parameters:
      slotChar - The char of the slot
      Returns:
      The element which was in that slot or null if there was none
    • removeElement

      public GuiElement removeElement(int slot)
      Remove the element that is currently in a specific slot. Will not remove that element from other slots
      Parameters:
      slot - 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(@NotNull @NotNull org.bukkit.entity.HumanEntity player)
      Get the number of the page that this gui is on. zero indexed. Only affects group elements.
      Parameters:
      player - The Player to query the page number for
      Returns:
      The page number
    • setPageNumber

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

      public void setPageNumber(org.bukkit.entity.HumanEntity player, int pageNumber)
      Set the number of the page that this gui is on for a player. zero indexed. Only affects group elements.
      Parameters:
      player - The player to set the page number for
      pageNumber - The page number to set
    • getPageAmount

      public int getPageAmount(@NotNull @NotNull org.bukkit.entity.HumanEntity player)
      Get the amount of pages that this GUI has for a certain player
      Parameters:
      player - The Player to query the page amount for
      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. Updates any dynamic elements.
    • draw

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

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

      public void draw(org.bukkit.entity.HumanEntity who, boolean updateDynamic, boolean recreateInventory)
      Draw the elements in the inventory. This can be used to manually refresh the gui.
      Parameters:
      who - For who to draw the GUI
      updateDynamic - Update dynamic elements
      recreateInventory - Recreate the inventory
    • runTask

      protected void runTask(org.bukkit.entity.HumanEntity entity, Runnable task)
      Schedule a task on a HumanEntity/main thread to run on the next tick
      Parameters:
      entity - the human entity to schedule a task on
      task - the task to be run
    • runTask

      protected void runTask(Runnable task)
      Schedule a task on the global region/main thread to run on the next tick
      Parameters:
      task - the task to be run
    • runTaskOrNow

      protected void runTaskOrNow(org.bukkit.entity.HumanEntity entity, Runnable task)
      Schedule a task on a HumanEntity to run on the next tick Alternatively if the current thread is already the right thread, execute immediately
      Parameters:
      entity - the human entity to schedule a task on
      task - the task to be run
    • updateElements

      public static void updateElements(org.bukkit.entity.HumanEntity who, Collection<GuiElement> elements)
      Update all dynamic elements in a collection of elements.
      Parameters:
      who - The player to update the elements for
      elements - The elements to update
    • 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 to close the GUI completely (by clearing the history)
    • close

      public void close(org.bukkit.entity.HumanEntity viewer)
      Closes the GUI for a specific viewer it
      Parameters:
      viewer - The player viewing it
    • close

      public void close(org.bukkit.entity.HumanEntity viewer, boolean clearHistory)
      Closes the GUI for a specific viewer it
      Parameters:
      viewer - The player viewing it
      clearHistory - Whether 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
    • getPlugin

      public org.bukkit.plugin.Plugin getPlugin()
      Get the plugin which owns this GUI. Should be the one who created it.
      Returns:
      The plugin which owns this GUI
    • getInventoryCreator

      public InventoryGui.InventoryCreator getInventoryCreator()
      Get the helper class which will create the custom inventory for this gui. Simply uses Bukkit.createInventory(InventoryHolder, int, String) by default.
      Returns:
      The used inventory creator instance
    • setInventoryCreator

      public void setInventoryCreator(InventoryGui.InventoryCreator inventoryCreator)
      Set the helper class which will create the custom inventory for this gui. Can be used to create more special inventories. Simply uses Bukkit.createInventory(InventoryHolder, int, String) by default. Should return a container inventory that can hold the size. Special inventories will break stuff.
      Parameters:
      inventoryCreator - The new inventory creator instance
    • getItemNameSetter

      public BiConsumer<org.bukkit.inventory.meta.ItemMeta,String> getItemNameSetter()
      Get the setter for item names.
      Returns:
      The setter instance
    • setItemNameSetter

      public void setItemNameSetter(BiConsumer<org.bukkit.inventory.meta.ItemMeta,String> itemNameSetter)
      Sets the setter ofr item names.
      Parameters:
      itemNameSetter - The item name setter BiConsumer taking the ItemMeta to be modified and the string for the name
    • getItemLoreSetter

      public BiConsumer<org.bukkit.inventory.meta.ItemMeta,List<String>> getItemLoreSetter()
      Get the setter for item lores.
      Returns:
      The setter instance
    • setItemLoreSetter

      public void setItemLoreSetter(BiConsumer<org.bukkit.inventory.meta.ItemMeta,List<String>> itemLoreSetter)
      Sets the setter for item lores.
      Parameters:
      itemLoreSetter - The item lore setter BiConsumer taking the ItemMeta to be modified and the string list for the lore lines
    • 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
    • getDefaultClickSound

      public static String getDefaultClickSound()
      Get the click sound to use for non-silent GUIs that don't have a specific one set
      Returns:
      The default click sound, if set null no sound will play
    • setDefaultClickSound

      public static void setDefaultClickSound(String defaultClickSound)
      Set the click sound to use for non-silent GUIs that don't have a specific one set
      Parameters:
      defaultClickSound - The default click sound, if set to null no sound will play
    • getClickSound

      public String getClickSound()
      Set the sound that plays when a button (that isn't preventing the item from being taken) is clicked in the GUI. Fillers will not play a click sound
      Returns:
      The key of the sound to play
    • setClickSound

      public void setClickSound(String soundKey)
      Set the sound that plays when a button (that isn't preventing the item from being taken) is clicked in the GUI. Fillers will not play a click sound
      Parameters:
      soundKey - The key of the sound to play, if null then no sound will play (same effect as setSilent(boolean))
    • 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

      @Deprecated 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
    • setItemText

      public void setItemText(org.bukkit.entity.HumanEntity player, 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:
      player - The player viewing the GUI
      item - The ItemStack to set the text for
      text - The text lines to set
    • replaceVars

      public String replaceVars(@NotNull @NotNull org.bukkit.entity.HumanEntity player, @NotNull @NotNull 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:
      player - The player viewing the GUI
      text - The text to replace the placeholders in
      replacements - Additional replacements. i = placeholder, i+1 = replacements
      Returns:
      The text with all placeholders replaced