Class VirtualInventory
java.lang.Object
de.studiocode.invui.virtualinventory.VirtualInventory
-
Constructor Summary
ConstructorsConstructorDescriptionVirtualInventory(@Nullable UUID uuid, int size) Constructs a newVirtualInventoryVirtualInventory(@Nullable UUID uuid, int size, @Nullable org.bukkit.inventory.ItemStack[] items, int[] stackSizes) Constructs a newVirtualInventory -
Method Summary
Modifier and TypeMethodDescriptionintaddItem(@Nullable UpdateReason updateReason, org.bukkit.inventory.ItemStack itemStack) Adds anItemStackto theVirtualInventory.intaddItemAmount(@Nullable UpdateReason updateReason, int slot, int amount) Adds a specific amount to anItemStackon a slot while respecting the maximum allowed stack size on that slot.voidAdds aWindowto the set ofWindows, telling theVirtualInventorythat its contents are now being displayed in thatWindow.callAfterUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable org.bukkit.inventory.ItemStack newItemStack) Creates anInventoryUpdatedEventand calls theinventoryUpdatedHandlerto handle it.callPreUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable org.bukkit.inventory.ItemStack newItemStack) Creates anItemUpdateEventand calls theitemUpdateHandlerto handle it.booleanbooleancanHold(@NotNull org.bukkit.inventory.ItemStack itemStack, @NotNull org.bukkit.inventory.ItemStack... itemStacks) intcollectToCursor(@Nullable UpdateReason updateReason, org.bukkit.inventory.ItemStack itemStack) Finds allItemStacks similar to the providedItemStackand removes them from their slot until the maximum stack size of theMaterialis reached.booleanforceSetItemStack(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack itemStack) Changes theItemStackon a specific slot to that one, regardless of what was previously on that slot.intgetAmount(int slot) Gets the amount of items on a slot.intGets the priority for shift-clickingItemStacksinto aGUIorg.bukkit.inventory.ItemStack[]getItems()Gets a copy of the contents of thisVirtualInventory.org.bukkit.inventory.ItemStackgetItemStack(int slot) Gets a clone of theItemStackon that slot.intgetMaxSlotStackSize(int slot) Gets the maximum stack size for a specific slot while ignoring theItemStackon it and its own maximum stack size.intgetMaxStackSize(int slot, int alternative) Gets the maximum stack size for a specific slot.intgetSize()Gets the size of thisVirtualInventory.int[]Gets the array of max stack sizes for thisVirtualInventory.org.bukkit.inventory.ItemStack[]Gets the sameItemStack[]that backs thisVirtualInventory.org.bukkit.inventory.ItemStackgetUnsafeItemStack(int slot) Returns the actualItemStackon that slot.@NotNull UUIDgetUuid()Gets theUUIDof thisVirtualInventory.Gets a set ofWindows that display thisVirtualInventory.booleanhasItem(int slot) Checks if there is anItemStackon that slot.booleanisEmpty()Checks if theVirtualInventoryis empty.booleanisSynced(int slot, org.bukkit.inventory.ItemStack assumedStack) Checks if theItemStackon that slot is the same as the assumedItemStackprovided as parameter.voidintputItemStack(@Nullable UpdateReason updateReason, int slot, @NotNull org.bukkit.inventory.ItemStack itemStack) Adds anItemStackon a specific slot.voidremoveWindow(Window window) Removes aWindowfrom the set ofWindows, telling theVirtualInventorythat its contents are no longer being displayed in thatWindow.voidresize(int size) Changes the size of theVirtualInventory.voidsetGuiShiftPriority(int guiShiftPriority) voidsetInventoryUpdatedHandler(Consumer<InventoryUpdatedEvent> inventoryUpdatedHandler) Sets a handler which is called every time after something has been updated in theVirtualInventory.intsetItemAmount(@Nullable UpdateReason updateReason, int slot, int amount) Sets the amount of anItemStackon a slot to the given value while respecting the max allowed stack size on that slot.booleansetItemStack(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack itemStack) Changes theItemStackon a specific slot to the given one, regardless of what previously was on that slot.voidsetItemStackSilently(int slot, @Nullable org.bukkit.inventory.ItemStack itemStack) Changes theItemStackon a specific slot to that one, regardless of what was previously on that slot.voidsetItemUpdateHandler(Consumer<ItemUpdateEvent> itemUpdateHandler) Sets a handler which is called every time something gets updated in theVirtualInventory.voidsetMaxStackSize(int slot, int maxStackSize) Sets the maximum allowed stack size on a specific slot.voidsetMaxStackSizes(int[] maxStackSizes) Sets all the maximum allowed stack sizesint[]simulateAdd(@NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks) Simulates addingItemStacks to thisVirtualInventoryand returns the amount ofItemStacks that did not fit.int[]simulateAdd(@NotNull org.bukkit.inventory.ItemStack itemStack, @NotNull org.bukkit.inventory.ItemStack... itemStacks) Simulates addingItemStacks to thisVirtualInventoryand returns the amount ofItemStacks that did not fit.int[]simulateMultiAdd(@NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks) Simulates adding multipleItemStacks to thisVirtualInventoryand returns the amount ofItemStacks that did not fit.intsimulateSingleAdd(@NotNull org.bukkit.inventory.ItemStack itemStack) Returns the amount of items that wouldn't fit in the inventory when added.byte[]toString()
-
Constructor Details
-
VirtualInventory
public VirtualInventory(@Nullable @Nullable UUID uuid, int size, @Nullable @Nullable org.bukkit.inventory.ItemStack[] items, int[] stackSizes) Constructs a newVirtualInventory- Parameters:
uuid- TheUUIDof thisVirtualInventory. Can be null, only used for serialization.size- The amount of slots thisVirtualInventoryhas.items- A predefined array of content. Can be null. Will not get copied!stackSizes- An array of maximum allowed stack sizes for each slot in theVirtualInventory.
-
VirtualInventory
Constructs a newVirtualInventory- Parameters:
uuid- TheUUIDof thisVirtualInventory. Can be null, only used for serialization.size- The amount of slots thisVirtualInventoryhas.
-
-
Method Details
-
toByteArray
public byte[] toByteArray() -
getWindows
Gets a set ofWindows that display thisVirtualInventory.- Returns:
- An unmodifiable view of the set that contains all
Windows that display content of thisVirtualInventory.
-
addWindow
Adds aWindowto the set ofWindows, telling theVirtualInventorythat its contents are now being displayed in thatWindow.- Parameters:
window- TheWindowto be added.
-
removeWindow
Removes aWindowfrom the set ofWindows, telling theVirtualInventorythat its contents are no longer being displayed in thatWindow.- Parameters:
window- TheWindowto be removed.
-
notifyWindows
public void notifyWindows()Notifies allWindows displaying thisVirtualInventoryto update their representativeItemStacks. This method should only be called manually in very specific cases like when theItemMetaof anItemStackin this inventory has changed. -
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
-
setItemUpdateHandler
Sets a handler which is called every time something gets updated in theVirtualInventory.- Parameters:
itemUpdateHandler- The new item update handler
-
setInventoryUpdatedHandler
Sets a handler which is called every time after something has been updated in theVirtualInventory.- Parameters:
inventoryUpdatedHandler- The new handler
-
getGuiShiftPriority
public int getGuiShiftPriority()Gets the priority for shift-clickingItemStacksinto aGUI- Returns:
- The priority for shift-clicking,
VirtualInventorieswith a higher priority get prioritized.
-
setGuiShiftPriority
public void setGuiShiftPriority(int guiShiftPriority) Sets the priority for shift-clickingItemStacksinto aGUIwith multipleVirtualInventory. Not serialized withVirtualInventoryManager.serializeInventory(VirtualInventory, OutputStream).- Parameters:
guiShiftPriority- The priority for shift-clicking,VirtualInventorieswith a higher priority get prioritized.
-
getUuid
Gets theUUIDof thisVirtualInventory.- Returns:
- The
UUID
-
getSize
public int getSize()Gets the size of thisVirtualInventory.- Returns:
- How many slots this
VirtualInventoryhas.
-
getStackSizes
public int[] getStackSizes()Gets the array of max stack sizes for thisVirtualInventory.- Returns:
- The array defining the max stack sizes for this
VirtualInventory
-
getItems
public org.bukkit.inventory.ItemStack[] getItems()Gets a copy of the contents of thisVirtualInventory.- Returns:
- A deep copy of the
ItemStacks thisVirtualInventorycontains.
-
getUnsafeItems
public org.bukkit.inventory.ItemStack[] getUnsafeItems()Gets the sameItemStack[]that backs thisVirtualInventory.
As it is not clone, it should be handled carefully as changes done on that array will not call any Window updates (and create inconsistency between server and client). -
getItemStack
public org.bukkit.inventory.ItemStack getItemStack(int slot) Gets a clone of theItemStackon that slot.- Parameters:
slot- The slot- Returns:
- The
ItemStackon the given slot
-
getUnsafeItemStack
public org.bukkit.inventory.ItemStack getUnsafeItemStack(int slot) Returns the actualItemStackon that slot.
Not a clone, should be handled carefully as changes done on that item will not call any Window updates (and create inconsistency between server and client), in which case a manual call ofnotifyWindows()is needed.
Modifying thisItemStackwill not call anItemUpdateEvent.- Parameters:
slot- The slot- Returns:
- The actual
ItemStackon that slot
-
hasItem
public boolean hasItem(int slot) Checks if there is anItemStackon that slot.- Parameters:
slot- The Slot- Returns:
- If there is an
ItemStackon that slot.
-
getAmount
public int getAmount(int slot) Gets the amount of items on a slot.- Parameters:
slot- The slot- Returns:
- The amount of items on that slot
-
isEmpty
public boolean isEmpty()Checks if theVirtualInventoryis empty.- Returns:
- If there are no
ItemStacksin thisVirtualInventory
-
getMaxStackSize
public int getMaxStackSize(int slot, int alternative) Gets the maximum stack size for a specific slot. If there is anItemStackon that slot, the returned value will be the minimum of both the slot limit andMaterial.getMaxStackSize().- Parameters:
slot- The slotalternative- The alternative maximum stack size if noItemStackis placed on that slot. Should probably be the max stack size of theMaterialthat will be added.- Returns:
- The current maximum allowed stack size on the specific slot.
-
getMaxSlotStackSize
public int getMaxSlotStackSize(int slot) Gets the maximum stack size for a specific slot while ignoring theItemStackon it and its own maximum stack size.- Parameters:
slot- The slot- Returns:
- The maximum stack size on that slo
-
setMaxStackSizes
public void setMaxStackSizes(int[] maxStackSizes) Sets all the maximum allowed stack sizes- Parameters:
maxStackSizes- All max stack sizes
-
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
-
callPreUpdateEvent
public ItemUpdateEvent callPreUpdateEvent(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable @Nullable org.bukkit.inventory.ItemStack newItemStack) Creates anItemUpdateEventand calls theitemUpdateHandlerto handle it.- Parameters:
updateReason- TheUpdateReasonslot- The slot of the affectedItemStackpreviousItemStack- TheItemStackthat was previously on that slotnewItemStack- TheItemStackthat will be on that slot- Returns:
- The
ItemUpdateEventafter it has been handled by theitemUpdateHandler
-
callAfterUpdateEvent
public InventoryUpdatedEvent callAfterUpdateEvent(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable @Nullable org.bukkit.inventory.ItemStack newItemStack) Creates anInventoryUpdatedEventand calls theinventoryUpdatedHandlerto handle it.- Parameters:
updateReason- TheUpdateReasonslot- The slot of the affectedItemStackpreviousItemStack- TheItemStackthat was on that slot previously.newItemStack- TheItemStackthat is on that slot now.- Returns:
- The
InventoryUpdatedEventafter it has been handled by theinventoryUpdatedHandler
-
isSynced
public boolean isSynced(int slot, org.bukkit.inventory.ItemStack assumedStack) Checks if theItemStackon that slot is the same as the assumedItemStackprovided as parameter.- Parameters:
slot- The slotassumedStack- The assumedItemStack- Returns:
- If the
ItemStackon that slot is the same as the assumedItemStack
-
setItemStackSilently
public void setItemStackSilently(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack) Changes theItemStackon a specific slot to that one, regardless of what was previously on that slot.
This method does not call anItemUpdateEventand ignores the maximum allowed stack size of both theMaterialand the slot.
This method will always be successful.- Parameters:
slot- The slotitemStack- TheItemStackto set.
-
forceSetItemStack
public boolean forceSetItemStack(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack) Changes theItemStackon a specific slot to that one, regardless of what was previously on that slot.
This method ignores the maximum allowed stack size of both theMaterialand the slot.- Parameters:
updateReason- The reason used in theItemUpdateEvent.slot- The slotitemStack- TheItemStackto set.- Returns:
- If the action was successful
-
setItemStack
public boolean setItemStack(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack) Changes theItemStackon a specific slot to the given one, regardless of what previously was on that slot.
This method will fail if the givenItemStackdoes not completely fit inside because of the maximum allowed stack size.- Parameters:
updateReason- The reason used in theItemUpdateEvent.slot- The slotitemStack- TheItemStackto set.- Returns:
- If the action was successful
-
putItemStack
public int putItemStack(@Nullable @Nullable UpdateReason updateReason, int slot, @NotNull @NotNull org.bukkit.inventory.ItemStack itemStack) Adds anItemStackon a specific slot.- Parameters:
updateReason- The reason used in theItemUpdateEvent.slot- The slotitemStack- TheItemStackto add.- Returns:
- The amount of items that did not fit on that slot.
-
setItemAmount
Sets the amount of anItemStackon a slot to the given value while respecting the max allowed stack size on that slot.- Parameters:
updateReason- The reason used in theItemUpdateEvent.slot- The slotamount- The amount to change to.- Returns:
- The amount that it actually changed to.
- Throws:
IllegalStateException- If there is noItemStackon that slot.
-
addItemAmount
Adds a specific amount to anItemStackon a slot while respecting the maximum allowed stack size on that slot. Returns 0 if there is noItemStackon that slot.- Parameters:
updateReason- The reason used in theItemUpdateEvent.slot- The slotamount- The amount to add- Returns:
- The amount that was actually added.
-
addItem
public int addItem(@Nullable @Nullable UpdateReason updateReason, org.bukkit.inventory.ItemStack itemStack) Adds anItemStackto theVirtualInventory. This method does not work the same way as Bukkit's addItem method as it respects the max stack size of the item type.- Parameters:
updateReason- The reason used in theItemUpdateEvent.itemStack- TheItemStackto add- Returns:
- The amount of items that didn't fit
- See Also:
-
simulateAdd
public int[] simulateAdd(@NotNull @NotNull org.bukkit.inventory.ItemStack itemStack, @NotNull @NotNull org.bukkit.inventory.ItemStack... itemStacks) Simulates addingItemStacks to thisVirtualInventoryand returns the amount ofItemStacks that did not fit.- Returns:
- An array of integers representing the leftover amount for each
ItemStackprovided. The size of this array is always equal to the amount ofItemStacks provided as method parameters.
-
simulateAdd
public int[] simulateAdd(@NotNull @NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks) Simulates addingItemStacks to thisVirtualInventoryand returns the amount ofItemStacks that did not fit.- Returns:
- An array of integers representing the leftover amount for each
ItemStackprovided. The size of this array is always equal to the amount ofItemStacks provided as method parameters.
-
canHold
public boolean canHold(@NotNull @NotNull org.bukkit.inventory.ItemStack itemStack, @NotNull @NotNull org.bukkit.inventory.ItemStack... itemStacks) - Returns:
- If all provided
ItemStacks would fit if added.
-
canHold
- Returns:
- If all provided
ItemStacks would fit if added.
-
simulateSingleAdd
public int simulateSingleAdd(@NotNull @NotNull org.bukkit.inventory.ItemStack itemStack) Returns the amount of items that wouldn't fit in the inventory when added.
Note: This method does not add anyItemStacks to theVirtualInventory.- Parameters:
itemStack- TheItemStackto use- Returns:
- How many items wouldn't fit in the inventory when added
-
simulateMultiAdd
public int[] simulateMultiAdd(@NotNull @NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks) Simulates adding multipleItemStacks to thisVirtualInventoryand returns the amount ofItemStacks that did not fit.- Parameters:
itemStacks- TheItemStackto be used in the simulation- Returns:
- An array of integers representing the leftover amount for each
ItemStackprovided. The size of this array is always equal to the amount ofItemStacks provided as method parameters.
-
collectToCursor
public int collectToCursor(@Nullable @Nullable UpdateReason updateReason, org.bukkit.inventory.ItemStack itemStack) Finds allItemStacks similar to the providedItemStackand removes them from their slot until the maximum stack size of theMaterialis reached.- Parameters:
updateReason- The reason used in theItemUpdateEvent.itemStack- TheItemStackto find matches to- Returns:
- The amount of collected items
-
toString
-