Tutorial Creating Custom Inventories 1.9

Discussion in 'Resources' started by Teeson1000, Mar 31, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Teeson1000

    Introduction
    If you don't know already, Custom Bukkit Inventories are basically custom menus for Minecraft. To make one, you'll need more than a basic understanding of Bukkit.

    Step 1: Creating your createItem Method
    First, you'll have to create your Menu Class which has all of the info for your Custom Inventory. Make sure to implement it as a Listener because we will have the InventoryClickEvent in it.
    After you create the class, type in this method below:

    Code:
    private ItemStack createItem(Material material, int amount, short color, String name, String desc) {
            ItemStack item = new ItemStack(material, amount, color);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            meta.setLore(Arrays.asList(c.white + desc));
            item.setItemMeta(meta);
            return item;
        }
    What it Does:
    This method will allow you to create the icons in your Bukkit Inventory.
    Parameters:
    Material = The material of the icon (ex: Material.DIRT)
    Amount = The amount of the inventory
    Color = The Color ID
    Name = The Name of the Item (can contain color codes)
    Desc = The Description, or Lore of the item.

    Step 2: Making the Menu
    Now we will create the class constructor, which has all the item initialization in it.

    Code:
    public MenuClass() {
        inv = Bukkit.createInventory(null, numberOfSlots, "Inventory Name");
    }
    Make sure to replace numberOfSlots with the, well, number of slots in your inventory, and Inventory Name with the name of the Inventory. The name will be the String that is displayed at the top right handed corner of the menu.
    NOTE: The number of slots parameter must be a multiple of 9, and at the most of 54 slots.

    Before we move on, add this outside of the constructor:

    Code:
    private Inventory inv;
    Step 3: Initializing the Items
    We will now make the items using our createItem method in our MenuClass.

    To make your icons, make an ItemStack outside of your constructor like this:

    Code:
    private ItemStack icon1, icon2, //name these whatever you want
    icon1 and icon2 are just examples, you can change them to whatever you want, and you can make as many as your want.

    This is where our createItem method comes in! To initialize the items in the constructor, type in the following: (I will be using the icon1 and icon2 as examples again)

    Code:
    icon1 = createItem(Material.LAVA_BUCKET, 1, (short) 0, "Lava Bukkit", "This is a lava bukkit.");
    icon2 = createItem(Material.STONE, 1, (short) 0, "Stone", "Plain old stone.");
    NOTE: That was an example! Replace the parameters with whatever you want.

    After you create the items, the items won't show up in the inventory just yet. You have to set the item in the slot of the inventory like so:

    Code:
    inv.setItem(slot, itemName);
    What it does:
    Allows you to set the slot of the item.
    Parameters:
    - slot (integer) the slot the item is in
    - itemName: The name of the item you made in the constructor.

    Because or inventory is private, we must make a method that will open the inventory like so:

    Code:
    public void openMenu(Player p) {
            p.openInventory(inv);
        }
    Remember when we made our inventory implement a listener? Well now we have to make the listener that listens for when the player clicks on an icon. We will be doing that with an InventoryClickEvent listener:

    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(banGUI.getName())) return;
            Player p = (Player) e.getWhoClicked();
    }
    Now we will use if / else if statements to find out if the player clicks this, then something happens.

    Code:
    if (e.getCurrentItem().getType() == Material.MATERIAL_NAME) {
        //something  happens
    } else if(e.getCurrentItem().getType() == Material.MATERIAL_NAME) {
        //something happnes
    }
    NOTE: You have to repeat the else if statements when listening for another icon.

    Now we are finished coding the inventory. Because this is a listener class, we must register it in our MainClass which extends JavaPlugin. See the http://wiki.bukkit.org/Event_API_Reference for help on registering events.

    Now, go to your command class or listener class that will open the inventory, and type in the following to open your inventory:

    Code:
    MenuClass.getInstance().openMenu(player);
    Finally, you can export your plugin and you're done!
    If you have any errors in your code, just tell me.
     
    Last edited: Apr 1, 2016
  2. Offline

    Zombie_Striker

    @Teeson1000
    1. You can delete the package line if you don't want people using your package.
    2. You don't need to comment out lines. Just delete them.
    3. This is not a tutorial as much as a Library.
    4. Use the Titles prefixes. Don't create your own.
    5. All menu manager has is a method to create skulls. Why do you need that class, since you can just move that method into the other class?
    6. Are items Test1 and Test2 important, or are the literally just for testing?
    7. getItemMeta will always return an object, because if the itemeta is null, getItemmeta will create it. Instead, use hasItemMeta.
    8. Don't use "equalsIgnoreCase" when comparing inventory titles. You know the exact capitalization for the title.
    9. There are a few violations of DRY (Don't Repeat Yourself). You basically copied 4 lines for both the magma cream and the cooked beef.
    10. OpenMainMenu only has one line in it. There is not a real need to have that one line have it's own method, as it is small and can be referenced anywhere.
    I know it's a huge list, but you might want to consider fixing these few things.
     
    mine-care likes this.
  3. Offline

    Regablith

    Wait. So you just posted a topic asking for help and now you're making a tutorial on the same subject? xD
     
    ChipDev and WolfMage1 like this.
  4. Offline

    Teeson1000

    I know right lol. I realized that too :/ I did that because this tutorial is making the inventory. THe other thread is about another feature for my own plugin
     
  5. Offline

    timtower Administrator Administrator Moderator

    Moved to resources
     
  6. Offline

    mcdorli

    @Teeson1000
    Then edit the part where forgot it. This isn't live speech, you can just change things.

    This tutorial is very very spoonfeedish, never say "If you want to copy paste it..." or "If you copied it...", just tepl the people to don't copy the thread, because then they won't learn from it.

    You don't own the admin.com domain, don't use it, use me.yourUsername.projectname
     
  7. Offline

    Zombie_Striker

    Libraries are not really for teaching. If this would be a tutorial, I agree that he should no allow people to just copy and paste the answer. However, this is labeled as a Library, so I have to assume that it is (even though it's formatted like a tutorial).
     
  8. Offline

    Teeson1000

    Okay. I just joined Bukkit yesterday and I am still getting used to how things are done.
    Thanks or the help again. And, I didn't change it to library.
     
    ChipDev likes this.
  9. Offline

    mcdorli

    Btw, this tutorial goes in the red area, when you say "if you want to create multiple menus, then create a new menu class". That's wrong on too many levels. Have you ever heard of inheritence or abstraction? This is really far from OOP.

    And also, you don't explain anything.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page