Jump to content

Custom keybinding crashes game when going to controls


PlanetTeamSpeak

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 locked this topic
Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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