Package net.minestom.server.inventory
Class AbstractInventory
java.lang.Object
net.minestom.server.inventory.AbstractInventory
- All Implemented Interfaces:
InventoryClickHandler
,Taggable
,TagReadable
,TagWritable
- Direct Known Subclasses:
Inventory
,PlayerInventory
public abstract sealed class AbstractInventory
extends Object
implements InventoryClickHandler, Taggable
permits Inventory, PlayerInventory
Represents an inventory where items can be modified/retrieved.
-
Field Summary
Modifier and TypeFieldDescriptionprotected final InventoryClickProcessor
protected final List
<InventoryCondition> protected final ItemStack[]
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addInventoryCondition
(@NotNull InventoryCondition inventoryCondition) Adds a newInventoryCondition
to this inventory.boolean
addItemStack
(@NotNull ItemStack itemStack) <T> T
addItemStack
(@NotNull ItemStack itemStack, @NotNull TransactionOption<T> option) Adds anItemStack
to the inventory and sends relevant update to the viewer(s).<T> @NotNull List
<@NotNull T> addItemStacks
(@NotNull List<@NotNull ItemStack> itemStacks, @NotNull TransactionOption<T> option) AddsItemStack
s to the inventory and sends relevant updates to the viewer(s).void
clear()
Clears the inventory and send relevant update to the viewer(s).void
copyContents
(@NotNull ItemStack[] itemStacks) Places all the items ofitemStacks
into the internal array.int
Gets the size of the "inner inventory" (which includes only "usable" slots).@NotNull List
<@NotNull InventoryCondition> Gets all theInventoryCondition
of this inventory.@NotNull ItemStack
getItemStack
(int slot) Gets theItemStack
at the specified slot.@NotNull ItemStack[]
Gets all theItemStack
in the inventory.int
getSize()
Gets the size of the inventory.<T> T
processItemStack
(@NotNull ItemStack itemStack, @NotNull TransactionType type, @NotNull TransactionOption<T> option) <T> @NotNull List
<@NotNull T> processItemStacks
(@NotNull List<@NotNull ItemStack> itemStacks, @NotNull TransactionType type, @NotNull TransactionOption<T> option) void
replaceItemStack
(int slot, @NotNull UnaryOperator<@NotNull ItemStack> operator) protected final void
safeItemInsert
(int slot, @NotNull ItemStack itemStack) protected final void
safeItemInsert
(int slot, @NotNull ItemStack itemStack, boolean sendPacket) Inserts safely an item into the inventory.void
setItemStack
(int slot, @NotNull ItemStack itemStack) Sets anItemStack
at the specified slot and send relevant update to the viewer(s).@NotNull TagHandler
<T> T
takeItemStack
(@NotNull ItemStack itemStack, @NotNull TransactionOption<T> option) Takes anItemStack
from the inventory and sends relevant update to the viewer(s).<T> @NotNull List
<@NotNull T> takeItemStacks
(@NotNull List<@NotNull ItemStack> itemStacks, @NotNull TransactionOption<T> option) TakesItemStack
s from the inventory and sends relevant updates to the viewer(s).protected abstract void
UNSAFE_itemInsert
(int slot, @NotNull ItemStack itemStack, boolean sendPacket) abstract void
update()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.minestom.server.inventory.InventoryClickHandler
callClickEvent, changeHeld, doubleClick, dragging, drop, leftClick, middleClick, rightClick, shiftClick
Methods inherited from interface net.minestom.server.tag.Taggable
getAndSetTag, getAndUpdateTag, getTag, hasTag, removeTag, setTag, updateAndGetTag, updateTag
-
Field Details
-
itemStacks
-
inventoryConditions
-
clickProcessor
-
-
Constructor Details
-
AbstractInventory
protected AbstractInventory(int size)
-
-
Method Details
-
setItemStack
Sets anItemStack
at the specified slot and send relevant update to the viewer(s).- Parameters:
slot
- the slot to set the itemitemStack
- the item to set
-
safeItemInsert
protected final void safeItemInsert(int slot, @NotNull @NotNull ItemStack itemStack, boolean sendPacket) Inserts safely an item into the inventory.This will update the slot for all viewers and warn the inventory that the window items packet is not up-to-date.
- Parameters:
slot
- the internal slot iditemStack
- the item to insert (use air instead of null)- Throws:
IllegalArgumentException
- if the slotslot
does not exist
-
safeItemInsert
-
UNSAFE_itemInsert
protected abstract void UNSAFE_itemInsert(int slot, @NotNull @NotNull ItemStack itemStack, boolean sendPacket) -
processItemStack
@NotNull public <T> T processItemStack(@NotNull @NotNull ItemStack itemStack, @NotNull @NotNull TransactionType type, @NotNull @NotNull TransactionOption<T> option) -
processItemStacks
@NotNull public <T> @NotNull List<@NotNull T> processItemStacks(@NotNull @NotNull List<@NotNull ItemStack> itemStacks, @NotNull @NotNull TransactionType type, @NotNull @NotNull TransactionOption<T> option) -
addItemStack
@NotNull public <T> T addItemStack(@NotNull @NotNull ItemStack itemStack, @NotNull @NotNull TransactionOption<T> option) Adds anItemStack
to the inventory and sends relevant update to the viewer(s).- Parameters:
itemStack
- the item to addoption
- the transaction option- Returns:
- true if the item has been successfully added, false otherwise
-
addItemStack
-
addItemStacks
@NotNull public <T> @NotNull List<@NotNull T> addItemStacks(@NotNull @NotNull List<@NotNull ItemStack> itemStacks, @NotNull @NotNull TransactionOption<T> option) AddsItemStack
s to the inventory and sends relevant updates to the viewer(s).- Parameters:
itemStacks
- items to addoption
- the transaction option- Returns:
- the operation results
-
takeItemStack
@NotNull public <T> T takeItemStack(@NotNull @NotNull ItemStack itemStack, @NotNull @NotNull TransactionOption<T> option) Takes anItemStack
from the inventory and sends relevant update to the viewer(s).- Parameters:
itemStack
- the item to take- Returns:
- true if the item has been successfully fully taken, false otherwise
-
takeItemStacks
@NotNull public <T> @NotNull List<@NotNull T> takeItemStacks(@NotNull @NotNull List<@NotNull ItemStack> itemStacks, @NotNull @NotNull TransactionOption<T> option) TakesItemStack
s from the inventory and sends relevant updates to the viewer(s).- Parameters:
itemStacks
- items to take- Returns:
- the operation results
-
replaceItemStack
public void replaceItemStack(int slot, @NotNull @NotNull UnaryOperator<@NotNull ItemStack> operator) -
clear
public void clear()Clears the inventory and send relevant update to the viewer(s). -
update
public abstract void update() -
getItemStack
Gets theItemStack
at the specified slot.- Parameters:
slot
- the slot to check- Returns:
- the item in the slot
slot
-
getItemStacks
Gets all theItemStack
in the inventory.Be aware that the returned array does not need to be the original one, meaning that modifying it directly may not work.
- Returns:
- an array containing all the inventory's items
-
getSize
public int getSize()Gets the size of the inventory.- Returns:
- the inventory's size
-
getInnerSize
public int getInnerSize()Gets the size of the "inner inventory" (which includes only "usable" slots).- Returns:
- inner inventory's size
-
getInventoryConditions
Gets all theInventoryCondition
of this inventory.- Returns:
- a modifiable
List
containing all the inventory conditions
-
addInventoryCondition
Adds a newInventoryCondition
to this inventory.- Parameters:
inventoryCondition
- the inventory condition to add
-
copyContents
Places all the items ofitemStacks
into the internal array.- Parameters:
itemStacks
- the array to copy the content from- Throws:
IllegalArgumentException
- if the size of the array is not equal togetSize()
NullPointerException
- ifitemStacks
contains one null element or more
-
tagHandler
- Specified by:
tagHandler
in interfaceTaggable
-