Function:
function checkPlayerInventory() {
for (const player of world.getPlayers()) {
const inventory = player.getComponent('minecraft:inventory').container;
for (let i = 0; i < inventory.size; i++) {
const item = inventory.getItem(i);
if (item && item.typeId === your_custom_item) {
item.setLore([
'§r§l§0hi this is 1 line',
'wow a second line',
'you can only have 20 lines and 50 characters'
]);
inventory.setItem(i, item);
}
}
}
system.runTimeout(checkPlayerInventory, 10);
}
Function Explanation:
This function periodically updates the lore of specific items in all players' inventories:
Iterate Through Players:
The function loops through every player in the game world to access their inventory.
Access Inventory:
For each player, it retrieves their inventory, allowing us to examine and modify the items.
Check Inventory Slots:
It then checks each slot in the inventory for items.
Identify Target Items:
If an item matches the specified type, its lore is updated. Lore is the descriptive text associated with the item.
Update Item:
The item with updated lore is placed back into its original slot, making the changes visible to the player.
Re-Schedule Function:
The function schedules itself to run again after a short delay to keep inventories updated.
This ensures that all specified items across players' inventories consistently have the updated lore and accommodates any new or changed items over time.