Virtual Inventory
A VirtualInventory is an inventory that is not directly tied to a physical inventory in the game, but can be used to represent a custom inventory for a GUI. It allows you to manage the items in the inventory and handle interactions with it without needing to create an actual Inventory object in Bukkit.
This can be useful for GUIs that have dynamic content or need to manage multiple inventories at once, as it allows you to keep track of the inventory state and update it as needed without having to worry about the underlying Bukkit inventory mechanics.
The VirtualInventory class provides methods for setting and getting items in the inventory, as well as registering GUIs that are using the inventory so that they can be updated when changes are made. It also includes a method for handling click events on the inventory, which can be overridden to implement custom behavior when items are interacted with.
Parameters
The size of the virtual inventory, which determines how many slots it has. This should be a positive integer and is typically a multiple of 9 to fit standard Minecraft inventory layouts.
An optional array of ItemStacks to initialize the inventory with. If no array is provided, the inventory will be initialized with null values (empty slots). The length of this array should match the specified size of the inventory.
Types
An ItemProvider that provides the item from a specific slot in the virtual inventory. This can be used to create dynamic GUI elements that reflect the current state of the virtual inventory, allowing you to display items from the inventory in the GUI and have them update automatically when the inventory changes. The slot property can be updated to change which slot's item is being provided, and the provider will refresh to reflect the new slot's item. When clicked, this provider does nothing by default, but it can be overridden to implement custom behavior when the item is interacted with in the GUI.
Functions
Allows for array-like access to the virtual inventory's items using the bracket notation. This operator function delegates to the getItem method, enabling you to retrieve items from the inventory using syntax like virtualInventoryslot. If the slot index is out of bounds or the slot is empty, this will return null. This provides a convenient way to access items in the virtual inventory without needing to call getItem directly.
Retrieves the item in the specified slot of the virtual inventory. If the slot index is out of bounds (not between 0 and size-1), this method will return null. Otherwise, it returns a clone of the ItemStack at the specified slot, allowing callers to safely modify the returned ItemStack without affecting the inventory's internal state. If the slot is empty (null), this method will also return null.
Handles click events on the virtual inventory. This method should be called when a click event occurs in a GUI that is associated with this virtual inventory. It checks if any of the slot changes in the click event are outside the bounds of the inventory size, and if so, it cancels the event to prevent invalid interactions. If the click event is valid, it calls the onPreUpdateEvent method to allow for any necessary processing before updating the inventory. Then, it schedules a task to run shortly after the click event to update the inventory based on the slot changes, setting the items in the virtual inventory to match the items in the physical inventory at the corresponding slots. Finally, it calls the onPostUpdateEvent method after updating the inventory to allow for any additional processing or cleanup after the update is complete.
Called after the virtual inventory has been updated in response to a click event. This method can be overridden to perform any necessary processing or cleanup after the inventory has been modified based on the user's interaction. For example, you could use this method to trigger additional updates, log changes, or perform any actions that should occur after the inventory state has been changed. By default, this method does nothing, but it provides a hook for subclasses to implement custom behavior after the inventory update is complete.
Called before the virtual inventory is updated in response to a click event. This method can be overridden to perform any necessary processing or validation before the inventory is modified based on the user's interaction. For example, you could use this method to check for specific conditions, modify the click event, or cancel the event if certain criteria are not met. By default, this method does nothing, but it provides a hook for subclasses to implement custom behavior before the inventory update occurs.
Refreshes all registered GUIs that are associated with this virtual inventory, prompting them to update their display to reflect the current state of the inventory. This method can be called after making multiple changes to the inventory to ensure that all GUIs are updated at once, rather than refreshing each GUI individually for every change. It iterates through all registered GUIs and calls their refreshVirtualInventory method with the corresponding inventoryId, allowing them to update their display based on the latest state of the virtual inventory.
Registers a GUI to receive updates from this virtual inventory. When a GUI is registered, it will be notified of any changes to the inventory and can update its display accordingly. The inventoryId parameter is used to identify which virtual inventory the GUI is associated with, allowing for multiple virtual inventories to be managed by the same GUI if needed.
Allows for array-like assignment to the virtual inventory's items using the bracket notation. This operator function delegates to the setItem method, enabling you to set items in the inventory using syntax like virtualInventoryslot = itemStack. If the provided itemStack is null, has an amount of 0, or is of type AIR, the specified slot will be cleared (set to null). Otherwise, the provided ItemStack will be set in the inventory at the specified slot. This provides a convenient way to modify items in the virtual inventory without needing to call setItem directly.
Sets the item in the specified slot of the virtual inventory. If the provided itemStack is null, has an amount of 0, or is of type AIR, the slot will be set to null (empty). After updating the item in the inventory, this method will notify all registered GUIs to refresh the corresponding slot in their display, ensuring that any changes to the inventory are reflected in the GUI.
Unregisters a GUI from this virtual inventory, so that it will no longer receive updates when the inventory changes. This is useful for cleaning up references to GUIs that are no longer in use or have been closed, preventing memory leaks and ensuring that only active GUIs are updated with changes to the inventory.