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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The error code shows that i have outdated mods, but only coFH core crashes my game and it's not outdated + i double checked and its on the right version of forge. -- Head -- Thread: Render thread Stacktrace:     at cofh.core.client.PostEffect.m_6213_(PostEffect.java:80) ~[cofh_core-1.20.1-11.0.2.56.jar%23218!/:11.0.2] {re:mixin,re:classloading}     at cofh.core.client.PostBuffer.m_6213_(PostBuffer.java:96) ~[cofh_core-1.20.1-11.0.2.56.jar%23218!/:11.0.2] {re:classloading}     at net.minecraft.server.packs.resources.ResourceManagerReloadListener.m_10759_(ResourceManagerReloadListener.java:15) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:computing_frames,re:classloading,re:mixin}     at java.util.concurrent.CompletableFuture$UniRun.tryFire(CompletableFuture.java:787) ~[?:?] {}     at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482) ~[?:?] {}     at net.minecraft.server.packs.resources.SimpleReloadInstance.m_143940_(SimpleReloadInstance.java:69) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:classloading}     at net.minecraft.util.thread.BlockableEventLoop.m_6367_(BlockableEventLoop.java:198) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,xf:OptiFine:default,re:computing_frames,pl:accesstransformer:B,xf:OptiFine:default,re:classloading,pl:accesstransformer:B,xf:OptiFine:default}     at net.minecraft.util.thread.ReentrantBlockableEventLoop.m_6367_(ReentrantBlockableEventLoop.java:23) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,re:computing_frames,re:classloading}     at net.minecraft.util.thread.BlockableEventLoop.m_7245_(BlockableEventLoop.java:163) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,xf:OptiFine:default,re:computing_frames,pl:accesstransformer:B,xf:OptiFine:default,re:classloading,pl:accesstransformer:B,xf:OptiFine:default} -- Overlay render details -- Details:     Overlay name: net.minecraftforge.client.loading.ForgeLoadingOverlay Stacktrace:     at net.minecraft.client.renderer.GameRenderer.m_109093_(GameRenderer.java:1385) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,xf:OptiFine:default,re:classloading,pl:accesstransformer:B,xf:OptiFine:default,pl:mixin:APP:moonlight-common.mixins.json:GameRendererMixin,pl:mixin:APP:supplementaries-common.mixins.json:GameRendererMixin,pl:mixin:APP:mixins.cofhcore.json:GameRendererMixin,pl:mixin:APP:create.mixins.json:accessor.GameRendererAccessor,pl:mixin:APP:create.mixins.json:client.GameRendererMixin,pl:mixin:A}     at net.minecraft.client.Minecraft.m_91383_(Minecraft.java:1146) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.m_91374_(Minecraft.java:718) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:218) ~[1.20.1-forge-47.3.0.jar:?] {re:mixin,pl:runtimedistcleaner:A,re:classloading,pl:mixin:APP:flywheel.mixins.json:ClientMainMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.runTarget(CommonLaunchHandler.java:111) ~[fmlloader-1.20.1-47.3.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.clientService(CommonLaunchHandler.java:99) ~[fmlloader-1.20.1-47.3.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonClientLaunchHandler.lambda$makeService$0(CommonClientLaunchHandler.java:25) ~[fmlloader-1.20.1-47.3.0.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:108) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:78) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at crystallauncher.MinecraftConstructor.run(MinecraftConstructor.java:29) ~[proxyserver.jar%2380!/:?] {}     at crystallauncher.MineClient.start(MineClient.java:201) ~[proxyserver.jar%2380!/:?] {}     at crystallauncher.MineClient.main(MineClient.java:42) ~[proxyserver.jar%2380!/:?] {}
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system
    • Need help with making shooting book, in this code i can summon fireball but it goes inside or oposite way can you help me? package net.LimboTeam.tropicmod.item.custom; import net.LimboTeam.tropicmod.item.ModCreativeModTab; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.projectile.LargeFireball; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import java.util.Random; public class FireballMagicBook extends Item { public FireballMagicBook(Properties properties) { super(new Item.Properties().tab(ModCreativeModTab.TropicModTab)); } @Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack itemStack = player.getItemInHand(hand); Random random = new Random(); Vec3 vec3 = player.getViewVector(1.0f); if (!level.isClientSide && hand == InteractionHand.MAIN_HAND){ double X = player.getX() - (player.getX() + vec3.x * 4.0); double Y = player.getY(0.5) - (0.5 + player.getY(0.5)); double Z = player.getZ() - (player.getZ() + vec3.z * 4.0); LargeFireball fireball = new LargeFireball(level, player, X , Y, Z,1); fireball.setPos(player.getX(), player.getY(), player.getZ()); level.addFreshEntity(fireball); level.playSound((Player)null, player.getX(), player.getY(), player.getZ(), SoundEvents.GHAST_SHOOT, SoundSource.NEUTRAL, 0.5F, 0.4F / (random.nextFloat() * 0.4F + 0.8F)); if (!player.isCreative()) { itemStack.setDamageValue(itemStack.getDamageValue() + 1); } player.getCooldowns().addCooldown(this, 40 ); return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemStack); } return new InteractionResultHolder<>(InteractionResult.FAIL, itemStack); } }  
  • Topics

×
×
  • Create New...

Important Information

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