-
Notifications
You must be signed in to change notification settings - Fork 3
3. Using MenuObjects
Once you've created your Menu, you'll want to add some objects to it. To do this, you're going to use MenuObjects. MenuObjects are very much akin to ItemStacks. However, they have Coordinates and an ActionHandler assigned to them. They're the objects that you'll use to detect clicks. You can use them either as displays or buttons. Here I will explain the process of making a simple display object (we'll add callbacks [click listeners] in the next tutorial).
You can initialize a MenuObject in two ways. First, the easiest. To make a MenuObject you can use the ItemStack constructor. First, make an ItemStack, then just put it as the only parameter while initializing the MenuObject. Example:
ItemStack itemStack = new ItemStack(Material.EMERALD);
MenuObject menuObject = new MenuObject(itemStack);This is the simplest way of doing it. This way, you can easily put up items onto the Menu without needing to fill out any other unnecessary fields. Easy, right?
Don't forget, you can also specify the display name, ItemStack, lore, and more:
ItemStack itemStack = new ItemStack(Material.WOOL, 1, 5);
ItemMeta itemMeta = itemStack.getItemMeta(); // You must declare a separate variable for the meta for this to work. i.e. You can NOT just use: itemStack.getItemMeta().setDisplayName("§aWool");
itemMeta.setDisplayName("§aWool"); // Using §-codes instead of ChatColors is a personal preference of mine.
List<String> lore = new ArrayList<>(); // Java 6 and below, make sure to specify "String" between the <>s
lore.add("§aSome cool lore.");
lore.add("§eMore cool lore.");
itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta); // Make sure you set the ItemMeta for things to work.
MenuObject menuObject = new MenuObject(itemStack);However, if you don't really like dragging it out like this, you can use one line to achieve the exact same thing with method #2:
MenuObject menuObject = new MenuObject(Material.WOOL, 5, "§aWool", Arrays.asList("§aSome cool lore.", "§eMore cool lore."));You should only go with method #2 if:
- You don't really care about Enchantment data
- You don't want the amount of items in the ItemStack to be anything except 1
Once we've made our MenuObject, we'll want to add it to a Menu. I'll be using a Menu stored in a variable called, well, "menu."
To begin with, let's decide where we want to place it on the Menu. This is going to involve Coordinates, so be sure to read the reference I've written on the subject to be able to understand this next bit.
I'm going to place a menu object called "menuObject" three spaces to the right, and four spaces down (in other words, [X: 3, Y: 4]) on a 45-slot Menu (Max X: 9, Max Y: 5). We'll need to make a new Coordinates object and put the details in, then sub that in alongside our MenuObject with the Menu#setItemAt(Coordinates, MenuObject) method:
// Given variables:
// menu = our inventory menu
// menuObject = a MenuObject
Coordinates coordinates = new Coordinates(menu, 3, 4);
menu.setItemAt(coordinates, menuObject);Test out the method and you should see your MenuObject at the location. Pretty cool, right?
Well, kind of. It doesn't really do much. But you can change that in the next tutorial, listed below: