Jump to content

[Resolved] [1.14.3] Access config settings during Registry events


Recommended Posts

Posted (edited)

Hi together, I am in kind of a pickle here. I either missed something on how to register/access the config or I have a conceptional error in my thinking. I want to create a mod that contains several different enchantments -- nothing too spectacular here yet. Via config files I want to make it configurable which enchantments are enabled. Though by trying out I realized that the configurations are loaded AFTER the Registration events have fired. So my question now is: Does anyone know how I can make it configurable if an enchantment is enabled or not? For clarity, here some snippets:

 

Crash Report (which shows the Config is not initialized yet):

[17:04:59.454] [Server thread/INFO] [minecraft/DedicatedServer]: Starting minecraft server version 1.14.3
[17:04:59.639] [modloading-worker-9/INFO] [ne.mi.co.ForgeMod/FORGEMOD]: Forge mod loading, version 27.0.57, for MC 1.14.3 with MCP 20190624.152911
[17:04:59.672] [modloading-worker-9/INFO] [ne.mi.co.MinecraftForge/FORGE]: MinecraftForge v27.0.57 Initialized
[17:04:59.836] [Server thread/ERROR] [ne.mi.fm.ja.FMLModContainer/]: Exception caught during firing event: Cannot get config value without assigned Config object present
        Index: 1
        Listeners:
                0: NORMAL
                1: ASM: class de.oxur.qole.enchantments.Enchantments$RegistrationHandler registerEnchantments(Lnet/minecraftforge/event/RegistryEvent$Register;)V
java.lang.NullPointerException: Cannot get config value without assigned Config object present
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
        at net.minecraftforge.common.ForgeConfigSpec$ConfigValue.get(ForgeConfigSpec.java:682)
        at de.oxur.qole.enchantments.Enchantments$RegistrationHandler.registerEnchantments(Enchantments.java:30)
        at net.minecraftforge.eventbus.ASMEventHandler_0_RegistrationHandler_registerEnchantments_Register.invoke(.dynamic)
        at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:80)
        at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258)
        at net.minecraftforge.fml.javafmlmod.FMLModContainer.fireEvent(FMLModContainer.java:106)
        at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65)
        at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65)
        at net.minecraftforge.fml.ModContainer.transitionState(ModContainer.java:112)
        at net.minecraftforge.fml.ModList.lambda$dispatchSynchronousEvent$4(ModList.java:110)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at net.minecraftforge.fml.ModList.dispatchSynchronousEvent(ModList.java:110)
        at net.minecraftforge.fml.ModList.lambda$static$0(ModList.java:81)
        at net.minecraftforge.fml.LifecycleEventProvider.dispatch(LifecycleEventProvider.java:71)
        at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:173)
        at net.minecraftforge.fml.ModLoader.lambda$gatherAndInitializeMods$21(ModLoader.java:165)
        at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:915)
        at net.minecraftforge.fml.ModLoader.gatherAndInitializeMods(ModLoader.java:165)
        at net.minecraftforge.fml.server.ServerModLoader.begin(ServerModLoader.java:45)
        at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:121)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:585)
        at java.lang.Thread.run(Thread.java:748)

[17:04:59.842] [Server thread/ERROR] [ne.mi.fm.ja.FMLModContainer/LOADING]: Caught exception during event RegistryEvent.Register<minecraft:enchantment> dispatch for modid oxur_qole
java.lang.NullPointerException: Cannot get config value without assigned Config object present
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787) ~[server-1.14.3-extra.jar:?] {}
        at net.minecraftforge.common.ForgeConfigSpec$ConfigValue.get(ForgeConfigSpec.java:682) ~[?:?] {}
        at de.oxur.qole.enchantments.Enchantments$RegistrationHandler.registerEnchantments(Enchantments.java:30) ~[?:1.0] {}
        at net.minecraftforge.eventbus.ASMEventHandler_0_RegistrationHandler_registerEnchantments_Register.invoke(.dynamic) ~[?:?] {}
        at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:80) ~[eventbus-0.10.3-milestone.0.1+1a5fa31-service.jar:?] {}
        at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258) ~[eventbus-0.10.3-milestone.0.1+1a5fa31-service.jar:?] {}
        at net.minecraftforge.fml.javafmlmod.FMLModContainer.fireEvent(FMLModContainer.java:106) ~[?:27.0] {}
        at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65) ~[?:1.8.0_211] {}
        at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65) ~[?:1.8.0_211] {}
        at net.minecraftforge.fml.ModContainer.transitionState(ModContainer.java:112) ~[?:?] {}
        at net.minecraftforge.fml.ModList.lambda$dispatchSynchronousEvent$4(ModList.java:110) ~[?:?] {}
        at java.util.ArrayList.forEach(ArrayList.java:1257) ~[?:1.8.0_211] {}
        at net.minecraftforge.fml.ModList.dispatchSynchronousEvent(ModList.java:110) ~[?:?] {}
        at net.minecraftforge.fml.ModList.lambda$static$0(ModList.java:81) ~[?:?] {}
        at net.minecraftforge.fml.LifecycleEventProvider.dispatch(LifecycleEventProvider.java:71) ~[?:?] {}
        at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:173) ~[?:?] {}
        at net.minecraftforge.fml.ModLoader.lambda$gatherAndInitializeMods$21(ModLoader.java:165) ~[?:?] {}
        at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:915) [?:?] {}
        at net.minecraftforge.fml.ModLoader.gatherAndInitializeMods(ModLoader.java:165) [?:?] {}
        at net.minecraftforge.fml.server.ServerModLoader.begin(ServerModLoader.java:45) [?:?] {}
        at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:121) [?:?] {pl:accesstransformer:B}
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:585) [?:?] {pl:accesstransformer:B,pl:runtimedistcleaner:A}
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211] {}
[17:04:59.863] [Server thread/FATAL] [ne.mi.fm.ModLoader/LOADING]: Failed to complete lifecycle event LOAD_REGISTRIES, 1 errors found
[17:04:59.864] [Server thread/ERROR] [minecraft/MinecraftServer]: Encountered an unexpected exception
net.minecraftforge.fml.LoadingFailedException: null
        at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:177) ~[?:?] {}
        at net.minecraftforge.fml.ModLoader.lambda$gatherAndInitializeMods$21(ModLoader.java:165) ~[?:?] {}
        at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:915) ~[?:?] {}
        at net.minecraftforge.fml.ModLoader.gatherAndInitializeMods(ModLoader.java:165) ~[?:?] {}
        at net.minecraftforge.fml.server.ServerModLoader.begin(ServerModLoader.java:45) ~[?:?] {}
        at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:121) ~[?:?] {pl:accesstransformer:B}
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:585) [?:?] {pl:accesstransformer:B,pl:runtimedistcleaner:A}
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211] {}
[17:04:59.896] [Server thread/ERROR] [minecraft/MinecraftServer]: This crash report has been saved to: C:\Users\USERNAME\Documents\Forge\server\.\crash-reports\crash-2019-07-18_17.04.59-server.txt
[17:04:59.920] [Server thread/INFO] [minecraft/MinecraftServer]: Stopping server
[17:04:59.920] [Server thread/INFO] [minecraft/MinecraftServer]: Saving worlds
[17:04:59.929] [Server thread/ERROR] [minecraft/MinecraftServer]: Exception stopping the server
java.lang.IllegalArgumentException: Can not hotload overworld. This must be loaded at all times by main Server.
        at org.apache.commons.lang3.Validate.isTrue(Validate.java:158) ~[server-1.14.3-extra.jar:?] {}
        at net.minecraftforge.common.DimensionManager.initWorld(DimensionManager.java:201) ~[?:?] {}
        at net.minecraftforge.common.DimensionManager.getWorld(DimensionManager.java:170) ~[?:?] {}
        at net.minecraft.server.MinecraftServer.func_71218_a(MinecraftServer.java:958) ~[?:?] {pl:accesstransformer:B,pl:runtimedistcleaner:A}
        at net.minecraft.server.MinecraftServer.func_213211_a(MinecraftServer.java:508) ~[?:?] {pl:accesstransformer:B,pl:runtimedistcleaner:A}
        at net.minecraft.server.MinecraftServer.func_71260_j(MinecraftServer.java:540) ~[?:?] {pl:accesstransformer:B,pl:runtimedistcleaner:A}
        at net.minecraft.server.dedicated.DedicatedServer.func_71260_j(DedicatedServer.java:540) ~[?:?] {pl:accesstransformer:B}
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:645) [?:?] {pl:accesstransformer:B,pl:runtimedistcleaner:A}
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211] {}

 

Config class (Don't bother about the SERVER variable, but config COMMON type. I was playing around with the different types of configs, thats all):

package de.oxur.qole;

import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.config.ModConfig;
import org.apache.commons.lang3.tuple.Pair;

public class QoLEModConfig {

    private static final ForgeConfigSpec serverSpec;
    public static final Server SERVER;

    static {
        final Pair<Server, ForgeConfigSpec> specPair = new ForgeConfigSpec.Builder().configure(Server::new);
        serverSpec = specPair.getRight();
        SERVER = specPair.getLeft();

    }

    public static void register(final ModLoadingContext context) {
        context.registerConfig(ModConfig.Type.COMMON, serverSpec);
    }

    public static class Server {
        public final ForgeConfigSpec.BooleanValue enableVeinminer;

        Server(final ForgeConfigSpec.Builder builder) {
            builder.comment("Enable and disable enchantments").push("enchantments");
            enableVeinminer = builder
                    .comment("Enables or disables the veinminer enchantment")
                    .translation("oxur_qole.config.common.enableVeinminer")
                    .define("enableVeinminer", true);
            builder.pop();
        }
    }
}

 

ModClass constructor:

public QoLEMod() {
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        QoLEModConfig.register(ModLoadingContext.get());
    }

 

EventHandler responsible for registering the enchantment and causing the crash:

@SubscribeEvent
        public static void registerEnchantments(final RegistryEvent.Register<Enchantment> event) {
            LOGGER.debug("Registering enchantments of {}", QoLEMod.MODID);
            IForgeRegistry<Enchantment> registry = event.getRegistry();
            if(QoLEModConfig.SERVER.enableVeinminer.get()) {
                Enchantment enchantment = new VeinminerEnchantment(Enchantment.Rarity.UNCOMMON, EnchantmentType.DIGGER, EquipmentSlotType.MAINHAND).setRegistryName("veinminer");
                registry.register(enchantment);
            }
        }

 

 

Thanks in advance

Edited by SamuelYaron
Posted

-- If alternatively there is a way to remove an item from the registry again after the Config has loaded this would be fine for me as well or is it even acceptable practice to register objects in the ModConfig.Loading event instead?

Posted

Because Enchantments are registry objects you should be always registering all of them all the time. You will need to find a different way of preventing them from being used.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Yeah, I expected something like that already..

 

For anyone interested: I solved my particular situation by making the canApply function of the enchantment always return false if my setting is false. This function is not called at registration time and thus has access to the config. That creates the following behavior:

Items can be enchanted neither via Anvil+book nor at the enchantment table anymore

Existing enchanted items and books still exist (though are useless since I respect the config also in the logic handling the enchantment)

Creative/commands are still able to create this enchantments in some cases

 

That behavior is enough for me. Thanks for the reply

  • Like 1
Posted
19 minutes ago, SamuelYaron said:

Yeah, I expected something like that already..

 

For anyone interested: I solved my particular situation by making the canApply function of the enchantment always return false if my setting is false. This function is not called at registration time and thus has access to the config. That creates the following behavior:

Items can be enchanted neither via Anvil+book nor at the enchantment table anymore

Existing enchanted items and books still exist (though are useless since I respect the config also in the logic handling the enchantment)

Creative/commands are still able to create this enchantments in some cases

 

That behavior is enough for me. Thanks for the reply

This... THIS is how people should handle things rather than insisting that you should be allowed to conditionally register things.

</rant>

  • Like 1

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

Spoiler

Logs (Most issues require logs to diagnose):

Spoiler

Please post logs using one of the following sites (Thank you Lumber Wizard for the list):

https://gist.github.com/100MB Requires member (Free)

https://pastebin.com/: 512KB as guest, 10MB as Pro ($$$)

https://hastebin.com/: 400KB

Do NOT use sites like Mediafire, Dropbox, OneDrive, Google Drive, or a site that has a countdown before offering downloads.

 

What to provide:

...for Crashes and Runtime issues:

Minecraft 1.14.4 and newer:

Post debug.log

Older versions:

Please update...

 

...for Installer Issues:

Post your installer log, found in the same place you ran the installer

This log will be called either installer.log or named the same as the installer but with .log on the end

Note for Windows users:

Windows hides file extensions by default so the installer may appear without the .jar extension then when the .log is added the log will appear with the .jar extension

 

Where to get it:

Mojang Launcher: When using the Mojang launcher debug.log is found in .minecraft\logs.

 

Curse/Overwolf: If you are using the Curse Launcher, their configurations break Forge's log settings, fortunately there is an easier workaround than I originally thought, this works even with Curse's installation of the Minecraft launcher as long as it is not launched THROUGH Twitch:

Spoiler
  1. Make sure you have the correct version of Forge installed (some packs are heavily dependent on one specific build of Forge)
  2. Make a launcher profile targeting this version of Forge.
  3. Set the launcher profile's GameDir property to the pack's instance folder (not the instances folder, the folder that has the pack's name on it).
  4. Now launch the pack through that profile and follow the "Mojang Launcher" instructions above.

Video:

Spoiler

 

 

 

or alternately, 

 

Fallback ("No logs are generated"):

If you don't see logs generated in the usual place, provide the launcher_log.txt from .minecraft

 

Server Not Starting:

Spoiler

If your server does not start or a command window appears and immediately goes away, run the jar manually and provide the output.

 

Reporting Illegal/Inappropriate Adfocus Ads:

Spoiler

Get a screenshot of the URL bar or copy/paste the whole URL into a thread on the General Discussion board with a description of the Ad.

Lex will need the Ad ID contained in that URL to report it to Adfocus' support team.

 

Posting your mod as a GitHub Repo:

Spoiler

When you have an issue with your mod the most helpful thing you can do when asking for help is to provide your code to those helping you. The most convenient way to do this is via GitHub or another source control hub.

When setting up a GitHub Repo it might seem easy to just upload everything, however this method has the potential for mistakes that could lead to trouble later on, it is recommended to use a Git client or to get comfortable with the Git command line. The following instructions will use the Git Command Line and as such they assume you already have it installed and that you have created a repository.

 

  1. Open a command prompt (CMD, Powershell, Terminal, etc).
  2. Navigate to the folder you extracted Forge’s MDK to (the one that had all the licenses in).
  3. Run the following commands:
    1. git init
    2. git remote add origin [Your Repository's URL]
      • In the case of GitHub it should look like: https://GitHub.com/[Your Username]/[Repo Name].git
    3. git fetch
    4. git checkout --track origin/master
    5. git stage *
    6. git commit -m "[Your commit message]"
    7. git push
  4. Navigate to GitHub and you should now see most of the files.
    • note that it is intentional that some are not synced with GitHub and this is done with the (hidden) .gitignore file that Forge’s MDK has provided (hence the strictness on which folder git init is run from)
  5. Now you can share your GitHub link with those who you are asking for help.

[Workaround line, please ignore]

 

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



×
×
  • Create New...

Important Information

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