Why does itemMeta.setLore() not set the lore of an item?

Discussion in 'Plugin Development' started by Blackilykat, Aug 11, 2022.

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

    Blackilykat

    I'm trying to edit this plugin so that each time a function is called it adds a few strings to the lore, though it isn't getting modified. Here's the important part of the class:
    Code:
        private Inventory createPowerInventory(GUITitle guiTitle, Inventory inventory, Origin origin, Power power) {
            String powerName = power.getName();
            String[] powerDescription = power.getDescription();
            ItemStack originItem = inventory.getItem(originItemSlot);
            assert originItem != null;
            ArrayList<String> originItemLore = (ArrayList<String>) Objects.requireNonNull(originItem.getItemMeta()).getLore();
            int n = 0;
            while(n<originItemLore.size()){
                //Bukkit.getConsoleSender().sendMessage(originItemLore.get(n));
                n++;
            }
            if (powerName != null && !powerName.isEmpty() && powerDescription != null && powerDescription.length != 0) {
                originItemLore.add("-"+powerName+"-");
                int j = 0;
                while(j< powerDescription.length){
                    originItemLore.add(powerDescription[j]);
                    j++;
                }
                Bukkit.getConsoleSender().sendMessage(originItemLore.toString());
                originItem.getItemMeta().setLore(originItemLore.stream().toList());
                inventory.getItem(originItemSlot).getItemMeta().setLore(originItemLore.stream().toList());
                ItemStack itemStack = new ItemStack(Material.FILLED_MAP);
                ItemMeta itemMeta = itemStack.getItemMeta();
    
    I know that the issue isn't in originItemLore since it returns fine in the console: originItemLoreConsole.png
    What i expect to happen is that the different strings add to each other and then that's the item lore. I'm pretty sure the only issue is the fact that the lore does not set, so how do I make it do that?
     

    Attached Files:

  2. Offline

    KarimAKL

    @Blackilykat #getItemMeta() returns a copy of the metadata, so you have to store, modify, then set it.
    Code:Java
    1. ItemMeta meta = itemStack.getItemMeta();
    2. meta.setLore(/* ... */);
    3. itemStack.setItemMeta(meta);

    It should look like that.
     
    Blackilykat likes this.
Thread Status:
Not open for further replies.

Share This Page