Solved Issue with ItemMeta

Discussion in 'Plugin Development' started by pairofsnipers, Apr 30, 2015.

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

    pairofsnipers

    Hello, this is a problem I have had several times when dealing with item metas. When I try creating a simple item stack and editing its meta it doesn't work. It lets me get this far and then won't let me do anything with the variable "meta": http://gyazo.com/67dbef38a02e2bee7b87970c85bb43b9

    It simply won't let me do anything with the meta. When I type "meta." nothing pops up as the list of things I can do with it. Does anyone know how to solve this or what is causing this?
     
  2. Offline

    SuperOriginal

    You need to modify meta inside a method.
     
  3. Offline

    pairofsnipers

    Wow... I never knew that. I must have always gotten by without knowing that since I mostly use it inside a onCommand method. Thanks @SuperOriginal :D
     
  4. Offline

    Garnetty

    You can put it inside an initialization block
    Code:
    ItemStack i = new ItemStack(Material.DIAMOND_SWORD, 1);
     
        {
            ItemMeta iMeta = i.getItemMeta();
            iMeta.setDisplayName(ChatColor.RED + "Special Sword");
            i.setItemMeta(iMeta);
        }
    
    And if you want you can make it static by tagging the itemstack as static and also putting static over the initialization block like so:
    Code:
    static ItemStack i = new ItemStack(Material.DIAMOND_SWORD, 1);
      
        static
        {
            ItemMeta iMeta = i.getItemMeta();
            iMeta.setDisplayName(ChatColor.RED + "Special Sword");
            i.setItemMeta(iMeta);
        }
     
  5. Offline

    pairofsnipers

    I did not know of this either thank you both for the tips @Garnetty :D
     
Thread Status:
Not open for further replies.

Share This Page