[Tutorial] Creating a Inventory

Discussion in 'Resources' started by InspectorFacepalm, May 10, 2013.

Thread Status:
Not open for further replies.
  1. If you want to create & open inventory this is how to do it, pretty much a example
    You can add more slots and everything
    Code:
          ItemStack slot0 = new ItemStack(Material.AnythingYouWant, 1);
            ItemStack slot1 = new ItemStack(Material.AnythingYouWant, 1);
            ItemStack slot2 = new ItemStack(Material.AnythingYouWant, 1);
            ItemStack slot3 = new ItemStack(Material.AnythingYouWant, 1);
    
    
            slot0.addEnchantment(Enchantment.EnchantmentOfYourChoice, 1);
            slot1.addEnchantment(Enchantment.EnchantmentOfYourChoice, 1);
            slot2.addEnchantment(Enchantment.EnchantmentOfYourChoice, 1);
            slot3.addEnchantment(Enchantment.EnchantmentOfYourChoice, 1);
    
            ItemMeta meta0 = slot0.getItemMeta();
            ItemMeta meta1 = slot1.getItemMeta();
            ItemMeta meta2 = slot2.getItemMeta();
            ItemMeta meta3 = slot3.getItemMeta();
    
    
            meta0.setDisplayName(ChatColor.DARK_GREEN + "Name");
            meta1.setDisplayName(ChatColor.AQUA + "Name");
            meta2.setDisplayName(ChatColor.DARK_RED + "Name");
            meta3.setDisplayName(ChatColor.BLUE + "Name");
    
    
            slot0.setItemMeta(meta0);
            slot1.setItemMeta(meta1);
            slot2.setItemMeta(meta2);
            slot3.setItemMeta(meta3);
    
    
            Inventory x104 = Bukkit.createInventory(null, 9, "Menu");
    
            x104.setItem(0, slot0);
            x104.setItem(1, slot1);
            x104.setItem(2, slot2);
            x104.setItem(3, slot3);
    
    
            p.openInventory(x104);
    
    So lets say I was making a inventory where I would do /inventory and it would come up as full diamond armor enchanted to protection I
    Here's how I would do it
    Code:
     ItemStack slot0 = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
            ItemStack slot1 = new ItemStack(Material.DIAMOND_BOOTS, 1);
            ItemStack slot2 = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
            ItemStack slot3 = new ItemStack(Material.DIAMOND_HELMET, 1);
    
    
            slot0.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
            slot1.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
            slot2.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
            slot3.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    
            ItemMeta meta0 = slot0.getItemMeta();
    
            ItemMeta meta1 = slot1.getItemMeta();
            ItemMeta meta2 = slot2.getItemMeta();
            ItemMeta meta3 = slot3.getItemMeta();
    
    
            meta0.setDisplayName(ChatColor.DARK_GREEN + "Protection I");
            meta1.setDisplayName(ChatColor.AQUA + "Protection I");
            meta2.setDisplayName(ChatColor.DARK_RED + "Protection I");
            meta3.setDisplayName(ChatColor.BLUE + "Protection I");
    
    
            slot0.setItemMeta(meta0);
            slot1.setItemMeta(meta1);
            slot2.setItemMeta(meta2);
            slot3.setItemMeta(meta3);
    
    
            Inventory x104 = Bukkit.createInventory(null, 9, "Enchant Menu");
    
            x104.setItem(0, slot0);
            x104.setItem(1, slot1);
            x104.setItem(2, slot2);
            x104.setItem(3, slot3);
    
    
            p.openInventory(x104);
    
    And then I would add a Listener class putting this
    Code:
    (cevent.getInventory().getName() == "Enchant Menu")) {
            
          
            if (cevent.getSlot() == 0) {
                Player pl = (Player)cevent.getWhoClicked();
    
              pl.closeInventory();
              Inventory target = pl.getInventory();
              ItemStack diamondlegg = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
    
              diamondlegg.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    
              ItemMeta mm = diamondlegg.getItemMeta();
              diamondlegg.setItemMeta(mm);
              target.addItem(new ItemStack[] { diamondlegg });
    
          }
          if (cevent.getSlot() == 1) {
              Player pl = (Player)cevent.getWhoClicked();
              pl.closeInventory();
              Inventory target = pl.getInventory();
              ItemStack diamondboots = new ItemStack(Material.DIAMOND_BOOTS, 1);
    
              diamondboots.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    
              ItemMeta mm = diamondboots.getItemMeta();
              diamondboots.setItemMeta(mm);
              target.addItem(new ItemStack[] { diamondboots });
    
          }
    
          if (cevent.getSlot() == 2) {
              Player pl = (Player)cevent.getWhoClicked();
              pl.closeInventory();
              Inventory target = pl.getInventory();
              ItemStack diamondchest = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
    
              diamondchest.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    
              ItemMeta mm = diamondchest.getItemMeta();
              diamondchest.setItemMeta(mm);
              target.addItem(new ItemStack[] { diamondchest });
       
          }
    
          if (cevent.getSlot() == 3) {
            Player pl = (Player)cevent.getWhoClicked();
            pl.closeInventory();
            Inventory target = pl.getInventory();
            ItemStack diamondhelm = new ItemStack(Material.DIAMOND_HELMET, 1);
    
            diamondhelm.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    
            ItemMeta mm = diamondhelm.getItemMeta();
            diamondhelm.setItemMeta(mm);
            target.addItem(new ItemStack[] { diamondhelm });
     
    Hopefully this helped =/=
     
    CryLegend and C0nsole like this.
  2. Offline

    CryLegend

    Thanks for help ;)
     
  3. Offline

    WazzapDoc

    Nice tutorial,

    If you would like to make a chest using /inventory so players can store items in it and it saves into a config file, how would you do it?
     
  4. Offline

    Door Knob

    I prefer to do it using arrays, like this :)
    for example;
    Code:java
    1. ItemStack slot[] = new ItemStack[8];
    2. slot[0] = new ItemStack(Material.SUGAR); //cocaine
    3. slot[1] = new ItemStack(Material.QUARTZ); //meth
    4. slot[2] = new ItemStack(Material.GLOWSTONE_DUST); //meridiem
    5. slot[3] = new ItemStack(Material.INK_SACK, 2); //marijuana
    6. slot[4] = new ItemStack(Material.INK_SACK, 3); //caffeine
    7. slot[5] = new ItemStack(Material.INK_SACK, 9); //ecstasy
    8. slot[6] = new ItemStack(Material.INK_SACK, 5); //cyanide
    9. slot[7] = new ItemStack(Material.INK_SACK, 14); //acid
    10.  
    11. ItemMeta meta[] = new ItemMeta[8];
    12. for(int i = 0; i>=7; i++){
    13. meta = slot.getItemMeta();
    14. }



    It makes it easier, since with arrays, you can use .setContent(); method, which will set all the slots at once based on an array!
     
    Ultimate_n00b likes this.
  5. Offline

    Cryteria

    Just a quick question :
    Can you type this code in a private void of a command? I really need this inventory thing for it.
     
  6. Offline

    chasechocolate

    Cryteria yeah, you can have the inventory-opening part. If you want to have a selection GUI, then I would use IconMenu (search it up).
     
Thread Status:
Not open for further replies.

Share This Page