#how to i add cooldown to item?

1 messages · Page 1 of 1 (latest)

vivid spire
#

You did add the cooldown. If you want the item to not be useable during it you need to check for a cooldown in the first function

vivid spire
#

You basically already did the check in the sceond function:

const cooldownComp = itemStack.getComponent('minecraft:cooldown');
if (cooldownComp) {
    const remainingTime = cooldownComp.getCooldownTicksRemaining(player);
        if (remainingTime == 0) {
            // execute whatever you want the item to do here
    }
}
prime agate
vivid spire
#
world.afterEvents.itemUse.subscribe((event) => {
    const item = event.itemStack
    const player = event.source

    if (item.typeId != "cata:floretstone") return

    const cooldownComp = item.getComponent('minecraft:cooldown');
    if (!cooldownComp) return

    const remainingTime = cooldownComp.getCooldownTicksRemaining(player);
    if (remainingTime > 0) return

    player.applyKnockback(player.getViewDirection().x,  player.getViewDirection().z, 5, 1,)
    player.runCommandAsync("particle cata:small_impact ~ ~ ~")
    player.runCommandAsync("playsound random.explode @a[r=10] ~ ~ ~ 10 2.5 10")
})

The first function could look something like this

prime agate
#

like this?

#

import * as server from "@minecraft/server"

const world = server.world

world.afterEvents.itemUse.subscribe((event) => {
    const item = event.itemStack
    const player = event.source

    if (item.typeId != "cata:floretstone") return

    const cooldownComp = item.getComponent('minecraft:cooldown');
    if (!cooldownComp) return

    const remainingTime = cooldownComp.getCooldownTicksRemaining(player);
    if (remainingTime > 0) return

    player.applyKnockback(player.getViewDirection().x, player.getViewDirection().z, 5, 1,)
    player.runCommandAsync("particle cata:small_impact ~ ~ ~")
    player.runCommandAsync("playsound random.explode @a[r=10] ~ ~ ~ 10 2.5 10")
})

import { world, system } from '@minecraft/server';

world.beforeEvents.itemUse.subscribe(({ source: player, itemStack }) => {
    let event = system.run(() => {
        if (itemStack.typeId === 'cata:floretstone') {
            const cooldownComp = itemStack.getComponent('minecraft:cooldown');
            if (cooldownComp) {
                const remainingTime = cooldownComp.getCooldownTicksRemaining(player);
                if (remainingTime == 0) {
                    cooldownComp.startCooldown(player);
                }
            }
        }
    })
})
vivid spire
#

Yeah, that should work

prime agate
vivid spire
#

Isn't that what it should do?

prime agate
#

its suppose to do this but only work when the cooldown is finished