Today we will be learning about inventories and their methods!
To create an inventory, we will be using the following code below.
When coding, remember when adding items to an inventory that the first slot of the inventory actually start at 0, making the last boxs position 53.
- The first argument, null, represents the inventory owner. In this case we have put it to null because we are not assigning the inventory to a specific player.
- The second argument, 54, is the size of the inventory. The number must be a multiple of nine due to the amount of boxes in each row.
- The final argument that this method takes is, "Your Inventory Name" which is considered a string. This is what your inventory will look like.
Inventory inv3 = Bukkit.createInventory(null, 54, "Your Inventory Name!");
// Creating the Item
- Here, we are creating an new ItemStack of stone with the quantity of 1.
- We are then declaring an ItemMeta variable as the ItemStack's meta.
- We are then accessing that variable and setting its display name to our string, "This is our super cool name!".
ItemStack STONE_BLOCK = new ItemStack(Material.STONE, 1);
ItemMeta STONE_META = STONE_BLOCK.getItemMeta();
STONE_META.setDisplayName("This is our super cool name!");
// Setting the item in a certain location
// Adding the item to the next available slot in your inventory.
// Using this as an example (Passing the instance of a player to the CommandSender in a CommandExecutor.)
Player player = (Player) sender;