Class InventoryComponent

java.lang.Object
com.github.stefvanschie.inventoryframework.gui.InventoryComponent

public class InventoryComponent extends Object
Represents a component within an inventory that can hold items. This is always in the shape of a rectangular grid.
Since:
0.8.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final @NotNull List<Pane>
    A set of all panes in this inventory.
  • Constructor Summary

    Constructors
    Constructor
    Description
    InventoryComponent(int length, int height)
    Creates a new inventory component with the specified length and width.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPane(@NotNull Pane pane)
    Adds a pane to the current collection of panes.
    void
    click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int slot)
    Delegates the handling of the specified click event to the panes of this component.
    Creates a deep copy of this inventory component.
    void
    This will make each pane in this component render their items in this inventory component.
    void
    display(@NotNull Inventory inventory, int offset)
    This will make each pane in this component render their items in this inventory component.
    void
    display(@NotNull PlayerInventory inventory, int offset)
    This will make each pane in this component render their items in this inventory component.
    excludeRows(int from, int end)
    Returns a new inventory component, excluding the range of specified rows.
    int
    Gets the height of this inventory component.
    @Nullable ItemStack
    getItem(int x, int y)
    Gets the item at the specified coordinates, or null if this cell is empty.
    int
    Gets the length of this inventory component.
    @NotNull List<Pane>
    Gets a list of panes this inventory component contains.
    int
    Gets the total size of this inventory component.
    boolean
    Checks whether this component has at least one item.
    boolean
    hasItem(int x, int y)
    Checks whether the item at the specified coordinates exists.
    void
    load(@NotNull Object instance, @NotNull Element element)
    Deprecated.
    void
    load(@NotNull Object instance, @NotNull Element element, @NotNull Plugin plugin)
    Loads the provided element's child panes onto this component.
    void
    placeItems(@NotNull Inventory inventory, int offset)
    This places the items currently existing in this inventory component into the specified inventory.
    void
    placeItems(@NotNull PlayerInventory inventory, int offset)
    This places the items currently existing in this inventory component into the specified player inventory.
    void
    setItem(@NotNull GuiItem guiItem, int x, int y)
    Adds the specified item in the slot at the specified positions.
    void
    setItem(@NotNull ItemStack item, int x, int y)
    Deprecated.
    usage of setItem(GuiItem, int, int) is preferred so gui item's item meta can be freely edited without losing important internal data

    Methods inherited from class java.lang.Object

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

    • panes

      @NotNull protected final @NotNull List<Pane> panes
      A set of all panes in this inventory. This is guaranteed to be sorted in order of the pane's priorities, from the lowest priority to the highest priority. The order of panes with the same priority is unspecified.
  • Constructor Details

    • InventoryComponent

      public InventoryComponent(int length, int height)
      Creates a new inventory component with the specified length and width. If either the length or the width is less than zero, an IllegalArgumentException will be thrown.
      Parameters:
      length - the length of the component
      height - the height of the component
      Since:
      0.8.0
  • Method Details

    • addPane

      public void addPane(@NotNull @NotNull Pane pane)
      Adds a pane to the current collection of panes.
      Parameters:
      pane - the pane to add
      Since:
      0.8.0
    • display

      public void display(@NotNull @NotNull Inventory inventory, int offset)
      This will make each pane in this component render their items in this inventory component. The panes are displayed according to their priority, with the lowest priority rendering first and the highest priority (note: highest priority, not Pane.Priority.HIGHEST priority) rendering last. The items displayed in this inventory component will be put into the specified inventory. The slots will start at the given offset up to this component's size + the offset specified.
      Parameters:
      inventory - the inventory to place the items in
      offset - the offset from which to start counting the slots
      Since:
      0.8.0
      See Also:
    • display

      public void display(@NotNull @NotNull PlayerInventory inventory, int offset)
      This will make each pane in this component render their items in this inventory component. The panes are displayed according to their priority, with the lowest priority rendering first and the highest priority (note: highest priority, not Pane.Priority.HIGHEST priority) rendering last. The items displayed in this inventory component will be put into the inventory found in InventoryHolder.getInventory(). The slots will be placed from the top-right to the bottom-left, continuing from left-to-right, top-to-bottom plus the specified offset. This ordering is different from the normal ordering of the indices of a PlayerInventory. See for the normal ordering of a PlayerInventory's slots its documentation.
      Parameters:
      inventory - the inventory to place the items in
      offset - the offset from which to start counting the slots
      Since:
      0.8.0
      See Also:
    • placeItems

      public void placeItems(@NotNull @NotNull PlayerInventory inventory, int offset)
      This places the items currently existing in this inventory component into the specified player inventory. The slots will be placed from the top-right to the bottom-left, continuing from left-to-right, top-to-bottom plus the specified offset. This ordering is different from the normal ordering of the indices of a PlayerInventory. See for the normal ordering of a PlayerInventory's slots its documentation. In contrast to display(PlayerInventory, int) this does not render the panes of this component.
      Parameters:
      inventory - the inventory to place the items in
      offset - the offset from which to start counting the slots
      Since:
      0.8.0
      See Also:
    • placeItems

      public void placeItems(@NotNull @NotNull Inventory inventory, int offset)
      This places the items currently existing in this inventory component into the specified inventory. The slots will start at the given offset up to this component's size + the offset specified. In contrast to display(Inventory, int) this does not render the panes of this component.
      Parameters:
      inventory - the inventory to place the items in
      offset - the offset from which to start counting the slots
      Since:
      0.8.0
      See Also:
    • click

      public void click(@NotNull @NotNull Gui gui, @NotNull @NotNull InventoryClickEvent event, int slot)
      Delegates the handling of the specified click event to the panes of this component. This will call Pane.click(Gui, InventoryComponent, InventoryClickEvent, int, int, int, int, int) on each pane until the right item has been found.
      Parameters:
      gui - the gui this inventory component belongs to
      event - the event to delegate
      slot - the slot that was clicked
      Since:
      0.8.0
    • copy

      @NotNull @Contract(pure=true) public @NotNull InventoryComponent copy()
      Creates a deep copy of this inventory component. This means that all internal items will be cloned and all panes will be copied as per their own ItemStack.clone() and Pane.copy() methods. The returned inventory component is guaranteed to not reference equals this inventory component.
      Returns:
      the new inventory component
      Since:
      0.8.0
    • excludeRows

      @NotNull @Contract(pure=true) public @NotNull InventoryComponent excludeRows(int from, int end)
      Returns a new inventory component, excluding the range of specified rows. The new inventory component will have its size shrunk so only the included rows are present and any items in the excluded rows are discarded. All panes will stay present. Note that while this does make a new inventory component, it does not make a copy. For example, the panes in the new inventory component will be the exact same panes as in this one and will not be copied. This is also true for any retained items. The specified range is 0-indexed: the first row starts at index 0 and the last row ends at height - 1. The range is inclusive on both ends, the row specified at either parameter will also be excluded. When the range specified is invalid - that is, part of the range contains rows that are not included in this inventory component, and IllegalArgumentException will be thrown.
      Parameters:
      from - the starting index of the range
      end - the ending index of the range
      Returns:
      the new, shrunk inventory component
      Since:
      0.8.0
    • load

      @Deprecated public void load(@NotNull @NotNull Object instance, @NotNull @NotNull Element element)
      Deprecated.
      Loads the provided element's child panes onto this component. If the element contains any child panes, this will mutate this component.
      Parameters:
      instance - the instance to apply field and method references on
      element - the element to load
      Since:
      0.8.0
    • load

      public void load(@NotNull @NotNull Object instance, @NotNull @NotNull Element element, @NotNull @NotNull Plugin plugin)
      Loads the provided element's child panes onto this component. If the element contains any child panes, this will mutate this component.
      Parameters:
      instance - the instance to apply field and method references on
      element - the element to load
      plugin - the plugin to load the panes with
      Since:
      0.10.12
    • hasItem

      @Contract(pure=true) public boolean hasItem()
      Checks whether this component has at least one item. If it does, true is returned; false otherwise.
      Returns:
      true if this has an item, false otherwise
      Since:
      0.8.0
    • display

      public void display()
      This will make each pane in this component render their items in this inventory component. The panes are displayed according to their priority, with the lowest priority rendering first and the highest priority (note: highest priority, not Pane.Priority.HIGHEST priority) rendering last.
      Since:
      0.8.0
      See Also:
    • hasItem

      @Contract(pure=true) public boolean hasItem(int x, int y)
      Checks whether the item at the specified coordinates exists. If the specified coordinates are not within this inventory component, an IllegalArgumentException will be thrown.
      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      true if an item exists at the given coordinates, false otherwise
      Throws:
      IllegalArgumentException - when the coordinates are out of bounds
      Since:
      0.8.0
    • getItem

      @Nullable @Contract(pure=true) public @Nullable ItemStack getItem(int x, int y)
      Gets the item at the specified coordinates, or null if this cell is empty. If the specified coordinates are not within this inventory component, an IllegalArgumentException will be thrown.
      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      the item or null
      Throws:
      IllegalArgumentException - when the coordinates are out of bounds
      Since:
      0.8.0
    • getPanes

      @NotNull @Contract(pure=true) public @NotNull List<Pane> getPanes()
      Gets a list of panes this inventory component contains. The returned list is modifiable. If this inventory component currently does not have any panes, an empty list is returned. This list is guaranteed to be sorted according to the panes' priorities.
      Returns:
      the panes this component has
      Since:
      0.8.0
    • setItem

      public void setItem(@NotNull @NotNull GuiItem guiItem, int x, int y)
      Adds the specified item in the slot at the specified positions. This will override an already set item if it resides in the same position as specified. If the position specified is outside of the boundaries set by this component, an IllegalArgumentException will be thrown.
      Parameters:
      guiItem - the item to place in this inventory component
      x - the x coordinate of the item
      y - the y coordinate of the item
      Since:
      0.9.3
    • setItem

      @Deprecated public void setItem(@NotNull @NotNull ItemStack item, int x, int y)
      Deprecated.
      usage of setItem(GuiItem, int, int) is preferred so gui item's item meta can be freely edited without losing important internal data
      Adds the specified item in the slot at the specified positions. This will override an already set item if it resides in the same position as specified. If the position specified is outside of the boundaries set by this component, an IllegalArgumentException will be thrown.
      Parameters:
      item - the item to place in this inventory component
      x - the x coordinate of the item
      y - the y coordinate of the item
      Since:
      0.8.0
    • getSize

      @Contract(pure=true) public int getSize()
      Gets the total size of this inventory component.
      Returns:
      the size
      Since:
      0.8.0
    • getHeight

      @Contract(pure=true) public int getHeight()
      Gets the height of this inventory component.
      Returns:
      the height
      Since:
      0.8.0
    • getLength

      @Contract(pure=true) public int getLength()
      Gets the length of this inventory component.
      Returns:
      the length
      Since:
      0.8.0