Jump to content

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


GEOEGII555

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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