How to detect right-click on air?

I’ve tried InteractBlockEntity.Secondary.Mainhand, but this only works when the player is facing a block in reach. I.e. right clicking into the air doesn’t trigger the event.

I know this possible within Minecraft. E.g. Hypixel lobby navigation by right clicking the compass. WorldEdit in Sponge also has this (/farwand gives you a wand that you right click to select far distances out of reach)

How would I go about doing this? Specifically, I want something to occur when a sword is right clicked.

The things you describe relate to a different kind of event. Let me try to explain.

InteractBlockEvent is described as follows:

 * Base event for all interactions involving a {@link BlockSnapshot} at a
 * {@link Location}.

This means that it ONLY gets fired when a block at a location gets clicked. This would not get fired in the things you described above.

What you are after is probably the InteractItemEvent which is described as follows:

Base event for all interactions with an {@link ItemStack} in hand.

Means it would fired whenever you click while holding an item in your hand. This would be likely what you are after. In there you can also get the ItemStack and check if its a sword and then do your codelogic.

Generally, when I try and figure out what Events to use, I look at their definitions, cause they are often decently commented and give a good idea on when they get fired and when not.

Hope this helps!

1 Like