#How do I run a command when a specific item with NBT data is used on a block?

16 messages · Page 1 of 1 (latest)

dense silo
#

I'm trying to run a command whenever I use a specific gate pearl from Gateways to Eternity. The item works by having you right click the ground with it, so I don't think ItemEvents.rightClicked is working. In any case, here's my code at the moment.

ItemEvents.rightClicked('gateways:gate_pearl', event => {
    const nbt = {gateway:"gateways:test_gateway"}

    if (event.item.nbt==nbt){
        event.server.runCommand('say Hello!');
    }
    
}
)
vernal solsticeBOT
#

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

glossy ibex
#

== will do a reference comparison, so it will be only true if both operands are referring to the exact same object. This is NOT a structural comparison.

dense silo
#

What exactly do you mean? Like, == isn't the right operator to use for comparing nbt tags?

glossy ibex
#

Yes

#
ItemEvents.rightClicked('gateways:gate_pearl', event => {
  const nbt = event.item.nbt
  
  if (!(nbt && nbt['gateway'] == 'gateways:test_gateway')) return
  event.server.tell('Hello!')
})
#

You can write it as nbt.gateway or nbt['gateway'], doesn't matter

dense silo
#

Trying to understand this if statement. I'm getting confused by the ! cause that means if the condition is false, right? Is the idea that if it's false, then return skips the rest of the code?

glossy ibex
#

That way, we avoid nesting blocks

dense silo
#

Okay, so that works, but there's still the issue of the gateway needing to be right clicked on a block similar to a spawn egg. This code only seems to run when I right click the air, which if I read the docs right, is what it's supposed to do.
But how do I make it happen when right clicking a block with the item?

#

I can't seem to find any events for using an item on a block.

glossy ibex
#
BlockEvents.rightClicked(block, event => {
  // ...
})
#

Within it, event.item returns the item with which the block was right-clicked

glossy ibex
#

Also, is the "ground" any block? If so, an unfiltered version should be used

#
BlockEvents.rightClicked(event => {
  const { item } = event
  const nbt = item.nbt
  if (item.id != 'gateways:gate_pearl') return;
  if (!(nbt && nbt['gateway'] == 'gateways:test_gateway')) return
  event.server.tell('Hello!')
})