Inventory createItem method help.

Discussion in 'Spigot Plugin Development' started by NaJated, May 9, 2015.

  1. Hey all, I'm trying to make an easy and simple createitem that creates the itemstack and everything like that but it's turning out quite hard because when I put ItemStack testItem in the method parameters and then go to the method block and try to define that item stack it doesn't work. So then I went ahead and tried to do it another way by leaving the itemstack out of the method parameters and just having the itemstack properties e.g. the name and material type but that still doesn't work. I keep getting NPE's on the creatItem in the items()

    http://hastebin.com/aqekezaraw.avrasm
     
  2. Code (Java):

    public ItemStack createItem(Material material, String name) {
       ItemStack stack = new ItemStack(material);
       ItemMeta im = stack.getItemMeta();
                        im.setDisplayName(name);
       stack.setItemMeta(im);
       return stack;
    }
     
     
  3. Thanks for the code? Anyways how would I go about retrieving that itemstack?
     
    #3 NaJated, May 9, 2015
    Last edited: May 9, 2015
  4. If you want to use his Method and have no Idea how to retrieve that ItemStack and use it further on, then have a look at this
     
  5. It already does return the itemstack but when I invoked on the method and set it in an inventory nothing appeared in the inv..
     
  6. The problem is you're passing null fields. Let's use MaTaMoR_'s method for reference but change it, and then use the following code to add to the inventory.

    Code (Text):
    public static ItemStack createItem(Material material, int amount, String name) {
        ItemStack stack = new ItemStack(material, amount);
        ItemMeta im = stack.getItemMeta();
        im.setDisplayName(name);
        stack.setItemMeta(im);
        return stack;
    }
    Code (Text):
    inventory.setItem(1, createItem(Material.DIAMOND, 1, "DIMONDD"));