#Adding cooldown to entity spawn

11 messages · Page 1 of 1 (latest)

timid nimbus
#

Hey im using this script to spawn a wandering trader dropping a stone in to the water:

EntityEvents.spawned('minecraft:item', event => {
    const { entity, level, server } = event
    if (entity.item.id != 'minecraft:stone')return 
    const uid = entity.uuid

    server.scheduleInTicks(40, () => {
        const item = level.getEntity(UUID.fromString(uid))
            if(item === null)return
        const x = item.x
        const y = item.y
        const z = item.z

        if(level.getBlock(x, y, z).id == 'minecraft:water'){
            const wanderingTrader = level.createEntity('minecraft:wandering_trader')
            wanderingTrader.setPosition(x, y + 1, z)
            wanderingTrader.spawn()
            item.item.shrink(1)
            console.log(`Wandering Trader spawned at (${x.toFixed(0)}, ${(y + 1).toFixed(0)}, ${z.toFixed(0)}) in water`);
        }
    })
})

is it possible to add cooldown so players do not spam this?

cunning boltBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

stone belfry
#

you want the cooldown for each player, or globally?

timid nimbus
#

globally if possible

#

and if you dont mind, maybe tell me how to add it per player so i do not have to create a new support ticket in the future XD

stone belfry
#
  1. read the persistent data(if precent) and compare with server tick count now
  2. store the server.tickCount in server persistent data
#

there's player persistentData and player.age as well

#

you can get the player from entity.thrower iirc

timid nimbus
#

cool, let me try, thanks ❤️

worthy cedarBOT
#

[➤](#1232006615327314010 message)
Can be used to nuke the loop(for example, hurt → attack →hurt...), or set a cooldown for a script.
Though the performance boost is neglectable, it can help throttling the script.

Function, put it in server scripts, can be in a seperate file.

General throttle:

const throttle = (temp => (tick) => {
    const now = Utils.server.tickCount;
    if (!temp[tick] || now - temp[tick] >= tick) {
        temp[tick] = now;
        return false;
    }
    return true;
})({});

Entity-specific throttle:

const throttle = (temp => (entity, tick) => {
    const now = Utils.server.tickCount;
    const uuid = entity.uuid;
    if(!temp[uuid] || now - temp[uuid] >= tick) {
        temp[uuid] = now;
        return false;
    };
    return true;
})({});

**Example: **

EntityEvents.hurt(["minecraft:snow_golem", "minecraft:iron_golem"], e => {
    if(throttle(30)) return; //30 ticks

    if(!e.source.getImmediate() || !e.source.getImmediate().isPlayer()) return;
    e.entity.attack(e.source.getImmediate(), 10); //Without throttling, this will cause loop
});
stone belfry
#

theres a function you can use