Class InventoryComponent
java.lang.Object
com.github.stefvanschie.inventoryframework.gui.InventoryComponent
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
-
Constructor Summary
ConstructorDescriptionInventoryComponent
(int length, int height) Creates a new inventory component with the specified length and width. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.@NotNull InventoryComponent
copy()
Creates a deep copy of this inventory component.void
display()
This will make each pane in this component render their items in this inventory component.void
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.@NotNull InventoryComponent
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.getPanes()
Gets a list of panes this inventory component contains.int
getSize()
Gets the total size of this inventory component.boolean
hasItem()
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
Deprecated.void
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
Adds the specified item in the slot at the specified positions.void
Deprecated.usage ofsetItem(GuiItem, int, int)
is preferred so gui item's item meta can be freely edited without losing important internal data
-
Field Details
-
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, anIllegalArgumentException
will be thrown.- Parameters:
length
- the length of the componentheight
- the height of the component- Since:
- 0.8.0
-
-
Method Details
-
addPane
Adds a pane to the current collection of panes.- Parameters:
pane
- the pane to add- Since:
- 0.8.0
-
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, notPane.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 inoffset
- the offset from which to start counting the slots- Since:
- 0.8.0
- See Also:
-
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, notPane.Priority.HIGHEST
priority) rendering last. The items displayed in this inventory component will be put into the inventory found inInventoryHolder.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 aPlayerInventory
. See for the normal ordering of aPlayerInventory
's slots its documentation.- Parameters:
inventory
- the inventory to place the items inoffset
- the offset from which to start counting the slots- Since:
- 0.8.0
- See Also:
-
placeItems
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 aPlayerInventory
. See for the normal ordering of aPlayerInventory
's slots its documentation. In contrast todisplay(PlayerInventory, int)
this does not render the panes of this component.- Parameters:
inventory
- the inventory to place the items inoffset
- the offset from which to start counting the slots- Since:
- 0.8.0
- See Also:
-
placeItems
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 todisplay(Inventory, int)
this does not render the panes of this component.- Parameters:
inventory
- the inventory to place the items inoffset
- the offset from which to start counting the slots- Since:
- 0.8.0
- See Also:
-
click
Delegates the handling of the specified click event to the panes of this component. This will callPane.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 toevent
- the event to delegateslot
- the slot that was clicked- Since:
- 0.8.0
-
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 ownItemStack.clone()
andPane.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
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, andIllegalArgumentException
will be thrown.- Parameters:
from
- the starting index of the rangeend
- the ending index of the range- Returns:
- the new, shrunk inventory component
- Since:
- 0.8.0
-
load
Deprecated.superseded byload(Object, Element, 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 onelement
- 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 onelement
- the element to loadplugin
- 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, notPane.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, anIllegalArgumentException
will be thrown.- Parameters:
x
- the x coordinatey
- 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
Gets the item at the specified coordinates, or null if this cell is empty. If the specified coordinates are not within this inventory component, anIllegalArgumentException
will be thrown.- Parameters:
x
- the x coordinatey
- the y coordinate- Returns:
- the item or null
- Throws:
IllegalArgumentException
- when the coordinates are out of bounds- Since:
- 0.8.0
-
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
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, anIllegalArgumentException
will be thrown.- Parameters:
guiItem
- the item to place in this inventory componentx
- the x coordinate of the itemy
- the y coordinate of the item- Since:
- 0.9.3
-
setItem
Deprecated.usage ofsetItem(GuiItem, int, int)
is preferred so gui item's item meta can be freely edited without losing important internal dataAdds 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, anIllegalArgumentException
will be thrown.- Parameters:
item
- the item to place in this inventory componentx
- the x coordinate of the itemy
- 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
-
load(Object, Element, Plugin)