Normal inventories
Every normal inventory will be a class that extends VitalInventory and is annotated with @VitalInventory.Info.
@VitalInventory.Info(type = VitalInventory.Type.GENERIC_9X1, name = "My inventory", background = Material.COBBLESTONE)
public class MyInventory extends VitalInventory {
}
You define a background for your inventory using the info annotation, by default, the background will be AIR (empty slots).
To fill this inventory with items, use the setItem(Player, int, ItemStack, Function<InventoryClickEvent>) method.
As you can see from the method signature, this method can receive a function that takes in an InventoryClickEvent, which will automatically be called when this item is clicked by a player.
Simply set all items in the onUpdate(Player) lifecycle.
@VitalInventory.Info(type = VitalInventory.Type.GENERIC_9X1, name = "My inventory", background = Material.COBBLESTONE)
public class MyInventory extends VitalInventory {
@Override
public void onUpdate(Player player) {
setItem(player, 0, new ItemStack(Material.STICK), e -> {
// Do something when this item is clicked.
});
// ...
}
}
To open this inventory for a player, use open(Player).
myInventory.open(myPlayer);
Additionally, you can also pass in a previous inventory, so players can automatically navigate back to a previous inventory by just clicking outside the inventory view (background).
myInventory.open(myPlayer, myPreviousInventory);
Vital will automatically handle all events and route them to your correct inventory and the item that was clicked.
Note that every player that opens an inventory menu will have its very own and unique inventory.
Inventories are never shared across multiple players and will always be different when newly opened.
That's why we want to mostly set our items during theonUpdate(Player)lifecycle instead ofonUpdate().Some special cases are, of course, if you do not need to show translated items or player-specific data.
Lifecycles
| Lifecycle | Usage |
|---|---|
onOpen(Player) | Perform an action when the given player opens this inventory (set items ONCE, etc.). |
onUpdate() | Perform an action when this inventory is updated for all players (set items that are always the same for all players, no translations, no specific data, etc.). |
onUpdate(Player) | Perform an action when this inventory is updated for the given player (set items for that player, translated data, specific data, etc.). |
onClick(InventoryClickEvent) | Perform an action when a slot is clicked in this inventory. Should only rarely be used since all click events are automatically routed to your item when you use setItem. |
onClose(Player) | Perform an action when this inventory is closed for the given player (play sound, remove stuff, etc.). |