#NetworkEvents.dataReceived firing multiple times even with check?

25 messages · Page 1 of 1 (latest)

spring scroll
#

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)

jade sequoiaBOT
#

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

spring scroll
#

as a note: i would like the cooldowns to be server-side so that it can't be abused

#

i have no clue what is going on anymore, i've had this issue for the past 2 hours when i changed it from being right click to keybind

#

i wonder

#

i'm gonna try using the EventJS addon and listen for tick event end phase

#

i still want to fix this server side if possible

spring scroll
#

wtf am i doing wrong

spring scroll
#

bump

#

it fires in a loop until I reload scripts

unkempt ether
#

Where all have you added logging on the server side to make sure it's early returning?

Also, I could've sworn in older versions there were two ways to schedule future events, and one of them was self-resetting

spring scroll
#

i cleaned up my code a bit to make it more readable

function isThrowingKnife(item) {
    return item.id == 'tetra:modular_sword' && item.nbt != null && item.nbt.get('sword/blade') == 'sword/throwing_knife'
}

NetworkEvents.dataReceived('arcanica:accessory_ability', event => {
    const { player, player: { mainHandItem: item } } = event
    if (item.item instanceof $ModularBladedItem && isThrowingKnife(item)) {
        if (player.persistentData.equippedKnifeSatchel && !player.cooldowns.isOnCooldown(item.id)) {
            let knives = player.inventory.allItems.filter(i => isThrowingKnife(i))
            if (knives.length < 2) return
            player.addItemCooldown(item.id, 20)
            for (let i = 0; i < knives.length; i++) {
                let knife = knives[i]
                Utils.server.scheduleInTicks(3 * (i + 1), () => {
                    knife.item.throwItem(player, knife, 0, 5)
                    player.swing()
                })
            }
        }
    }
})
#

still does the same thing ofc

#

the first logger i tried adding right after where i was checking if it's on cooldown, logged once when the key is pressed once, and then again every time the cooldown ended and (unintentionally) ran the code again

spring scroll
#

so i put a logger at the very start of the event

NetworkEvents.dataReceived('arcanica:accessory_ability', event => {
    const { player, player: { mainHandItem: item } } = event
    player.tell('test')
    if (item.item instanceof $ModularBladedItem && isThrowingKnife(item)) {
        if (player.persistentData.equippedKnifeSatchel && !player.cooldowns.isOnCooldown(item.id)) {
            let knives = player.inventory.allItems.filter(i => isThrowingKnife(i))
            if (knives.length < 2) return
            player.addItemCooldown(item.id, 20)
            for (let i = 0; i < knives.length; i++) {
                let knife = knives[i]
                Utils.server.scheduleInTicks(3 * (i + 1), () => {
                    knife.item.throwItem(player, knife, 0, 5)
                    player.swing()
                })
            }
        }
    }
})

i press the key once and it fires multiple times even when the key was released

spring scroll
#

just having this:

NetworkEvents.dataReceived('arcanica:accessory_ability', event => {
    event.player.tell('test')
})

fires in a loop

#

it's so odd

spring scroll
#

i fixed it on the client but how do i confirm if a player changed the client code on the server side

unkempt ether
#

What was the fix on the client side?

spring scroll
#

(i'm using an addon for the nativeevents stuff) previously i had this:

NativeEvents.onEvent('net.minecraftforge.event.TickEvent$ClientTickEvent', event => {
    if (event.phase == 'END') {
        if (global.KEY_ACCESSORY_ABILITY.consumeClick()) {
            Client.player.sendData('arcanica:accessory_ability', {})
        }
    }
})
// checking for end phase to make absolute sure

but then i changed consumeClick to isDown and it somehow worked

#

the problem is that the server code loops if i use consumeClick on the client

#

if somebody changes that code then they could easily bypass this on servers

unkempt ether
#

Shot in the dark, do network events require a confirmation response?

Because presumably, everything you're doing you want to do on the server, so that the client can't be messed with

And the client should only be the invocation