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!
#setLore() doesn't seem to do anything
1 messages · Page 1 of 1 (latest)
And as a note my import handling from @minecraft/sever is already stated at the top of the script with the required data
Maybe you need to do a setItem with the new lore
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
I believe that'll remove other item data, like enchantments or durability
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
Ik yeah I changed some of it for testing purposes
I wasn't sure if setting the value in something already defined would apply the change to the actual thing
So basically what you said
👍
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
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
There's no console.warn() anywhere in my code
getLore() works, so it's not a undefined stack
Sorry, I mean you can't set setLore, you have to set the itemStack directly
I figured, just dunno how lol
// 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)```
^
YOu were setting the itemStk variable as the setLore function return value- which returns no value.
AH yeah that makes total sense right (that doesn't sound sarcastic does it? It's not supposed to be sarcastic but for some reason it sounds sarcastic to me lol)
Not sarcastic, dw 😛
Correct me if I'm wrong but doesn't .push() return a number?
Cuz then it'd be setting lore to a value not the new lore array