Jump to content
Sign in to follow this  

Minecraft Tutorial #2 : Inventories

Recommended Posts

Today we will be learning about inventories and their methods!


To create an inventory, we will be using the following code below. 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.

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.


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!".

ItemStack STONE_BLOCK = new ItemStack(Material.STONE, 1);
ItemMeta STONE_META = STONE_BLOCK.getItemMeta();
STONE_META.setDisplayName("This is our super cool name!");

// Adding the items to the inventory

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

// Adding the item to the next available slot in your inventory.


Time to give our player our inventory!

// Using this as an example (Passing the instance of a player to the CommandSender in a CommandExecutor.)

Player player = (Player) sender;



5234f1ab87075d0db6dddc0f9b7b1579.png  VOILA! This is our inventory with our custom item in it!



This is a list of the different methods along with a description and their modifiers.


Modifier and Type                                                 Method and Description

HashMap<Integer,ItemStack> addItem(ItemStack... items)
Stores the given ItemStacks in the inventory.
HashMap<Integer,? extends ItemStack> all(int materialId)
Magic value
HashMap<Integer,? extends ItemStack> all(ItemStack item)
Finds all slots in the inventory containing any ItemStacks with the given ItemStack.
HashMap<Integer,? extends ItemStack> all(Material material)
Returns a HashMap with all slots and ItemStacks in the inventory with the given Material.
void clear()
Clears out the whole Inventory.
void clear(int index)
Clears out a particular slot in the index.
boolean contains(int materialId)
Magic value
boolean contains(int materialId, int amount)
Magic value
boolean contains(ItemStack item)
Checks if the inventory contains any ItemStacks matching the given ItemStack.
boolean contains(ItemStack item, int amount)
Checks if the inventory contains at least the minimum amount specified of exactly matching ItemStacks.
boolean contains(Material material)
Checks if the inventory contains any ItemStacks with the given material.
boolean contains(Material material, int amount)
Checks if the inventory contains any ItemStacks with the given material, adding to at least the minimum amount specified.
boolean containsAtLeast(ItemStack item, int amount)
Checks if the inventory contains ItemStacks matching the given ItemStack whose amounts sum to at least the minimum amount specified.
int first(int materialId)
Magic value
int first(ItemStack item)
Returns the first slot in the inventory containing an ItemStack with the given stack.
int first(Material material)
Finds the first slot in the inventory containing an ItemStack with the given material
int firstEmpty()
Returns the first empty Slot.
ItemStack[] getContents()
Returns all ItemStacks from the inventory
InventoryHolder getHolder()
Gets the block or entity belonging to the open inventory
ItemStack getItem(int index)
Returns the ItemStack found in the slot at the given index
Location getLocation()
Get the location of the block or entity which corresponds to this inventory.
int getMaxStackSize()
Returns the maximum stack size for an ItemStack in this inventory.
String getName()
Returns the name of the inventory
int getSize()
Returns the size of the inventory
ItemStack[] getStorageContents()
Return the contents from the section of the inventory where items can reasonably be expected to be stored.
String getTitle()
Returns the title of this inventory.
InventoryType getType()
Returns what type of inventory this is.
List<HumanEntity> getViewers()
Gets a list of players viewing the inventory.
ListIterator<ItemStack> iterator() 
ListIterator<ItemStack> iterator(int index)
Returns an iterator starting at the given index.
void remove(int materialId)
Magic value
void remove(ItemStack item)
Removes all stacks in the inventory matching the given stack.
void remove(Material material)
Removes all stacks in the inventory matching the given material.
HashMap<Integer,ItemStack> removeItem(ItemStack... items)
Removes the given ItemStacks from the inventory.
void setContents(ItemStack[] items)
Completely replaces the inventory's contents.
void setItem(int index, ItemStack item)
Stores the ItemStack at the given index of the inventory.
void setMaxStackSize(int size)
This method allows you to change the maximum stack size for an inventory.
void setStorageContents(ItemStack[] items)
Put the given ItemStacks into the storage slots

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

About us

MMO-Society is the Forum former known as Lordcraft, MMO-Society offers various of Resources to help you evolve your knowledge within Emulation, In the process we make sure to form a strong community bond in-between each other and thus we are proud to can admit that MMO-Society is possibly the friendliest Community Forum to currently exist.

Useful Quick-Links

  • Create New...