Solved setLore not working for me. Please help

Discussion in 'Spigot Plugin Development' started by FalconSeeker, Jun 11, 2018.

  1. Code (Text):
        public void getUses(ItemStack itemInHand, Player p) {
            List<String> lores = itemInHand.getItemMeta().getLore();
            lores.add(5, "addddd"); //String.valueOf(Integer.valueOf((lores.get(4))) - 1));
            itemInHand.getItemMeta().setLore(lores);
            p.setItemInHand(itemInHand);
        }
    then i do
    Code (Text):
                getUses(p.getItemInHand(), p);
     
    and NOTHING happens
     
  2. [​IMG]

    You are modifying a copy of the ItemMeta. The original is still intact and unchanged on the item.

    You have to set the meta back to the item.

    Code (Text):
        ItemMeta meta = itemInHand.getItemMeta();
        List<String> lores = meta.getLore();
        lores.add(5, "addddd");
        meta.setLore(lores);
        itemInHand.setItemMeta(meta);
        p.setItemInHand(itemInHand);
     
  3. Additional always check your item if it has an itemMeta and lore first before doing anything to prevent a null pointer exception when the item does not hava a lore a common mistake I make
     
  4. i checked it before i executed the method
    if(!(p.getItemInHand().hasItemMeta()){
    return;
    }
     
    #4 FalconSeeker, Jun 11, 2018
    Last edited: Jun 11, 2018
  5. If you are simply adding to it you do not have to null check. Simply getting the meta will create it if its not already there. That's why there is a #hasItemMeta() method. If you do #getItemMeta() == null it will always be false because if there wasnt any there it creates it automatically. ;)
     
    • Agree Agree x 1