Setting Material Meta Data

Discussion in 'Plugin Development' started by TheCakeMaster, Oct 5, 2014.

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

    TheCakeMaster

    So I've made a chest GUI that has a bunch of items in it, using Material. to specify a material. But the problem is that I can't set a metadata to that item. Eg, If I'm setting an item in the menu to wool, but I want it to be colored wool, how would I do this?
    Here is a bit of my code:
    Code:
    public static Inventory mainGadgetMenu = Bukkit.createInventory(null, 27, ChatColor.GOLD + "Gadget Menu");
    public static Inventory petGadgetMenu = Bukkit.createInventory(null, 54, ChatColor.GOLD + "Pet Menu");
     
    static {
    //mainGadgetMenu
    createDisplay(Material.HAY_BLOCK, mainGadgetMenu, 2, "" + ChatColor.RED + ChatColor.BOLD + "Pets", ChatColor.GOLD + "Choose and customize your pet!");
    createDisplay(Material.NETHER_STAR, mainGadgetMenu, 6, "" + ChatColor.RED + ChatColor.BOLD + "Trails", ChatColor.GOLD + "Choose a particle trail!");
    createDisplay(Material.DIAMOND_HELMET, mainGadgetMenu, 20, "" + ChatColor.RED + ChatColor.BOLD + "Hats", ChatColor.GOLD + "Choose a hat!");
    createDisplay(Material.FIRE, mainGadgetMenu, 24, "" + ChatColor.RED + ChatColor.BOLD + "Coming Soon!", "");
    //Here I want to set an item to be colored wool. (eg, a metadata of 5)
    createDisplay(Material.WOOL, petGadgetMenu, 0, "" + ChatColor.RED + ChatColor.BOLD + "Select Pet", null);
    }
     
    public static void createDisplay(Material mat, Inventory inv, int Slot, String name, String lore) {
    ItemStack item = new ItemStack(mat);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(name);
    meta.setLore(Arrays.asList(lore));
    item.setItemMeta(meta);
    inv.setItem(Slot, item);
    }
    
     
  2. Offline

    Skionz

    ItemStack item = new ItemStack(Matierial.WOOL, 1, 5);
     
  3. Offline

    Gamefreak_0

    TheCakeMaster
    As an answer to your example, you need to take a durability parameter because wool is colored based off it's durability in the ItemStack, NOT the ItemMeta. Eg, If you want red wool the durability of red wool is 14, so use:
    Code:java
    1. ItemStack item = new ItemStack(mat);
    2. item.setDurability(14);

    If you need to know the durability to each wool color you can push F3+h in game (without any inventory open) then mouse over an item and after the name will be the Item ID followed by a slash, then the durability, therefore red wool will say, Red Wool (#0035/14)
     
  4. Offline

    fireblast709

    TheCakeMaster Skionz Gamefreak_0 Or we stay off the magic numbers since we can. Just create a new org.bukkit.material.Wool instance and call ItemStack#setData(woolInstance) ;3
     
    Skionz likes this.
  5. Offline

    TheCakeMaster

    fireblast709
    Sooo, what would the code for that be and where would I put it in my current code?
     
  6. Offline

    Gamefreak_0

    TheCakeMaster
    Well in my opinion, doing it fireblast's way would just be more work that only has one purpose, if he wants to show you how to do it that way that's his/her own choice.
    My way however can be done just by making a second method after your current createDisplay method that is exactly the same but only takes one more parameter the code for it would be this:
    Code:java
    1. public static void createDisplay(Material mat, Inventory inv, int Slot, String name, String lore, short durability) {
    2. ItemStack item = new ItemStack(mat);
    3. ItemMeta meta = item.getItemMeta();
    4. meta.setDisplayName(name);
    5. meta.setLore(Arrays.asList(lore));
    6. item.setItemMeta(meta);
    7. item.setDurability(durability);
    8. inv.setItem(Slot, item);
    9. }

    Then when you want to color wool, use this:
    Code:java
    1. // change the last value (the 1) to the durabilitiy of the wool color you want
    2. createDisplay(Material.WOOL, petGadgetMenu, 0, "" + ChatColor.RED + ChatColor.BOLD + "Select Pet", null, 1);

    My first post told you how to find the durability of wool if you didn't see it.
     
  7. Offline

    TheCakeMaster

    Gamefreak_0
    Tried that, and got an error on the createDisplay(Material, Inventory, Slot...)
    "The method createDisplay(Material, Inventory, int, String, String, short) in the type gadgets is not applicable for the
    arguments (Material, Inventory, int, String, null, int)"
     
  8. Offline

    fireblast709

    Gamefreak_0 my code, however, has clarity. In the event that you want to change wool colour, you would have to look up the datavalue, while I can just change the enum constant.
     
  9. Offline

    TheCakeMaster

    Could you give me an example of your way of doing it please?
     
  10. Offline

    fireblast709

    TheCakeMaster It's literally one method call, what part of it needs clarification?
     
  11. Offline

    TheCakeMaster

    fireblast709
    So:

    Code:java
    1.  
    2. Wool wool = new Wool();
    3.  

    I'm not exactly sure what you mean by ItemStack#setData(wool)
     
  12. Offline

    fireblast709

    AdamQpzm likes this.
  13. Offline

    TheCakeMaster

Thread Status:
Not open for further replies.

Share This Page