Jump to content

[1.19.2] preInit & postInit


Kosh

Recommended Posts

Hello. I'm trying to update my mod from from 1.12.2 to 1.19.2.

Could someone please explain how can I do the same things as "preInit(FMLPreInitializationEvent event)" and "postInit(FMLPostInitializationEvent event)" methods did in 1.12.2

1) I need something like preInit, or any other way to load mod configs before any Items/Blocks/Entities initialization.

As for example I need to set custom value for entity attribute. When I tried to use it from configuration file I got

IllegalStateException: Cannot get config value before config is loaded.

Here is a link on my mod class https://github.com/NightKosh/Sophisticated-wolves/blob/1.19.2/src/main/java/sophisticated_wolves/SophisticatedWolvesMod.java

2) I need something like postInit to update some data which my mod's API should provide

Link to comment
Share on other sites

https://forge.gemwire.uk/wiki/Stages_of_Modloading

You can't use configurations at registration time. You couldn't really do it in previous versions either.

If you tried, it would just give the default value not the value in the config file.

Now it gives an error so you know you are doing it incorrectly.

 

The usual pattern is to pass a java.util.Supplier of the configuration to your registered objects instead of the value directly.

Then the object does supplier.get() when it needs the value at runtime.

This also means the data updates dynamically, e.g. if the config changes when it comes from the server you are logged into.

e.g. new MyObject(myConfig::getValue) short for new MyObject(() -> myConfig.getValue()) instead of new MyObject(myConfig.getValue())

Or you can just have the objects reference the config directly when they actually need it.

 

There are many questions about different use cases in this forum.

Although a lot are about the "anti-pattern" of injecting config file data into datapack data.

 

 

Edited by warjort

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

1 hour ago, warjort said:

You couldn't really do it in previous versions either.

In 1.12.2 I did it in different events FMLPreInitializationEvent, FMLInitializationEvent and FMLPostInitializationEvent. Also some of configs were used after initialization, but now it looks like it can't be used in such way.

Here is a description of the problem, I mentioned in the previouse post:

I have an entity with configurable Maximum health amount.

This is a method which provides AttributeSupplier (commented line crashed)

    public static AttributeSupplier createAttributeSupplier() {
        return Mob.createMobAttributes()
                .add(Attributes.MOVEMENT_SPEED, 0.4)
                //TODO IllegalStateException: Cannot get config value before config is loaded.
                //.add(Attributes.MAX_HEALTH, SWConfiguration.wolvesHealthWild.get())
                .add(Attributes.ATTACK_DAMAGE, 2)
                .build();
    }

This is an event with attribute supplier registration:

    @SubscribeEvent
    public static void registerEntityAttributes(EntityAttributeCreationEvent event) {
        event.put(getSophisticatedWolfType(), SophisticatedWolf.createAttributeSupplier());
    }

 

Also another one case is Villagers registration

Is it possible to use configs to disable it registration, like in 1.12.2? Or may be it can be done in other way?

 

In case you'll need to look at sources:

Mod: https://github.com/NightKosh/Sophisticated-wolves/tree/feature/1.19.2_update

Entity: https://github.com/NightKosh/Sophisticated-wolves/blob/feature/1.19.2_update/src/main/java/sophisticated_wolves/entity/SophisticatedWolf.java

Entity registration: https://github.com/NightKosh/Sophisticated-wolves/blob/feature/1.19.2_update/src/main/java/sophisticated_wolves/core/SWEntities.java

 

 

Link to comment
Share on other sites

I can confirm the EntityAttributeCreationEvent is fired during registration before the configs are loaded.

But you don't have to code it that way. You can set a default max health in that code.

Then in your entity constructor you can apply a permanent attribute modifier from your configuration.

 

I am the wrong person to talk about this stuff. I generally avoid config files like the plague.

They are just a way for users to break things. 🙂  And only multiply the amount of testing you have to do with different combinations of config options.

Edited by warjort

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.