Class VirtualInventory
Inventory
implementation that is identified by a UUID
and backed by a simple ItemStack
array.- See Also:
-
Constructor Summary
ConstructorDescriptionVirtualInventory
(int size) Constructs a newVirtualInventory
VirtualInventory
(int @NotNull [] maxStackSizes) Constructs a newVirtualInventory
VirtualInventory
(int size, @Nullable org.bukkit.inventory.ItemStack @Nullable [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
VirtualInventory
(@Nullable UUID uuid, int size) Constructs a newVirtualInventory
VirtualInventory
(@Nullable UUID uuid, int @NotNull [] maxStackSizes) Constructs a newVirtualInventory
VirtualInventory
(@Nullable UUID uuid, int size, @Nullable org.bukkit.inventory.ItemStack @Nullable [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
VirtualInventory
(@Nullable UUID uuid, @Nullable org.bukkit.inventory.ItemStack @NotNull [] items) Constructs a newVirtualInventory
VirtualInventory
(@Nullable UUID uuid, @Nullable org.bukkit.inventory.ItemStack @NotNull [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
VirtualInventory
(@Nullable org.bukkit.inventory.ItemStack @NotNull [] items) Constructs a newVirtualInventory
VirtualInventory
(@Nullable org.bukkit.inventory.ItemStack @NotNull [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
VirtualInventory
(VirtualInventory inventory) Creates a copy of the givenVirtualInventory
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addResizeHandler
(@NotNull BiConsumer<@NotNull Integer, @NotNull Integer> resizeHandler) Adds a handler that is called every time thisVirtualInventory
is resized.static VirtualInventory
deserialize
(byte[] bytes) Deserializes aVirtualInventory
from a byte array.static VirtualInventory
Deserializes aVirtualInventory
from anInputStream
.@Nullable org.bukkit.inventory.ItemStack
getItem
(int slot) Gets a clone of theItemStack
on that slot.@Nullable org.bukkit.inventory.ItemStack @NotNull []
getItems()
Gets a copy of the contents of thisInventory
.int
getMaxSlotStackSize
(int slot) Gets the maximum stack size for a specific slot while ignoring max stack size of theItemStack
on it.int @NotNull []
Gets the array of max stack sizes for thisInventory
.@Nullable List
<@NotNull BiConsumer<@NotNull Integer, @NotNull Integer>> Gets the handlers that are called every time thisVirtualInventory
is resized.int
getSize()
Gets the size of thisInventory
.org.bukkit.inventory.ItemStack
getUnsafeItem
(int slot) Gets theItemStack
on that slot.@Nullable org.bukkit.inventory.ItemStack @NotNull []
Gets theItemStacks
thisInventory
contains.@NotNull UUID
getUuid()
Gets theUUID
of thisVirtualInventory
.void
removeResizeHandler
(@NotNull BiConsumer<@NotNull Integer, @NotNull Integer> resizeHandler) Removes a handler that is called every time thisVirtualInventory
is resized.void
resize
(int size) Changes the size of theVirtualInventory
.byte[]
Serializes thisVirtualInventory
to a byte array.void
serialize
(OutputStream out) Serializes thisVirtualInventory
to anOutputStream
.protected void
setCloneBackingItem
(int slot, @Nullable org.bukkit.inventory.ItemStack itemStack) Clones the givenItemStack
and sets in the backing array of thisInventory
.protected void
setDirectBackingItem
(int slot, @Nullable org.bukkit.inventory.ItemStack itemStack) Sets theItemStack
in the backing array of thisInventory
without explicitly cloning it.void
setMaxStackSize
(int slot, int maxStackSize) Sets the maximum allowed stack size on a specific slot.void
setMaxStackSizes
(int @NotNull [] stackSizes) Sets the array of max stack sizes for thisInventory
.void
setResizeHandlers
(@Nullable List<@NotNull BiConsumer<@NotNull Integer, @NotNull Integer>> resizeHandlers) Sets the handlers that are called every time thisVirtualInventory
is resized.Methods inherited from class xyz.xenondevs.invui.inventory.Inventory
addItem, addItemAmount, addWindow, callPostUpdateEvent, callPreUpdateEvent, canHold, canHold, collectSimilar, collectSimilar, contains, containsSimilar, count, countSimilar, forceSetItem, getGuiPriority, getItemAmount, getMaxSlotStackSize, getMaxSlotStackSize, getMaxStackSize, getMaxStackSize, getMaxStackSize, getPostUpdateHandler, getPreUpdateHandler, getWindows, hasEmptySlot, hasEventHandlers, hasItem, isEmpty, isFull, isSynced, modifyItem, notifyWindows, putItem, removeFirst, removeFirstSimilar, removeIf, removeSimilar, removeWindow, replaceItem, setGuiPriority, setItem, setItemAmount, setItemSilently, setPostUpdateHandler, setPreUpdateHandler, simulateAdd, simulateAdd, simulateMultiAdd, simulateSingleAdd
-
Constructor Details
-
VirtualInventory
public VirtualInventory(@Nullable @Nullable UUID uuid, int size, @Nullable @Nullable org.bukkit.inventory.ItemStack @Nullable [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
- Parameters:
uuid
- TheUUID
of thisVirtualInventory
. Can be null, only used for serialization.size
- The amount of slots thisVirtualInventory
has.items
- A predefined array of content. Can be null. Will not get copied!maxStackSizes
- An array of maximum allowed stack sizes for each slot in theVirtualInventory
. Can be null for 64.- Throws:
IllegalArgumentException
- If the given size does not match the length of the items array or the length of the stackSizes array.IllegalArgumentException
- If the items array contains airItemStacks
.
-
VirtualInventory
public VirtualInventory(int size, @Nullable @Nullable org.bukkit.inventory.ItemStack @Nullable [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
- Parameters:
size
- The amount of slots thisVirtualInventory
has.items
- A predefined array of content. Can be null. Will not get copied!maxStackSizes
- An array of maximum allowed stack sizes for each slot in theVirtualInventory
. Can be null for 64.
-
VirtualInventory
public VirtualInventory(@Nullable @Nullable UUID uuid, @Nullable @Nullable org.bukkit.inventory.ItemStack @NotNull [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
- Parameters:
uuid
- TheUUID
of thisVirtualInventory
. Can be null, only used for serialization.items
- A predefined array of content. Will not get copied!maxStackSizes
- An array of maximum allowed stack sizes for each slot in theVirtualInventory
. Can be null for 64.
-
VirtualInventory
public VirtualInventory(@Nullable @Nullable org.bukkit.inventory.ItemStack @NotNull [] items, int @Nullable [] maxStackSizes) Constructs a newVirtualInventory
- Parameters:
items
- A predefined array of content. Will not get copied!maxStackSizes
- An array of maximum allowed stack sizes for each slot in theVirtualInventory
. Can be null for 64.
-
VirtualInventory
public VirtualInventory(@Nullable @Nullable UUID uuid, @Nullable @Nullable org.bukkit.inventory.ItemStack @NotNull [] items) Constructs a newVirtualInventory
- Parameters:
uuid
- TheUUID
of thisVirtualInventory
. Can be null, only used for serialization.items
- A predefined array of content. Will not get copied!
-
VirtualInventory
public VirtualInventory(@Nullable @Nullable org.bukkit.inventory.ItemStack @NotNull [] items) Constructs a newVirtualInventory
- Parameters:
items
- A predefined array of content. Will not get copied!
-
VirtualInventory
Constructs a newVirtualInventory
- Parameters:
uuid
- TheUUID
of thisVirtualInventory
. Can be null, only used for serialization.maxStackSizes
- An array of maximum allowed stack sizes for each slot in theVirtualInventory
.
-
VirtualInventory
public VirtualInventory(int @NotNull [] maxStackSizes) Constructs a newVirtualInventory
- Parameters:
maxStackSizes
- An array of maximum allowed stack sizes for each slot in theVirtualInventory
.
-
VirtualInventory
Constructs a newVirtualInventory
- Parameters:
uuid
- TheUUID
of thisVirtualInventory
. Can be null, only used for serialization.size
- The amount of slots thisVirtualInventory
has.
-
VirtualInventory
public VirtualInventory(int size) Constructs a newVirtualInventory
- Parameters:
size
- The amount of slots thisVirtualInventory
has.
-
VirtualInventory
Creates a copy of the givenVirtualInventory
.- Parameters:
inventory
- TheVirtualInventory
to copy.
-
-
Method Details
-
deserialize
Deserializes aVirtualInventory
from a byte array.- Parameters:
bytes
- The byte array to deserialize from.- Returns:
- The deserialized
VirtualInventory
.
-
deserialize
Deserializes aVirtualInventory
from anInputStream
.- Parameters:
in
- TheInputStream
to deserialize from.- Returns:
- The deserialized
VirtualInventory
.
-
serialize
public byte[] serialize()Serializes thisVirtualInventory
to a byte array.This method only serializes the
UUID
andItemStacks
.- Returns:
- The serialized data.
-
serialize
Serializes thisVirtualInventory
to anOutputStream
.This method only serializes the
UUID
andItemStacks
.- Parameters:
out
- TheOutputStream
to write serialized data to.
-
setResizeHandlers
public void setResizeHandlers(@Nullable @Nullable List<@NotNull BiConsumer<@NotNull Integer, @NotNull Integer>> resizeHandlers) Sets the handlers that are called every time thisVirtualInventory
is resized.- Parameters:
resizeHandlers
- The handlers to set.
-
getResizeHandlers
@Nullable public @Nullable List<@NotNull BiConsumer<@NotNull Integer,@NotNull Integer>> getResizeHandlers()Gets the handlers that are called every time thisVirtualInventory
is resized.- Returns:
- The handlers.
-
addResizeHandler
public void addResizeHandler(@NotNull @NotNull BiConsumer<@NotNull Integer, @NotNull Integer> resizeHandler) Adds a handler that is called every time thisVirtualInventory
is resized.- Parameters:
resizeHandler
- The handler to add.
-
removeResizeHandler
public void removeResizeHandler(@NotNull @NotNull BiConsumer<@NotNull Integer, @NotNull Integer> resizeHandler) Removes a handler that is called every time thisVirtualInventory
is resized.- Parameters:
resizeHandler
- The handler to remove.
-
resize
public void resize(int size) Changes the size of theVirtualInventory
.ItemStacks
in slots which are no longer valid will be removed from theVirtualInventory
. This method does not call an event.- Parameters:
size
- The new size of theVirtualInventory
-
setMaxStackSizes
public void setMaxStackSizes(int @NotNull [] stackSizes) Sets the array of max stack sizes for thisInventory
.- Parameters:
stackSizes
- The array defining the max stack sizes for thisInventory
.
-
setMaxStackSize
public void setMaxStackSize(int slot, int maxStackSize) Sets the maximum allowed stack size on a specific slot.- Parameters:
slot
- The slotmaxStackSize
- The max stack size
-
getUuid
Gets theUUID
of thisVirtualInventory
.- Returns:
- The
UUID
of thisVirtualInventory
.
-
getSize
public int getSize()Description copied from class:Inventory
Gets the size of thisInventory
. -
getMaxStackSizes
public int @NotNull [] getMaxStackSizes()Description copied from class:Inventory
Gets the array of max stack sizes for thisInventory
.- Specified by:
getMaxStackSizes
in classInventory
- Returns:
- The array defining the max stack sizes for this
Inventory
-
getMaxSlotStackSize
public int getMaxSlotStackSize(int slot) Description copied from class:Inventory
Gets the maximum stack size for a specific slot while ignoring max stack size of theItemStack
on it.- Specified by:
getMaxSlotStackSize
in classInventory
- Parameters:
slot
- The slot- Returns:
- The maximum stack size on that slo
-
getItems
@Nullable public @Nullable org.bukkit.inventory.ItemStack @NotNull [] getItems()Description copied from class:Inventory
Gets a copy of the contents of thisInventory
.It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.
-
getUnsafeItems
@Nullable public @Nullable org.bukkit.inventory.ItemStack @NotNull [] getUnsafeItems()Description copied from class:Inventory
Gets theItemStacks
thisInventory
contains. Depending on the implementation, this method may return a copy, a deep copy, or the actual backing item stack array. Modifying the returned array might or might not reflect in thisInventory
.It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.
- Specified by:
getUnsafeItems
in classInventory
- Returns:
- The
ItemStacks
thisInventory
contains.
-
getItem
@Nullable public @Nullable org.bukkit.inventory.ItemStack getItem(int slot) Description copied from class:Inventory
Gets a clone of theItemStack
on that slot.It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.
-
getUnsafeItem
public org.bukkit.inventory.ItemStack getUnsafeItem(int slot) Description copied from class:Inventory
Gets theItemStack
on that slot. Depending on the implementation, this method may a copy of or the actual backingItemStack
.It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.
- Specified by:
getUnsafeItem
in classInventory
- Parameters:
slot
- The slot- Returns:
- The
ItemStack
on the given slot.
-
setCloneBackingItem
protected void setCloneBackingItem(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack) Description copied from class:Inventory
Clones the givenItemStack
and sets in the backing array of thisInventory
.This method should never be invoked with an air / empty item stack. Those should always be represented by null.
- Specified by:
setCloneBackingItem
in classInventory
- Parameters:
slot
- The slotitemStack
- TheItemStack
to be set
-
setDirectBackingItem
protected void setDirectBackingItem(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack) Description copied from class:Inventory
Sets theItemStack
in the backing array of thisInventory
without explicitly cloning it. Depending on the implementation, theItemStack
might still be cloned.This method should never be invoked with an air / empty item stack. Those should always be represented by null.
- Specified by:
setDirectBackingItem
in classInventory
- Parameters:
slot
- The slotitemStack
- TheItemStack
to be set
-