Login Register


Plugin Development | Tutorial 1: Inventories

#1
(This post was last modified: 12-24-2016, 03:15 AM by Blessings.)

Today we will be learning about inventories and their methods!

To create an inventory, we will be using the following code below.
  1. 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.
  2. 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.
  3. The final argument that this method takes is, "Your Inventory Name" which is considered a string. This is what your inventory will look like.
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.

Code:
Inventory inv3 = Bukkit.createInventory(null, 54, "Your Inventory Name!");
  • 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!".
// Creating the Item
Code:
ItemStack STONE_BLOCK = new ItemStack(Material.STONE, 1);
ItemMeta STONE_META = STONE_BLOCK.getItemMeta();
STONE_META.setDisplayName("This is our super cool name!");
STONE_BLOCK.setItemMeta(STONE_META);




// Setting the item in a certain location
Code:
inventory.setItem(0, STONE_BLOCK);




// Adding the item to the next available slot in your inventory.
Code:
inventory.addItem(STONE_BLOCK);



// Using this as an example (Passing the instance of a player to the CommandSender in a CommandExecutor.)
Code:
Player player = (Player) sender;

player.openInventory(inventory);


[Image: 5234f1ab87075d0db6dddc0f9b7b1579.png]


_________________________________________________________________________________________________________________________________________________________________
Reply
#2

Superb tutorial sir! Keep them going! Smile


Create a brighter future
[Image: pwg86Hx.png]
"To live a creative life, we must first lose our fear of being wrong"
Reply
#3

(12-23-2016, 04:41 PM)katos Wrote: Superb tutorial sir! Keep them going! Smile

Will do!
Reply
#4

(12-24-2016, 03:04 AM)Blessings Wrote:
(12-23-2016, 04:41 PM)katos Wrote: Superb tutorial sir! Keep them going! Smile

Will do!

Good to hear! Smile


Create a brighter future
[Image: pwg86Hx.png]
"To live a creative life, we must first lose our fear of being wrong"
Reply




Users browsing this thread:
1 Guest(s)