Set Item lore and name Need Help!

Discussion in 'Plugin Development' started by inventorman101, Jun 1, 2013.

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

    inventorman101

    I am having trouble with this bit of code I keep getting a NullPointerExcpetion when I call the getItemMeta() method here is my code.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            Player p = (Player) sender;
           
            boolean returnValue = false;
           
            if (commandLabel.equalsIgnoreCase("geweapon"))
            {
                ItemStack pistol = new ItemStack(Material.BOW, 1);
                setName(pistol,"Pistol", "1 Damage", "Cooldown: none", "A simple handgun");
                p.getInventory().addItem(pistol);
                returnValue = true;
            }
            return returnValue;
        }
     
        @Override
        public ItemStack setName(ItemStack is, String name, String damage, String cooldown, String desc)
        {
            ItemMeta a = is.getItemMeta();
            System.out.println("ItemMeta: " + a);
            a.setDisplayName(ChatColor.GOLD + name);
            ArrayList<String> lore = new ArrayList<String>();
            lore.addAll(a.getLore());
            lore.add(ChatColor.RED + damage);
            lore.add(ChatColor.GREEN + cooldown);
            lore.add(ChatColor.GRAY + desc);
            a.setLore(lore);
            is.setItemMeta(a);
            return is;
        }
    }    
     
  2. Offline

    GodzOfMadness

    inventorman101 Check if it has ItemMeta before doing so. The method returns an ItemStack so you need to set "pistol" to the method like pistol = setName(..);
     
  3. Offline

    inventorman101

    Ok thanks I will try this :)

    Ok I revised my code and it give me a bow with none of the lore set or the name. It is probably something stupid I didn't do XD

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            Player p = (Player) sender;
           
            boolean returnValue = false;
           
            if (commandLabel.equalsIgnoreCase("geweapon"))
            {
                ItemStack pistol = new ItemStack(Material.BOW, 1);
                pistol = setName(pistol,"Pistol", "1 Damage", "Cooldown: none", "A simple handgun");
                p.getInventory().addItem(pistol);
                returnValue = true;
            }
            return returnValue;
        }
     
        @Override
        public ItemStack setName(ItemStack is, String name, String damage, String cooldown, String desc)
        {
            if(is.hasItemMeta()==true)
            {
                ItemMeta a = is.getItemMeta();
                System.out.println("ItemMeta: " + a);
                a.setDisplayName(ChatColor.GOLD + name);
                ArrayList<String> lore = new ArrayList<String>();
                lore.addAll(a.getLore());
                lore.add(ChatColor.RED + damage);
                lore.add(ChatColor.GREEN + cooldown);
                lore.add(ChatColor.GRAY + desc);
                a.setLore(lore);
                is.setItemMeta(a);
            }
            return is;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    GodzOfMadness

    inventorman101 You can remove ==true. If you let it go like (is.hasItemMeta()) it will check if it's true and to check if it's false just do (!is.hasItemMeta()). It should work either way though.
     
  5. Offline

    inventorman101

    I change my code to what you said and it still it still give me a plain bow
     
  6. Offline

    GodzOfMadness

    inventorman101 It basically has no ItemMeta. That's why it's not working.
     
  7. Offline

    inventorman101

    So should I choose another item???
     
  8. Offline

    GodzOfMadness

    inventorman101 Try replacing pistol in the addItem parameter with the setName(..)

    EDIT: Also to remove pistol = ..
     
  9. Offline

    inventorman101

    GodzOfMadness Ok I tried that now it doesn't give me anything
     
  10. Offline

    GodzOfMadness

    inventorman101 add player.updateInventory() after adding the item.
     
  11. Offline

    inventorman101

    I did that but it is still a plain bow without any of the lore do i need to clear the name/lore before I set it or anything? GodzOfMadness
     
  12. Offline

    GodzOfMadness

    inventorman101 No, I'm not sure why the ItemMeta is null. Instead of if(is.hasItemMeta()) just check if the itemStack is null so like if(is != null) do stuff
     
  13. Offline

    inventorman101

    GodzOfMadness I get the same thing a NullPointerException. The Stacktrace says at line 62 which is the setName method I am calling with all the parameters.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            Player p = (Player) sender;
           
            boolean returnValue = false;
           
            if (commandLabel.equalsIgnoreCase("geweapon"))
            {
                ItemStack pistol = new ItemStack(Material.BOW, 1);
                setName(pistol,"Pistol", "1 Damage", "Cooldown: none", "A simple handgun");
                p.getInventory().addItem(pistol);
                returnValue = true;
            }
            return returnValue;
        }
     
        @Override
        public ItemStack setName(ItemStack is, String name, String damage, String cooldown, String desc)
        {
            if(is != null)
            {
                ItemMeta a = is.getItemMeta();
                System.out.println("ItemMeta: " + a);
                a.setDisplayName(ChatColor.GOLD + name);
                ArrayList<String> lore = new ArrayList<String>();
                lore.addAll(a.getLore());
                lore.add(ChatColor.RED + damage);
                lore.add(ChatColor.GREEN + cooldown);
                lore.add(ChatColor.GRAY + desc);
                a.setLore(lore);
                is.setItemMeta(a);
            }
            return is;
        }
     
  14. Offline

    GodzOfMadness

    inventorman101 Don't try to print out the itemMeta object. That might be causing the error but what is on line 62?
     
  15. Offline

    inventorman101

    GodzOfMadness I meant line 44 and that is the setName(pistol, "Pistol", "1 Damage" etc...); method I was calling I just need this to work and the rest will be much easier than this How about I send you the jar file ( Open in Notepad++ or what ever and save as a jar)
     

    Attached Files:

  16. Offline

    inventorman101

    GodzOfMadness or anyone else I think it has something to do with the lore and adding it because I comment it out and it works fine any idea why?
     
  17. Offline

    GodzOfMadness

    inventorman101 Try removing the line where you add the previous lore from it.
     
  18. Offline

    inventorman101

    I got it to work so thank you very much :)
     
  19. Offline

    Shzylo

    On the line you are getting, start a new line for each argument like:
    Code:
    setName(
    pistol,
    "Pistol",
    "1 Damage",
    "Cooldown: none",
    "A simple handgun");
    
    it may help.
     
Thread Status:
Not open for further replies.

Share This Page