#BlockEvents.leftClicked seems to double register

74 messages · Page 1 of 1 (latest)

river kelp
#

The following code logs twice when a block is left-clicked once. First time with the correct facing, second time always showing down.

BlockEvents.leftClicked(event => {
  console.log(event);
  console.log(event.item);
  console.log(event.getFacing())
})
[17:42:42] [INIT] KubeJS 2101.7.1-build.181; MC 2101 NeoForge
[17:42:42] [INIT] Loaded plugins:
[17:42:42] [INIT] - dev.latvian.mods.kubejs.BuiltinKubeJSPlugin
[17:42:42] [INIT] - dev.latvian.mods.kubejs.client.BuiltinKubeJSClientPlugin
[17:42:42] [INIT] - dev.latvian.mods.kubejs.integration.architectury.ArchitecturyIntegration
[17:42:42] [INIT] - com.almostreliable.morejs.Plugin
[17:42:42] [INFO] Loaded script server_scripts:main.js in 0.008 s
[17:42:42] [INFO] Loaded 1/1 KubeJS server scripts in 0.017 s with 0 errors and 0 warnings
[17:42:45] [INFO] Server resource reload complete!
[17:42:53] [INFO] dev.latvian.mods.kubejs.block.BlockLeftClickedKubeEvent@789ea0df [dev.latvian.mods.kubejs.block.BlockLeftClickedKubeEvent]
[17:42:53] [INFO] 1 minecraft:wooden_pickaxe [net.minecraft.world.item.ItemStack]
[17:42:53] [INFO] north [net.minecraft.core.Direction]
[17:42:53] [INFO] dev.latvian.mods.kubejs.block.BlockLeftClickedKubeEvent@36db04e8 [dev.latvian.mods.kubejs.block.BlockLeftClickedKubeEvent]
[17:42:53] [INFO] 1 minecraft:wooden_pickaxe [net.minecraft.world.item.ItemStack]
[17:42:53] [INFO] down [net.minecraft.core.Direction]
clear bronzeBOT
#

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

craggy quartz
#

once for each arm

river kelp
#

🤯 that explains it

#

tyvm

river kelp
#

I have trawled through the discord and looked at the code to the extent that I can understand it, and I'm struggling to find out how to check for what hand the event is running for. From what I can read, only rightClick events propagate the hand attribute, and this double trigger seems to be more related to mouse down / up events?

#

If there is a simple way to do this (without setting state on the player), I would appreciate a pointer 🙏

craggy quartz
#

if you want to check the main hand (right hand) you can add this on the first line of the event

if(e.hand != 'MAIN_HAND') return
river kelp
#

e.hand returns undefined

#

let me try again 🤞

craggy quartz
#

did you replace e with event for your script

river kelp
#

ahh, yes

#

So, the following code logs undefined and shows no message to the player

BlockEvents.leftClicked(event => {
  console.log(event.hand)
  if (event.hand == 'MAIN_HAND') {
    event.player.tell("Main hand")
  }
})
craggy quartz
#

show me what is undefined

robust bluffBOT
#

Please send your KubeJS server log. It can be found at /minecraft/logs/kubejs/server.log.
If you are on 1.18 or 1.16 it will be called server.txt.
Please send the file directly, without links or snippets.

craggy quartz
#

ah theres no hand

#

try

event.entity.hand
#

or event.item if theres a specific item you want to check

river kelp
#

🤔 neither of those seem to have anything. event.entity.hand is undefined and event.item does not seem to have any useful info

boreal pagoda
#

event.player.mainHeldItem

craggy quartz
#

theres no player tho hmmm

#

entity, block, item, and facing

#

wait hmmm

#

oh nvm

#

inherited from playerevent

#

but it returns entity just the same

#

xD

#
    public Player getPlayer() {
        return getEntity();
    }
river kelp
#

it inherits from KubePlayerEvent which seem to implement getHand

boreal pagoda
#

it should be mainHeldItem though

river kelp
#

mainHeldItem gave me undefined

boreal pagoda
#

oops, mainHandItem

#

from entity or player should work

river kelp
#

It does tell me the main hand item, but it gives the same result for both events

#

so, one click, two events, with the same mainHandItem

boreal pagoda
#

that i’m uncertain why it’s executing twice. sorry

craggy quartz
#

can you show me your current code

river kelp
#

From reading the discord i suspect it's mouse up / down

boreal pagoda
#

haven’t experienced this in 1.20 and below. bug in 1.21?

craggy quartz
#

and see if thats the case

river kelp
#

indeed seems so

#

triggers on click and again on release

boreal pagoda
#

does event have button/mouse state at all? maybe it does invoke twice on down and up and i never realized 😅

river kelp
#

I noticed a thread mentioning it, but I lost track of where it was

#

but it was when reflecting on the native forge left click item interaction event

#

something about "action"

#

can't for the life of me find it again

craggy quartz
#

action...?

boreal pagoda
#

time to pull out the ole console.log(Object.keys(event)) 😅

river kelp
#

thanks, I was looking for that one, but wasn't sure if it worked on Rhino

#

(should have just tried)

#
getRegistries
cancel
getClass
server
getPlayer
wait
notifyAll
getItem
facing
registries
notify
hashCode
block
class
player
getServer
getBlock
item
level
getEntity
getLevel
exit
success
equals
getFacing
toString
entity                 
#

nothing that jumps out at me

boreal pagoda
#

may be on player hopefully. look for something related to mouse, button, key, isDown, isUp, isPressed, etc.

#

or might have to use player persistentData (which was removed in 1.21?!)

river kelp
#

Saw something the other day about persistent data actually still working

boreal pagoda
#

yea there’s a workaround re-implementation

river kelp
#

Thanks for the help though ... I was sure it was supposed to be simple, but glad I'm not just missing something obvious

river kelp
#

There indeed seems to be an action state on left click events. The following code prints the different states

const $Action = Java.loadClass("net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action")

NativeEvents.onEvent("net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock", event => {
  console.log(event.action);
  if (event.action == $Action.START) {
    console.log("On START action (mouse down)")
  }
  console.log("On every action")
})
#

Logs the following

#
[21:46:42] [INIT] KubeJS 2101.7.2-build.201; MC 2101 NeoForge
[21:46:42] [INIT] Loaded plugins:
[21:46:42] [INIT] - dev.latvian.mods.kubejs.BuiltinKubeJSPlugin
[21:46:42] [INIT] - dev.latvian.mods.kubejs.client.BuiltinKubeJSClientPlugin
[21:46:42] [INIT] - dev.latvian.mods.kubejs.integration.architectury.ArchitecturyIntegration
[21:46:42] [INIT] - com.almostreliable.morejs.Plugin
[21:46:42] [INFO] startup_scripts:main.js#1: Loaded Java class 'net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action'
[21:46:42] [INFO] startup_scripts:main.js#3: Loaded Java class 'net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock'
[21:46:42] [INFO] Loaded script startup_scripts:main.js in 0.0 s
[21:46:42] [INFO] Loaded 1/1 KubeJS startup scripts in 0.004 s with 0 errors and 0 warnings
[21:46:46] [INFO] START [net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action]
[21:46:46] [INFO] On START action (mouse down)
[21:46:46] [INFO] On every action
[21:46:46] [INFO] CLIENT_HOLD [net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action]
[21:46:46] [INFO] On every action
[21:46:46] [INFO] startup_scripts:main.js#4: START [net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action]
[21:46:46] [INFO] startup_scripts:main.js#6: On START action (mouse down)
[21:46:46] [INFO] startup_scripts:main.js#8: On every action
[21:46:46] [INFO] CLIENT_HOLD [net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action]
[21:46:46] [INFO] On every action
[21:46:46] [INFO] startup_scripts:main.js#4: ABORT [net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action]
[21:46:46] [INFO] startup_scripts:main.js#8: On every action
#

(it logs it twice, but I think that's an side effect of the startup_scripts rather than the event handler)

river kelp
#

correction, the above code logs twice, once on the client and once on the server, which can be checked with event.side. So the following code works:

#
const $Action = Java.loadClass("net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock$Action")

NativeEvents.onEvent("net.neoforged.neoforge.event.entity.player.PlayerInteractEvent$LeftClickBlock", event => {
  if (event.side == 'SERVER') {
    console.log(event.action);
    if (event.action == $Action.START) {
      console.log("On START action (mouse down)")
    }
    console.log("On every action")

  }
})