Jump to content

[1.16.4] Mod registries not loaded before Event Listener


st4s1k

Recommended Posts

Hi guys, I'm trying to access `ForgeRegistries.ITEMS` at the stage of config initialization but there are no mod items at this stage, here are some logs listing all items before and after config loading.
 

[19:24:19] [modloading-worker-9/INFO] [co.st.st.StackDown/]: ForgeRegistries.ITEMS:
[19:24:19] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:air
[19:24:19] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:stone
[19:24:19] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:granite
... vanilla minecraft items ...
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:polished_blackstone_bricks
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:polished_blackstone_brick_slab
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:polished_blackstone_brick_stairs
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:cracked_polished_blackstone_bricks
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]:   minecraft:respawn_anchor
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]: Loading config: D:\Downloads\MC\server-forge\config\stack-down.toml
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]: Built config: D:\Downloads\MC\server-forge\config\stack-down.toml
[19:24:21] [modloading-worker-9/INFO] [co.st.st.StackDown/]: Loaded config: D:\Downloads\MC\server-forge\config\stack-down.toml
[19:24:21] [main/ERROR] [minecraft/Util]: No data fixer registered for
[19:24:21] [modloading-worker-15/INFO] [co.st.st.StackDown/]: ForgeRegistries.ITEMS:
[19:24:21] [Forge Version Check/INFO] [ne.mi.fm.VersionChecker/]: [forge] Starting version check at https://files.minecraftforge.net/maven/net/minecraftforge/forge/promotions_slim.json
[19:24:21] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   minecraft:air
[19:24:21] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   minecraft:stone
[19:24:21] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   minecraft:granite
... vanilla minecraft items ...
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   minecraft:polished_blackstone_brick_stairs
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   minecraft:cracked_polished_blackstone_bricks
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   minecraft:respawn_anchor
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   toughtrees:flint_axe
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   toughtrees:flint_pickaxe
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:backpack
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:iron_backpack
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:gold_backpack
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:diamond_backpack
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:pickup_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_pickup_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:filter_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_filter_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:magnet_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_magnet_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:feeding_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:compacting_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_compacting_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:void_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_void_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:restock_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_restock_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:deposit_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_deposit_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:refill_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:inception_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:everlasting_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:smelting_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:auto_smelting_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:crafting_upgrade
[19:24:23] [modloading-worker-15/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:upgrade_base


Here is my code, it works in SSP but doesn't work in SMP:
 

@Mod("stackdown")
public class StackDown {
    public static final Logger LOGGER = LogManager.getLogger();

    public StackDown() {
        ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, Config.config);
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::overrideMaxStackSize);
        MinecraftForge.EVENT_BUS.register(this);
        Config.loadConfig(Config.config, FMLPaths.CONFIGDIR.get().resolve("stack-down.toml").toString());
    }

    private void overrideMaxStackSize(final FMLLoadCompleteEvent event) {
        StackDown.LOGGER.info("ForgeRegistries.ITEMS:");
        ForgeRegistries.ITEMS.getValues().forEach(item ->
            StackDown.LOGGER.info("  {}", item.getRegistryName()));

        if (overrideMaxStackSize.get()) {
            ForgeRegistries.ITEMS.getValues().stream()
                .filter(Objects::nonNull)
                .forEach(item -> setPrivateValue(
                    Item.class, item,
                    getMaxStackSizeOrDefault(item),
                    "field_77777_bU"
                ));
        }
    }

    private int getMaxStackSizeOrDefault(Item item) {
        ResourceLocation resourceLocation = item.getRegistryName();
        Integer newMaxStackSize = resourceLocation == null || useDefaultMaxStackSize.get()
            ? defaultMaxStackSize.get()
            : resourceToMaxStackSize.get(resourceLocation.toString()).get();
        return Math.min(item.getMaxStackSize(), newMaxStackSize);
    }
}


 

public class Config {
    private static final ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
    public static final ForgeConfigSpec config;

    static {
        StackDownConfig.init(builder);
        config = builder.build();
    }

    public static void loadConfig(ForgeConfigSpec config, String path) {
        StackDown.LOGGER.info("Loading config: " + path);
        final CommentedFileConfig file = CommentedFileConfig.builder(new File(path))
            .sync()
            .autosave()
            .writingMode(WritingMode.REPLACE)
            .build();
        StackDown.LOGGER.info("Built config: " + path);
        file.load();
        StackDown.LOGGER.info("Loaded config: " + path);
        config.setConfig(file);
    }
}


 

public class StackDownConfig {

    public static ForgeConfigSpec.IntValue defaultMaxStackSize;
    public static ForgeConfigSpec.BooleanValue overrideMaxStackSize;
    public static ForgeConfigSpec.BooleanValue useDefaultMaxStackSize;
    public static Map<String, ForgeConfigSpec.IntValue> resourceToMaxStackSize;

    public static void init(ForgeConfigSpec.Builder builder) {
        builder.comment("StackDown config");
        overrideMaxStackSize = builder
            .comment("Decide if you want to override vanilla max stack size (applies to all)")
            .define("common.override_max_stack_size", true);
        useDefaultMaxStackSize = builder
            .comment("Decide if you want to use default max stack size (ignoring custom settings)")
            .define("common.use_default_max_stack_size", true);
        defaultMaxStackSize = builder
            .comment("Maximum default stack size")
            .defineInRange("common.default_max_stack_size", 16, 1, 64);

        StackDown.LOGGER.info("ForgeRegistries.ITEMS:");
        ForgeRegistries.ITEMS.getValues().forEach(item ->
            StackDown.LOGGER.info("  {}", item.getRegistryName()));

        resourceToMaxStackSize = ForgeRegistries.ITEMS.getValues().stream()
            .map(ForgeRegistryEntry::getRegistryName)
            .filter(Objects::nonNull)
            .map(ResourceLocation::toString)
            .collect(toMap(registryName -> registryName, registryName -> builder
                .comment("Maximum stack size for " + registryName)
                .defineInRange("custom." + registryName, 16, 1, 64)));
    }
}


Can you help me with some advice? Thanks in advance.

Edited by st4s1k
Link to comment
Share on other sites

54 minutes ago, diesieben07 said:

That is correct and intentional. How would there be items in your mod constructor?

If you need initialized items you need to use FMLCommonSetupEvent.

I actually had FMLCommonSetupEvent in overrideMaxStackSize(), but no difference. The config initializer is static and happens much earlier before I actually need it to be.

Edited by st4s1k
Link to comment
Share on other sites

Here is my updated code:

@Mod("stackdown")
public class StackDown {
    public static final Logger LOGGER = LogManager.getLogger();

    public StackDown() {
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::overrideMaxStackSize);
        MinecraftForge.EVENT_BUS.register(this);
    }

    private void overrideMaxStackSize(final FMLCommonSetupEvent event) {
        Config.init();

        StackDown.LOGGER.info("ForgeRegistries.ITEMS:");
        ForgeRegistries.ITEMS.getValues().forEach(item ->
            StackDown.LOGGER.info("  {}", item.getRegistryName()));

        if (overrideMaxStackSize.get()) {
            ForgeRegistries.ITEMS.getValues().stream()
                .filter(Objects::nonNull)
                .forEach(item -> setPrivateValue(
                    Item.class, item,
                    getMaxStackSizeOrDefault(item),
                    "field_77777_bU"
                ));
        }
    }

    private int getMaxStackSizeOrDefault(Item item) {
        ResourceLocation resourceLocation = item.getRegistryName();
        Integer newMaxStackSize = resourceLocation == null || useDefaultMaxStackSize.get()
            ? defaultMaxStackSize.get()
            : resourceToMaxStackSize.get(resourceLocation.toString()).get();
        return Math.min(item.getMaxStackSize(), newMaxStackSize);
    }
}


 

public class Config {
    private static final ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
    public static ForgeConfigSpec config;

    private Config() {
    }

    public static void init() {
        StackDownConfig.init(builder);
        config = builder.build();
        ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, config);
        Config.loadConfig(config, FMLPaths.CONFIGDIR.get().resolve("stack-down.toml").toString());

    }

    public static void loadConfig(ForgeConfigSpec config, String path) {
        StackDown.LOGGER.info("Loading config: " + path);
        final CommentedFileConfig file = CommentedFileConfig.builder(new File(path))
            .sync()
            .autosave()
            .writingMode(WritingMode.REPLACE)
            .build();
        StackDown.LOGGER.info("Built config: " + path);
        file.load();
        StackDown.LOGGER.info("Loaded config: " + path);
        config.setConfig(file);
    }
}


P.S. I didn't make any changes in StackDownConfig.class, so I didn't include it here.

Edited by st4s1k
Link to comment
Share on other sites

So, what I did is, I extracted this code into a separate static method Config.init(), which I can call whenever I need to, rather than a static block:

    static {
        StackDownConfig.init(builder);
        config = builder.build();
    }

 

And moved this lines inside the new Config.init() method:

ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, Config.config);
Config.loadConfig(Config.config, FMLPaths.CONFIGDIR.get().resolve("stack-down.toml").toString());


Then I just call Config.init() inside this method:

overrideMaxStackSize(final FMLCommonSetupEvent event)

 

Edited by st4s1k
Link to comment
Share on other sites

So it seems like it works! All items are loaded before loading config.
 

[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   minecraft:polished_blackstone_brick_stairs
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   minecraft:cracked_polished_blackstone_bricks
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   minecraft:respawn_anchor
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   toughtrees:flint_axe
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   toughtrees:flint_pickaxe
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:backpack
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:iron_backpack
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:gold_backpack
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:diamond_backpack
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:pickup_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_pickup_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:filter_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_filter_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:magnet_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_magnet_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:feeding_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:compacting_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_compacting_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:void_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_void_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:restock_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_restock_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:deposit_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:advanced_deposit_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:refill_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:inception_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:everlasting_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:smelting_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:auto_smelting_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:crafting_upgrade
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]:   sophisticatedbackpacks:upgrade_base
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]: Loading config: D:\Downloads\MC\server-forge\config\stack-down.toml
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]: Built config: D:\Downloads\MC\server-forge\config\stack-down.toml
[21:17:56] [modloading-worker-11/INFO] [co.st.st.StackDown/]: Loaded config: D:\Downloads\MC\server-forge\config\stack-down.toml

 

Edited by st4s1k
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.

Announcements



×
×
  • Create New...

Important Information

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