Jump to content

Recommended Posts

Posted (edited)

Hey! I'm trying to implement a new "mode" for the player, where they press a button and the left and right mouse buttons no longer destroy/place blocks, but do other functions. I have everything set up except for a way to cancel the original destruction/placement events. Is there any way of doing this? 

 

Thanks.

Edited by GooberGunter
Posted

Well, you could probably use the "keybinding" system which actually abstracts the mouse buttons as well. GameSettings.keybindUseItem (which is left button) has keycode -99 by default, keybindAttack (which is right button) has keycode -100 by default. keybindPickBlock (middle button) has keycode -98 by default.

 

I think you could probably reassign the codes temporarily to some code that can never be pressed (not a valid keyboard code) or something similar.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

The sub-events of PlayerInteractEvent are fired when the player interacts with the world in various ways; you should be able to subscribe to the individual sub-events and cancel them when this mode is active.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

Okay, I tried it out. The easiest way is to handle the InputEvent.MouseInputEvent and set the state of the keybind directly with code such as:

    @SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
    public static void onEvent(InputEvent.MouseInputEvent event)
    {
        GameSettings gs = Minecraft.getMinecraft().gameSettings;
        if (gs.keyBindAttack.isPressed()) // add your additional conditions here
        {
            KeyBinding.setKeyBindState(gs.keyBindAttack.getKeyCode(), false);
        }
    }

 

Note that it must be the InputEvent.MouseInputEvent not the MouseEvent because the latter is called before the keybindings are updated during the Minecraft tick.

 

Also, note that due to the weird way that the set state is implemented you also need to call the isPressed() method to ensure that any pressed counter is also cleared out. This is because there is both a pressed and a pressTime field which have to be cleared but the pressTime is private and needs the call to isPressed() to decrement. (Alternatively you could use Java reflection to access it directly).

 

I tested it and it works well. All left-clicks on entities and blocks were prevented, but GUI interaction still worked. However, I guess you might still want to allow clicking on entities so you could check the mouse over function to make sure it was clicking on a block.

 

There are alternative ways to do it. Like Choonster said, you could handle and cancel all the related events. Yet another way would be to create your own KeyBinding subclass that considers your conditions for blocking the keys, and replace the instances in GameSettings with your own version.

Edited by jabelar
  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.

Guest
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.

Announcements



×
×
  • Create New...

Important Information

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