Jump to content

[1.16.5] Change Keybinding Inputkey


Skyriis

Recommended Posts

Hi Guys,

 

i'm trying to change the input key of a keybinding if the keybinding is contained by a HashMap (which also contains the new input key).

 

Here is what i'm calling at the FMLClientSetupEvent. 

KeyBinding[] keyBindings = Minecraft.getInstance().gameSettings.keyBindings.clone();
        for (KeyBinding keyBinding : keyBindings) {
            if (SyncedKeyBindings.containsKey(keyBinding.getKeyDescription())) {
                KeyData data = SyncedKeyBindings.get(keyBinding.getKeyDescription());
                keyBinding.setKeyModifierAndCode(data.getModifier(), data.getValue());
                final GameSettings gameSettings = Minecraft.getInstance().gameSettings;

                if (data.getValue().toString().contains("mouse")) {
                    gameSettings.setKeyBindingCode(keyBinding, InputMappings.Type.MOUSE.getOrMakeInput(data.getValue().getKeyCode()));
                } else {
                    gameSettings.setKeyBindingCode(keyBinding, InputMappings.getInputByName(data.getValue().toString()));
                }

                LOGGER.debug("Changed " + data.getIdentidier() + " to " + data.getModifier().toString() + " + " + data.getValue().toString());
            }
        }

        KeyBinding.resetKeyBindingArrayAndHash();

 

For some reason the new input won't be appled to the keybinding.

Is the FMLClientSetupEvent to early to change the input key? Or am i missing something?

Link to comment
Share on other sites

I'm trying to load already changed keybindings from other Profiles to the current instance so the keybindings are already set if you start playing.

This only applies to keybindings which where marked in the original Profile and should sync to other Profiles.

Link to comment
Share on other sites

The GameSettings.setKeyBindingCode() Method should call the saveOptions() Method which should save the Keybindings to the options.txt file.

But the Keybindings only apply if there is no options.txt before startup.

If the options.txt has been generated, the loaded Keybindings don't apply to the started instance anymore.

 

Even if i load the options.txt again using the loadOptions() Method, the Keybinds won't be there.

 

Edit:

i guess the

if (net.minecraftforge.fml.client.ClientModLoader.isLoading()) return;

part inside of the saveOptions() chould be the issue since i'm calling it at the FMLClientSetupEvent.

 

Is there a better Event i chould use to apply the Keybindings?

 

Edited by Skyriis
Link to comment
Share on other sites

The loaded Keybindings only should be applied to the running instance at startup (or exactly after startup).

The Goal is to make a Mod which sets your favorite keybinds on the first load and sync changed (favorite) keybinds to other instances so you don't have to set them manually anymore.

 

Link to comment
Share on other sites

I could also start a new Thread at FMLClientSetupEvent which could wait for the loadup and instandly apply the Keybindings but i'm not sure if that would be a good way since the new Thread would have to check ClientModLoader.isLoading until it's false...

Link to comment
Share on other sites

I tried

new Thread(() -> {
                while (ClientModLoader.isLoading()) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Minecraft.getInstance().enqueue(() -> SyncHandler.applyKeybindings());
            }).start();

 and it would "work" but since GameSettings is not threadsafe it would be a bad idea to use this correct? :S

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.

Announcements



×
×
  • Create New...

Important Information

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