GooberGunter Posted February 22, 2018 Share Posted February 22, 2018 (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 February 22, 2018 by GooberGunter Quote Link to comment Share on other sites More sharing options...
jabelar Posted February 22, 2018 Share Posted February 22, 2018 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. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/ Link to comment Share on other sites More sharing options...
Choonster Posted February 22, 2018 Share Posted February 22, 2018 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. Quote 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. Link to comment Share on other sites More sharing options...
jabelar Posted February 22, 2018 Share Posted February 22, 2018 (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 February 22, 2018 by jabelar 1 Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/ Link to comment Share on other sites More sharing options...
GooberGunter Posted February 22, 2018 Author Share Posted February 22, 2018 Thanks guys, works great! I'll keep the other possible methods in mind just in case there are any future incompatibilities. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.