Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

  • Author
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?

 

  • Author
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.

  • Author

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.

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. 

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.