wernon Posted July 28, 2020 Posted July 28, 2020 Hello, guys! I'm trying to do some simple mod, which will be an addition to my vanilla spigot server, so this mod should be client-side only. The feature I've tried to implement is executing commands on keypress. I came up with this solution: @SubscribeEvent public void onKeyPress(InputEvent.KeyInputEvent event) { KeyBinding te1 = KEYBIND; //was ofk initialised before int t1 = te1.getKey().getKeyCode(); if(event.getKey() == t1 && event.getAction() == 1){ Minecraft.getInstance().player.sendChatMessage("/command"); } This works fine, but it also toggles when player is in menu or in chat. How can I make behavior of these buttons similar to vanilla WASD? Quote
wernon Posted July 28, 2020 Author Posted July 28, 2020 4 hours ago, diesieben07 said: KeyInputEvent is for raw keyboard input, not key bindings. To check for key bindings use KeyBinding#isPressed in ClientTickEvent (check TickEvent#phase, it fires twice every tick). To set when your key binding is active, set it's IKeyConflictContext (provided via the constructor). Forge provides some default implementations with the KeyConflictContext enum, from your description it sounds like you need KeyConflictContext.IN_GAME. Huge thanks! That solves my problem, but in my case i dont want command to keep spamming while button is pressed. If I use InputEvent.KeyInputEvent, I can use #getAction, but I found no way to do this on ClientTickEvent (I looked through KeyBinding source code but found no method that can help me). Docs says that isPressed should work only once, but it is not working that way. @SubscribeEvent public void onKeyPress(TickEvent.ClientTickEvent event) { if(TEST.isPressed()){ Minecraft.getInstance().player.sendChatMessage("looool"); } } I initialise TEST as public static final KeyBinding TEST = new KeyBinding("key.test", KeyConflictContext.IN_GAME, InputMappings.Type.KEYSYM, GLFW.GLFW_KEY_Z, "test"); I feel like I am doing something stupid and sorry if that really is so, but I tried as much, as I could and this still does not work. Maybe should I write custom KeyConflictContest? Quote
poopoodice Posted July 28, 2020 Posted July 28, 2020 Mmmm I'm not sure but do you want to give it a try: call unpressKey() after you send the msg? 1 Quote
wernon Posted July 29, 2020 Author Posted July 29, 2020 8 hours ago, poopoodice said: Mmmm I'm not sure but do you want to give it a try: call unpressKey() after you send the msg? This method is private, so I cant call it w/o class rewrite. I tried #setPressed(false) and executing my event only once per tick, not twice, but there were no results. Quote
wernon Posted July 29, 2020 Author Posted July 29, 2020 I ended but with extremly bad solution, but it's better, that something, that does not work. @SubscribeEvent public void onKeyPress(InputEvent.KeyInputEvent event) { KeyBinding te1 = TEST; int t1 = te1.getKey().getKeyCode(); if(event.getKey() == t1 && event.getAction() == 1){ if(TEST.isPressed()){ Minecraft.getInstance().player.sendChatMessage("lol"); } } I still trying to figure out how to make thing with TickEvent work and if I manage to find better solution, I'll post code here. Quote
poopoodice Posted July 29, 2020 Posted July 29, 2020 I just have a quick look at the vanilla key binding processing (L1510 in Minecraft), it uses while loop with a condition of KeyBinding.isPressed() in runTick() which you should probably follow how Minecraft does it. Quote
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.