I'm trying to run some code once and then add a cooldown to an item when pressing a keybind with the item in hand. For some reason, even if I add data flags to the player in an attempt to prevent it happening multiple times, it fires more than once, even waiting for the cooldown to finish to run it again.
It's probably such a simple fix and I'm missing something.
Server code:
NetworkEvents.dataReceived('arcanica:accessory_ability', event => {
const { player, player: { mainHandItem: item }} = event
if (player.cooldowns.isOnCooldown(item.item)) return
if (player.persistentData.equippedKnifeSatchel) {
if (item.id != 'tetra:modular_sword' || item.nbt == null || item.nbt.get('sword/blade') != 'sword/throwing_knife') return
if (item.item instanceof Java.loadClass('se.mickelus.tetra.items.modular.impl.ModularBladedItem')) {
let knives = player.inventory.allItems.filter(i => i.id == 'tetra:modular_sword' && i.nbt != null && i.nbt.get('sword/blade') == 'sword/throwing_knife')
if (knives.length < 1) return
for (let i = 0; i < Math.min(knives.length, 3); i++) {
let knife = knives[i]
Utils.server.scheduleInTicks(3 * (i + 1), () => {
knife.item.throwItem(player, knife, 0, 5)
player.swing()
})
}
player.addItemCooldown(item.id, 100)
}
}
})
Client code:
ClientEvents.tick(event => {
if (global.KEY_ACCESSORY_ABILITY.consumeClick()) {
Client.player.sendData('arcanica:accessory_ability', {})
}
})
(keybinds are defined already)