Jump to content

init() method not called on a screen because of a Forge modification, is there any alternative way?


Recommended Posts

Posted (edited)

So, I was trying to create a screen. I created an init method which adds all the buttons: (1.19.2)

    @Override
    protected void init() {
        this.addRenderableWidget(new Button(this.width / 2 - (18 / 2), this.height / 2 - (50 / 2), 18, 50, Component.literal("Test"), button -> {
            this.minecraft.setScreen(null);
        }));
    }

But it didn't work. At first I thought it was my fault, but after some digging I found out this:

   public final void init(Minecraft p_96607_, int p_96608_, int p_96609_) {
      this.minecraft = p_96607_;
      this.itemRenderer = p_96607_.getItemRenderer();
      this.font = p_96607_.font;
      this.width = p_96608_;
      this.height = p_96609_;
      java.util.function.Consumer<GuiEventListener> add = (b) -> {
         if (b instanceof Widget w)
            this.renderables.add(w);
         if (b instanceof NarratableEntry ne)
            this.narratables.add(ne);
         children.add(b);
      };
      if (!net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.client.event.ScreenEvent.Init.Pre(this, this.children, add, this::removeWidget))) {
      this.rebuildWidgets();
      this.triggerImmediateNarration(false);
      this.suppressNarration(NARRATE_SUPPRESS_AFTER_INIT_TIME);
      }
      net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.client.event.ScreenEvent.Init.Post(this, this.children, add, this::removeWidget));
   }

Now, it's not the entire code. Only one part of it:

      if (!net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.client.event.ScreenEvent.Init.Pre(this, this.children, add, this::removeWidget))) {
      this.rebuildWidgets();
      this.triggerImmediateNarration(false);
      this.suppressNarration(NARRATE_SUPPRESS_AFTER_INIT_TIME);
      }

Apparently, Minecraft Forge made it so that if it succeeds in posting a message to the event bus, it does not call the [rebuildWidgets()] method, which results in [init()] not being called (since rebuildWidgets is responsible for calling init() without arguments):

   protected void rebuildWidgets() {
      this.clearWidgets();
      this.setFocused((GuiEventListener)null);
      this.init();
   }

I was relying on the fact, that [public final void init(Minecraft minecraft, int width, int height)] would call the [protected void init()] method (via the [rebuildWidgets()] method), but it doesn't. I can't overwrite the method with 3 arguments since it's [final]. Is there any other place which is called to init the screen? I tried doing it on the constructor, but before [public final void init(Minecraft minecraft, int width, int height)] was called, width and height were 0.

 

How can I init my screen? I don't think doing it in the render method would be the correct way.

Edited by GEOEGII555
Posted

For the 10,000th time...

Posting code snippets out of context in the forums will just mean you are ignored unless your problem is obvious.

To get help you need to post a build (preferably on github) so we can see **all** the relevant code and reproduce your problem.

 

The rest of your comments are wrong.

Forge will only skip your init method if some other mod decides to **cancel** that Screen.Init.Pre event. i.e. it wants to rework your screen.

https://github.com/MinecraftForge/MinecraftForge/blob/e69f5e262ac6481ca35e181b1720e99bc50320fe/src/main/java/net/minecraftforge/client/event/ScreenEvent.java#L119

 

There was however a bug relating to this processing in some older 1.19.4 forge releases:

https://github.com/MinecraftForge/MinecraftForge/pull/9431

But you don't even bother to say which version you are using.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted
9 minutes ago, warjort said:

For the 10,000th time...

Posting code snippets out of context in the forums will just mean you are ignored unless your problem is obvious.

To get help you need to post a build (preferably on github) so we can see **all** the relevant code and reproduce your problem.

 

The rest of your comments are wrong.

Forge will only skip your init method if some other mod decides to **cancel** that Screen.Init.Pre event. i.e. it wants to rework your screen.

https://github.com/MinecraftForge/MinecraftForge/blob/e69f5e262ac6481ca35e181b1720e99bc50320fe/src/main/java/net/minecraftforge/client/event/ScreenEvent.java#L119

 

There was however a bug relating to this processing in some older 1.19.4 forge releases:

https://github.com/MinecraftForge/MinecraftForge/pull/9431

But you don't even bother to say which version you are using.

Added that, I use 1.19.2. Is it affected by the bug?

Posted

Not that I'm aware of.

I believe the bug arose out of changes made by Mojang to the Screen processing when they rewrote the "Create New World" screen in 1.19.4

If it did apply, I would have expect one of the mods that add or modify screens to have requested a backport of that fix by now.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am having some issues starting an RLCraft server on a minimal install of Debian 12. I have Java installed and I'm able to start the vanilla Minecraft server jar no problem and people can join and play without any issues, as soon as I try to create a new directory with the Forge jar the initial install with the INSTALLER jar works when I use the java command with the --installServer flag, but as soon as I try to start the server using the forge jar that is NOT labelled with installer I get the following error: A problem occurred running the Server launcher.java.lang.reflect.InvocationTargetException         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)         at java.base/java.lang.reflect.Method.invoke(Method.java:569)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.run(ServerLaunchWrapper.java:70)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.main(ServerLaunchWrapper.java:34) Caused by: java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')         at net.minecraft.launchwrapper.Launch.<init>(Launch.java:34)         at net.minecraft.launchwrapper.Launch.main(Launch.java:28)         ... 6 more   I have tried using newer versions of Java directly from Oracle as well. Has anybody been successful in starting and running a RLCraft server from the terminal on a Linux machine? I cannot figure out why it doesn't want to work but the vanilla jar works without issue. Thank you in advance!
    • This is my latest attempt :  public class ManaScreen extends Screen { Mana mana = new Mana(); boolean removeManaBar = false; ResourceLocation manaBar = ResourceLocation.fromNamespaceAndPath(RSGArmoury.MOD_ID, "/textures/block/spawnable_arena_wall.png"); public ManaScreen() { super(Component.literal("Mana")); } @Override protected void init() { super.init(); Minecraft.getInstance().setScreen(this); } @Override public boolean isPauseScreen() { return false; } @Override public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) { pGuiGraphics.blit(manaBar, 10, -10, 0, 0, mana.getMana(), 10, mana.getMana(), 10); if (removeManaBar) { this.onClose(); return; } super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick); } public void addManaBar() { removeManaBar = false; Minecraft.getInstance().setScreen(new ManaScreen()); } public boolean removeManaBar() { return removeManaBar = true; } }
    • I tried a few different things that all didnt work. Right now I have nothing but what I had that seemed most likely to work was just a guiOverlay.blit(x, y, z, vx, vy, getMana()). I dont remember the exact code but it was somthing along those lines. It was in a new class extending screen I believe.
    • yo help pls  i get this error message The game crashed whilst rendering screen Error: java.lang.NullPointerException: Rendering screen https://pastebin.com/fHwr7nXA
  • Topics

×
×
  • Create New...

Important Information

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