#Random Item Every 30 Seconds

1 messages ยท Page 1 of 1 (latest)

hardy sandal
#
const itemStack = new ItemStack(ItemTypes.getAll().sort(() => Math.random() - 0.5)[0].id, 1);
lethal canopy
#
const itemList = ItemTypes.getAll().map((item)=>item.id);

function giveRandomItem(player) {
    const inv = player.getComponent("inventory")?.container;
    if (!inv) return;
    //much more cheaper in term of performance
    const itemID = itemList[Math.floor(itemList.length * Math.random())];
    const item = new ItemStack(itemID,1);
    inv.addItem(item);
}

system.runInterval(()=>{
    world.getPlayers((player)=>{
        //Call function to give random item
        giveRandomItem(player);
    })
},600) //600 tick = 30 seconds
lethal canopy
hardy sandal
#

the sort thing every time would be uneccessary though, i can agree on that

#

ngl i didnt process the every 30 seconds thing and just saw random item

lethal canopy
hardy sandal
#

once again, the sort and generally how the list was stored i agree

#

but we would have called math.random the same amount

#

you have edited that message like 3 times now ๐Ÿ’€

lethal canopy
#

No, we don't.

your Math.random() is within the sort(), iterating the ItemTypes. Meaning, it's being called proportional to how many items ItemTypes returned.

hardy sandal
#

oh that yeah oops misunderstood what you were saying

lethal canopy
#

import it from "@minecraft/server"