Jump to content

Recommended Posts

Posted (edited)

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
Posted

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

Posted

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!

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi everyone, I'm currently developing a Forge 1.21 mod for Minecraft and I want to display a custom HUD overlay for a minigame. My goal: When the game starts, all players should see an item/block icon (from the base game, not a custom texture) plus its name/text in the HUD – similar to how the bossbar overlay works. The HUD should appear centered above the hotbar (or at a similar prominent spot), and update dynamically (icon and name change as the target item changes). What I've tried: I looked at many online tutorials and several GitHub repos (e.g. SeasonHUD, MiniHUD), but most of them use NeoForge or Forge versions <1.20 that provide the IGuiOverlay API (e.g. implements IGuiOverlay, RegisterGuiOverlaysEvent). In Forge 1.21, it seems that neither IGuiOverlay nor RegisterGuiOverlaysEvent exist anymore – at least, I can't import them and they are missing from the docs and code completion. I tried using RenderLevelStageEvent as a workaround but it is probably not intended for custom HUDs. I am not using NeoForge, and switching the project to NeoForge is currently not an option for me. I tried to look at the original minecraft source code to see how elements like hearts, hotbar etc are drawn on the screen but I am too new to Minecraft modding to understand. What I'm looking for: What is the correct way to add a custom HUD element (icon + text) in Forge 1.21, given that the previous overlay API is missing? Is there a new recommended event, callback, or method in Forge 1.21 for custom HUD overlays, or is everyone just using a workaround? Is there a minimal open-source example repo for Forge 1.21 that demonstrates a working HUD overlay without relying on NeoForge or deprecated Forge APIs? My ideal solution: Centered HUD element with an in-game item/block icon (from the base game's assets, e.g. a diamond or any ItemStack / Item) and its name as text, with a transparent background rectangle. It should be visible to the players when the mini game is running. Easy to update the item (e.g. static variable or other method), so it can change dynamically during the game. Any help, code snippets, or up-to-date references would be really appreciated! If this is simply not possible right now in Forge 1.21, it would also help to know that for sure. Thank you very much in advance!
    • The simple answer is there is not an easy way. You would need to know how to program in Java, as well as at least some familiarity with how Forge works so you could port the differences. You would also need the sourcecode for the original mod, and permission from the author to modify it, if they did not use some sort of open source license. So it's not impossible, but it would take some effort, but doing so would open up a whole new world of possibilities for you!
    • Does it still crash if you remove holdmyitems? Looks like that mod doesn't work on a server as far as I can tell from the error.  
    • Crashes the server when trying to start. Error code -1. Log  
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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