The ScreenHandlerRegistry failed to work occasionally
rikka0w0 opened this issue ยท 2 comments
In some case, some Mod Gui cannot be opened in a multiplayer session. It does not crash the game, but something like the following is printed on the client's log:
Trying to open invalid screen with name: Potion Bag
. Closer examine shows that the msg is printed by net.minecraft.client.gui.screen.Screens.open(@Nullable ContainerType<T> type, MinecraftClient client, int id, Text name)
. Its value comes from OpenContainerS2CPacket:
public ContainerType<?> getContainerType() {
return (ContainerType)Registry.CONTAINER.get(this.containerId);
}
This mod (https://github.com/zabi94/ExtraAlchemy) uses the ScreenHandlerRegistry in a very standard way. The ScreenHandlerType variable POTION_BAG is a static variable and statically initialized. The problem will happen if the POTION_BAG is not accessed during the mod initialization. By adding zabi.minecraft.extraalchemy.screen.ModScreenHandlerTypes.POTION_BAG.toString();
to the end of the mod initializer, the problem is solved.
I would suggest that the ScreenHandlerType variables should be initialized in the mod initializer, it should not be statically initialized.
Extra Alchemy dev here. It's not an api problem, statically initializing things and assigning the value through a registration isn't exactly what I'd define standard.
It's fixed already in the mod by using a method called during mod init to register and assign the values.
This issue can be closed.
Ideally you'd called some empty no-op method to load the class instance to register the screen handler types.
I do this in a single init
method I call in my mod which then loads everything from all registries: https://github.com/i509VCB/BulkyShulkies/blob/1.16.x/src/main/java/me/i509/fabric/bulkyshulkies/registry/ShulkerRegistries.java
Will close this since it's a static init issue on the side of user error