Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Custom keybinding crashes game when going to controls


PlanetTeamSpeak
 Share

Recommended Posts

I have made a custom keybinding which works fine but whenever I launch the game, go to options and click controls, the game crashes for some reason.

Here's how I registered the keybinding:

KeyBinding keyBind = new KeyBinding("Toggle overlay", Keyboard.KEY_C, "MoreCommands")
ClientRegistry.registerKeyBinding(keyBind);

Crash report:

java.lang.NullPointerException: Rendering screen
	at net.minecraft.client.gui.GuiListExtended.updateItemPos(GuiListExtended.java:41)
	at net.minecraft.client.gui.GuiSlot.drawSelectionBox(GuiSlot.java:446)
	at net.minecraft.client.gui.GuiSlot.drawScreen(GuiSlot.java:236)
	at net.minecraft.client.gui.GuiControls.drawScreen(GuiControls.java:163)
	at net.minecraftforge.client.ForgeHooksClient.drawScreen(ForgeHooksClient.java:349)
	at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1168)
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1192)
	at net.minecraft.client.Minecraft.run(Minecraft.java:436)
	at net.minecraft.client.main.Main.main(Main.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
	at GradleStart.main(GradleStart.java:26)

 

Edited by PlanetTeamSpeak
Link to comment
Share on other sites

  • 1 month later...

I'm also having this issue. So far, this is what I know:

 

It initially looks like an array indexing error, with GuiListExtended::getListEntry being called with an entryID which is out of bounds. In my case, the entryID is 39. However, my listEntries is 40 elements long, with the last one (index 39) having value "null". (Stupid java and it's "everything is a pointer and can be null hur-dur-dur").

 

listEntries is created with a fixed length equal to the number of key bindings in the game (stored in the field Minecraft::gameSettings.keyBindings) plus the number of keybind categories (stored in the static field net.minecraft.client.settings.KeyBinding.KEYBIND_SET, which is very misleading - it's actually a set of strings, each being the unlocalized name of a keybind category). Whenever you initialize a KeyBinding, its category is added to the set of categories. Note that it's a set, not a list, so it can't have duplicates.

 

Once the length is known, the values are initialized by iterating over the key bindings. (The "current category" string is initialized to an empty string outside the loop.) If the key binding's category is different than the current category, the game adds a "category header" UI element for it. Then, it adds the actual UI element for the key binding.

 

The issue is arising (I think) because we're adding a keybinding category to the set, but a key isn't known to the game with that category, so the category header UI element is never added to listEntries. This leaves the last value of listEntries null, since the game is expecting there to be an additional category header which was never actually created.

 

This is very bad defensive coding on the part of the original coders, and is incredibly fragile, as we both discovered. At the very least there should be checks that the list is fully populated, so that the game can fail fast and make issues like this a lot easier to debug.

 

As far as how to fix it, for me, I had just forgotten to call ClientRegistry.registerKeyBinding for all of my bindings. registerKeyBinding extends the array in Minecraft::gameSettings.keyBindings so that the UI elements will be created for your custom bindings, and so the game won't crash because a category isn't used. I'm initializing all the KeyBinding instances during the init phase (as opposed to pre-init, post-init, or at static initialization time), and calling ClientRegistry.registerKeyBinding immediately after that. Maybe you're initializing them at the wrong time?

Link to comment
Share on other sites

  • 3 years later...

Its crashing because "MoreCommands" is not valid category in the game

 

//Simple fix

public KeyBinding keyBind = new KeyBinding("Toggle overlay", Keyboard.KEY_C, "key.categories.misc")

 

 

Edited by Rreis019
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.