Posted March 5, 20214 yr 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?
March 5, 20214 yr Author 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.
March 5, 20214 yr Author 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 March 5, 20214 yr by Skyriis
March 5, 20214 yr Author 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.
March 5, 20214 yr Author 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...
March 5, 20214 yr Author 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?
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.