#block.left_click event executing twice

64 messages · Page 1 of 1 (latest)

light furnace
#

Hello, the following code seems to be executing twice and I'm not sure why and how to change that to a single time. Instead of popping one pebble, the left-click interaction pops 2 at a time.

function handleLeftClick(event, expectedBlock, toolId) {
    if (event.getBlock() !== expectedBlock || event.item.id !== toolId) return;
    event.block.popItem('expcaves:stone_pebble');
    event.player.damageHeldItem(event.hand, 1);
}
onEvent('block.left_click', event => {
    handleLeftClick(event, "rankine:dark_gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "minecraft:gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "rankine:dark_gravel", "kubejs:steel_rake")
    handleLeftClick(event, "minecraft:gravel", "kubejs:steel_rake")
    handleLeftClick(event, "rankine:light_gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "rankine:light_gravel", "kubejs:steel_rake")
});
cosmic thunderBOT
#

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

light furnace
#

...also it seems to not damage the tool

#

I adapted this code from a block.right_click event script so probably the events are not as similar as I thought in terms of methods?

elfin mural
#

it happens on client & server.

#

though idk how you got the server only stuff to happen twice...

light furnace
#

O.o

#

so I didn't have this issue with the right-click code

elfin mural
#

oh, hm

light furnace
#

hang on

#

you can left-click with both hands, can you not?

#

only right-click is unique to the main-hand

#

wait no..

elfin mural
#

oh it might also be that yes

light furnace
#

y'know I still don't understand why there's no methods list for each event in the wiki

#

like probejs dumps a file that doesn't generate all events, you first have to use them; in this case it refuses to generate the methods for block.left_click

#

and the file is already too big, even while not having what I need

#

it has quarter of a million columns and it crashes VSC

#

so it's unusable

#

can we not just get a brief list of methods in the wiki's event list?

#

like, the events dump listed the event itself, but the globals file doesn't contain it

still crypt
still crypt
#

And yes, the event gets triggered for each hand

light furnace
#

any way to limit triggering to main-hand?

still crypt
#

the hand is private ... idk why lol.

light furnace
#

..so wait, no matter what, any and all block.left_click interactions trigger twice?

still crypt
#
if(event.player.mainHandItem.equals(event.item)) {}

or

if(event.player.mainHandItem == event.item) {}

could do it

#

would go with first one

light furnace
#
onEvent('block.left_click', event => {
    if (event.player.mainHandItem.equals(event.item)) {
        if (event.getBlock() !== "minecraft:gravel" || event.item.id !== "kubejs:steel_rake") return;
        event.block.popItem('expcaves:stone_pebble');
        event.player.damageHeldItem(event.hand, 1);
    }
}
#

something like this?

#

nah it still pops 2.

still crypt
#

can you console log the item

bold flicker
#

to give a quick update on some support on another server, right click has been figured out (it was main hand off hand running for both) but it seems 1.18 doesnt have a method to distinguish which hand the left click event is running on. But yeah printing the item only returns the main hand item twice

#

my current cursed af solution i was about to suggest is giving the player a stage on the first event trigger, then removing it on the next kekw

light furnace
#
function handleLeftClick(event, expectedBlock, toolId) {
    if (event.getBlock() !== expectedBlock || event.item.id !== toolId) return;
    event.block.popItem('expcaves:stone_pebble');
    event.server.tell(event.getItem());
    //event.player.damageHeldItem(event.hand, 1);
}
onEvent('block.left_click', event => {
    handleLeftClick(event, "rankine:dark_gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "minecraft:gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "rankine:dark_gravel", "kubejs:steel_rake")
    handleLeftClick(event, "minecraft:gravel", "kubejs:steel_rake")
    handleLeftClick(event, "rankine:light_gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "rankine:light_gravel", "kubejs:steel_rake")
});

so this is the code that I have currently; commented out the damageHeldItem line as it doesn't work in this event (it worked for the right_click one) so perhaps you could assist with the correct method for the left_click

also included the log output in the codeblock.

#

that being this

#

ignore the existing damage values on the item, those were from testing the right-click

still crypt
#

But this code does not have the mainhand check...

light furnace
#

that didn't work

#

let me include it again but it didn't work

still crypt
#

have you tried to log the items inside the if etc?

bold flicker
#

wouldnt the item be in main hand for both event triggers? still triggering it twice?

still crypt
#

When the event is fired with the offhand, the item should be difference than the mainhand

#

The main event would be triggered twice yes

bold flicker
#

almost leads me to believe its not firing for off hand main hand and something else is going on

light furnace
#
function handleLeftClick(event, expectedBlock, toolId) {
    if(event.player.mainHandItem.equals(event.item)) {
        if (event.getBlock() !== expectedBlock || event.item.id !== toolId) return;
        event.block.popItem('expcaves:stone_pebble');
        event.server.tell(event.getItem());
        //event.player.damageHeldItem(event.hand, 1);
}}
onEvent('block.left_click', event => {
    handleLeftClick(event, "rankine:dark_gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "minecraft:gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "rankine:dark_gravel", "kubejs:steel_rake")
    handleLeftClick(event, "minecraft:gravel", "kubejs:steel_rake")
    handleLeftClick(event, "rankine:light_gravel", "kubejs:wooden_rake")
    handleLeftClick(event, "rankine:light_gravel", "kubejs:steel_rake")
});
#

this has same behavior, same log output, no errors in log

bold flicker
#

BigEyes is it triggering once for pressing down left click and once for lifting up right click?

light furnace
#

what

#

oh

#

lmfao

bold flicker
#

was able to finally reproduce in 1.20

still crypt
#

interesting lol

bold flicker
#

rip they actually spotted it in 1.19 back in august but nothing has been done: <#1145432143347060756 message>. Time for some hacky workarounds lmao

#
BlockEvents.leftClicked(event => {
    if(event.block !== "minecraft:sandstone" || event.item !== "minecraft:diamond_sword") return
    if(event.player.stages.has("cooling_down")) return

    event.player.stages.add("cooling_down")
    /* do stuff */

    event.server.scheduleInTicks(20, callback => {
        event.player.stages.remove("cooling_down")
    })
})

heres some 1.20 code you can adapt, idk 1.18 syntax. Essentially what im doing is cancelling the event if they're calling this event again within 20 ticks of a successful left click. Should fix your problem of damaging items spamming the left click event too

light furnace
#

this worked, thank you;
last issue would be getting the item's durability down

#

damageHeldItem worked for right_click, doesn't work for left_click, clearly there's a different method for it or once again a workaround is necessary

light furnace
#

this was fixed

#

it occured to me that instead of event.player.damageHeldItem(event.hand, 1) I could do event.player.damageHeldItem(MAIN_HAND, 1)

#

and that works.

#

thanks for the help everyone