Jump to content

[1.20.1] Is there a way to detect a left click with an item in all cases?


Recommended Posts

I created an item with different modes. To cycle through those modes i wanted to use shift left and right clicks with the item in hand. But i cant seem to find a way to hook myself into left clicks.


Right clicks are easy:

For right clicks on a Block i can use:

 public InteractionResult useOn(UseOnContext pContext)

For right clicks in the air i can use:

public InteractionResult useOn(UseOnContext pContext)


For left clicks on block it also is kinda similar. I can just override this:

public boolean canAttackBlock(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer)

But what about left clicks in the air?


Obviously i can just say that you can only cycle through the modes when you look at a block, but that feels iffy to me.

Link to comment
Share on other sites

How about Subscribe to the MouseButton event? That's what I'm using.


public class ClientEventSubscriber {
     * Subscribe to Forge-defined events
     * <p>
     * These events will be derived from Event
    @EventBusSubscriber(modid = MOD_ID, bus = EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
    public static class ForgeBus {
        public static void onMouseClickPreEvent(InputEvent.MouseButton.Pre event) {
          final boolean isLeftClick = event.getButton() == GLFW.GLFW_MOUSE_BUTTON_LEFT;


Alternatively you could do on ClientTick `Minecraft.getInstance().mouseHandler.isLeftPressed()`

Edited by dee12452
Add boolean check
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.