#setLore() doesn't seem to do anything

1 messages · Page 1 of 1 (latest)

stuck drum
#
                  const players = world.getPlayers()
                  for (let player = 0; player < players.length; player++) {
                      const tags = players[player].getTags()
                       for (let tag = 0; tag < tags.length; tag++) {
                           if (tags[tag].includes("enchant_") == true) {
                             players[player].removeTag(tags[tag])
                             const enchant = tags[tag].replace("enchant_", "" );
                             let item =players[player].getComponent(EntityInventoryComponent.componentId);
                             let lore = item.container.getItem(players[player].selectedSlotIndex).getLore();
                             lore.push(`${enchant}`)
                             players[player].sendMessage(`${JSON.stringify(lore)}`) //this is for debug
                             players[player].getComponent(EntityInventoryComponent.componentId).container.getItem(players[player].selectedSlotIndex).setLore(enchant)
                            }
                       }
                }
}); ```


This code is supposed to allow me to add lore elements to items with tags, but despite calling setLore(), the lore on my item remains the same.
Anyone know why?
I'm very new to scripting so it's probably obvious.
Thanks!
#

And as a note my import handling from @minecraft/sever is already stated at the top of the script with the required data

upper mica
#

Maybe you need to do a setItem with the new lore

stuck drum
#

I am using setLore()
I'm using getLore() to get the current item lore, then appending the new lore item, then setting the item lore it it

stuck drum
echo nova
#

Yeah, once you set the lore back to the itemstack, you have to set it back to the inventory. ItemStack is an abstract item that does not represent the physical item in the inventory slot.

#

Also, using constants/variables to define stuff beforehand will have your code much easier to use and read

#

const plrContainer = players[player].getComponent(EntityInventoryComponent.componentId).container

stuck drum
echo nova
#

👍

stuck drum
# echo nova Yeah, once you set the lore back to the itemstack, you have to set it back to th...

I tried this:

                             lore.push(`${enchant}`)
                             let itemStk = item.container.getItem(players[player].selectedSlotIndex).setLore(["test", "test1"] ) //`${lore}`)
                             players[player].sendMessage(`${JSON.stringify(lore)}, ${JSON.stringify(itemStk)}`) //this is for debug
                             players[player].getComponent(EntityInventoryComponent.componentId).container.setItem(players[player].selectedSlotIndex, itemStk)```

itemStk == undefined for some reason
upper mica
#

That's because you're doing a console.warn of setLore which is not a function

#

So you don't have any data, it's setter no read

stuck drum
stuck drum
upper mica
#

Sorry, I mean you can't set setLore, you have to set the itemStack directly

stuck drum
upper mica
#
// Get inventory component
const inv = player.getComponent('inventory').container;
// Get item
const item = inv.getItem(player.selectedSlotIndex);
// Get lore and push new object
const lore = item.getLore().push(`${idk}`);
// Set lore to the ItemStack with the new data
item.setLore(lore);
// Set the ItemStack with the new data in the inventory 
inv.setItem(player.selectedSlotIndex, item)```
echo nova
#

^
YOu were setting the itemStk variable as the setLore function return value- which returns no value.

stuck drum
echo nova
#

Not sarcastic, dw 😛

stuck drum
echo nova
#

Yeah, it does. You want to make the variable set to just item.getLore().

#

Then modify it from there