Jump to content

[1.12.2] Disabling Input Temporarily Through a Capability


EtherealOnyx

Recommended Posts

Hello! I'm trying to figure an optimal way of being able to "intercept" key presses. I made a capability that gets attached to the player, and when active, it should prevent player input until it's inactive. I have it written down so there is function called disableMovement() in my Event Handler class that sets whether the player is allowed to move like so:

 

public class MyEventHandler {
    ...
    //This method is called from a LivingUpdateEvent method
    private void doAerial(EntityLivingBase entity, IEntityEffect cap) {
        switch(cap.getPhase()) {
            case 0:
              ...
              cap.changeStatus();
              disableMovement(true);
              break;
            ...
            case 3:
                if (entity.onGround || entity.isInWater()) {
                    cap.reset();
                    disableMovement(false);
                }
                break;
            ...
        }
    }
}

 

The capability works fine and so does reaching the disableMovement() function, Despite the name, however, I want to control all player input depending on whether or not "disableMovement()" is enabled. I've tried to accomplish this several different methods, including the ones listed below:

@SideOnly(Side.CLIENT)
public class MOKeyBinding extends KeyBinding {
    boolean enableInput = true;

    public MOKeyBinding(KeyBinding key) {
        super(key.getKeyDescription(), key.getKeyConflictContext(),
                key.getKeyModifier(), key.getKeyCode(), key.getKeyCategory());
    }

    public void changeInputStatus(boolean disabled) {
        enableInput = !(disabled);
    }


    @Override
    public boolean isPressed() {
        boolean isPressed = false;
        if (enableInput) isPressed = super.isPressed();
        System.out.println(isPressed);
        return isPressed;
    }

}

And call this function in postInit():

public void changeKeybindings() {
	KeyBinding[] keys = Minecraft.getMinecraft().gameSettings.keyBindings;
		for (int i = 0; i < keys.length; i++)
			keys[i] = new MOKeyBinding(keys[i]);
}

Then add this code to my event handler (which gets called by the function doAerial()):

public void disableMovement(boolean disable) {
        for (int i = 0; i < Minecraft.getMinecraft().gameSettings.keyBindings.length; i++)
            ((MOKeyBinding) Minecraft.getMinecraft().gameSettings.keyBindings[i]).changeInputStatus(disable)
}

 

This would allow me to use my custom keyBindings class and also be able to store them in gameSettings. However, when loading up the game, I am unable to press any keys or interact with anything whatsoever. The only button that works is escape and moving the mouse to move the camera. However, when I go to the control options, all of the controls are their listed defaults (WASD for movement, E for inventory, etc). If I change these to other values, pressing the respective key would still not work. I'm assuming this has to do with the private static final variables listed in the KeyBinding class like KEYBIND_ARRAY, KEYBIND_SET, and HASH? It seems that by initializing a new KeyBinding object they are being added to these maps again which I think could cause problems, I'm not sure. Regardless, I was wondering if the final method that I used to try and solve my problem is an optimal choice, and if so, what can I do to fix the problems I've encountered? Thanks!

Edited by EtherealOnyx
Link to comment
Share on other sites

Thanks for your reply! That's actually perfect...scrap everything! It's a lot simpler too, and allows me to see what keybinds are pressed.

 

It may have seem to worked too well, actually, as I did run into two issues...the first one, the game would pause when the GUI is set to open - this was easily fixed by overriding the doesGuiPauseGame() function, however...the second issue still persists. The GUI shows up and disappears perfectly. When it's present, however, the mouse is rendered and you can no longer move the camera. I guess this is normal because that's what is supposed to happen when you open a GUI. Is it possible to keep camera movement and prevent the mouse from displaying though? I was thinking that maybe I need to mimic camera movement by overriding the handleMouseInput() function, but I still don't think that would prevent the mouse from rendering so that's probably not a good solution.

 

The way I get the GUI to render is by making the disableMovement() function client side only, and then just using displayGuiScreen() from the client minecraft instance. (Although I'm probably going to switch the GUI call to be server side in the future to prevent desyncs.)

Link to comment
Share on other sites

Alright, so I added a new event in my EventHandler class:

@SubscribeEvent
public void guiOpening(GuiOpenEvent event) {
	if (settingFocus)
		event.setCanceled(true);
}

settingFocus is a field in the same class, although static. Thus, I'm able to set it in the GuiScreen class:

public class GuiControlled extends GuiScreen {

    @Override
    public boolean doesGuiPauseGame() {
        return false;
    }

    @Override
    public void initGui() {
        MOEventHandler.settingFocus = true;
        MOEventHandler.mc.setIngameFocus();
    }

}

I originally had settingFocus = false directly after the setIngameFocus() method call, but to prevent the GUI from closing as you mentioned earlier I ended up putting the statement where the capability effect ends, which worked like a charm. Just one small issue that seemed to pop up, however. After the GUI is terminated when the capability effect ends, if any keys were pressed before the GuiScreen was brought up, these same keys would still be registered as being pressed.

 

For example, if I'm moving forward and jumping before the GuiScreen is brought up, then I would continuously be jumping and moving forward when the GuiScreen exits until I press those keys again. It seems I have to reset the keys. Initially I tried setting player movement through the player.movementInput field, but not only did that not work but that still wouldn't fix the other non-movement keybinds from being 'stuck'. Is there any way to reset keys after exiting the GuiScreen, or maybe even right before the GuiScreen shows up? Thanks again!

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.

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.



×
×
  • Create New...

Important Information

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