Skip to content
This repository was archived by the owner on May 18, 2020. It is now read-only.

Inventory API

Diogo Correia edited this page Dec 10, 2017 · 4 revisions

How to create an Inventory?

When I say "Inventory Menu", I mean, a custom inventory, with custom items. Different things can happen when items are clicked. This tutorial covers creating an inventory, using the inventory click event, and assigning items.

Part 1: Creating a inventory.

I am going to create an inventory, name "My custom Inventory", and have it have 9 slots.

Inventory inv = new Inventory(9, "My custom Inventory!");
//The first parameter is the slots in an inventory. Must be a multiple of 9. Can be up to 54 (over 54 the Client will render the slots out of the box).
//The second parameter is the inventory name. This will accept chat colors.

Part 2: Assign items to the inventory

inv.setItem(0, new ItemStack(Material.WOOL,1){
  @Override
  public void click(Click click) {
     //This method will be executed when the player clicks the item (Async)
  }
});
//The ItemStack constructor parameters are like the Bukkit parameters.
//The first parameter is the slot that is assigned to. Starts counting at 0

Part 2.1: Assign items to the inventory (With the ItemBuilder)

Item item = ItemBuilder.create()
		.id(1) //Change the item id
		.durbility(0) //Change the item durbility (parameter must be inside Short.MIN_VALUE and Short.MAX_VALUE)
		.amount(12) //The amount of the item (maximal Byte.MAX_VALUE minimal Byte.MIN_VALUE)
		.name("§aHello world") //Change the item name
		.lore("First") //Add a line at the lore
		.lore("Second") //Add a second line
		.glow() //Let the item glow (the enchant glow)
		.listener((click)->{ //Applay a click listener. The variable type of click is ItemStack.Click (Like the general click listener)
			click.getPlayer().sendMessage("Hey, you clicked me!"); //Just sending a message to the player who cliked at the item
		})
		.build(); //Lets build the item.
				  //Return type is an Item, but if the listener has a click listener then
				  // it's returned an instance of an ItemStack

Part 3: Open the Inventory

Player player = (Player) BungeeCord.getInstance().getPlayer("WolverinDEV");
//Get the player who will see the inventory. ProxiedPlayer can be cast to Player!

player.openInventory(inv);
//Open the inventory.

Clone this wiki locally