Jump to content

Registries that Require Registries


Syric

Recommended Posts

I've created a new registry for a class called 'Ingredient'. Each ingredient has several properties, including the item that represents them in the game. However, when I attempt to start the game, I get a crash caused by attempting to register an Ingredient associated with an item my mod adds. The crash report claims that the registry object for that added item is not present. I'm registering my modded items (i.e. calling the register() method) before I register my ingredients, but I suspect that the 'deferred' part of 'DeferredRegister' is interfering with the neat ordering of events.

How can I register objects which require other modded objects to be registered first?

Link to comment
Share on other sites

First, this method is called from the main mod class:

    public static void register() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

        AlchemineBlocks.register(modEventBus);
        AlchemineItems.register(modEventBus);
        AlchemineBlockEntityTypes.register(modEventBus);
!       AlchemicalIngredients.register(modEventBus);
        AlchemicalRecipes.register(modEventBus);
    }

Then the AlchemicalIngredients class:

public class AlchemicalIngredients {

    private static boolean isInitialized = false;

    public static final DeferredRegister<Ingredient> INGREDIENTS = DeferredRegister.create(new ResourceLocation(Alchemine.MODID, "ingredients"), Alchemine.MODID);

    public static final HashMap<Item, Ingredient> INGREDIENTS_MAP = new HashMap<>();

    public static final RegistryObject<Ingredient> PHOSPHORUS = registerIngredient("phosphorus",
!            () -> new Ingredient(AlchemineItems.PHOSPHORUS.get(), 2.9D, 0.3D)
                    .addAspect(Aspect.LIGHT, 2).addAspect(Aspect.STICKY, 1));
    

    public static void register(final IEventBus modEventBus) {
        if (isInitialized) {
            throw new IllegalStateException("Ingredients already initialized");
        }
        INGREDIENTS.makeRegistry(RegistryBuilder::new);
        INGREDIENTS.register(modEventBus);
        isInitialized = true;
        LogUtils.getLogger().info("Ingredients initialized");
    }

    private static <T extends Ingredient> RegistryObject<T> registerIngredient(String name, Supplier<T> ingredient) {
        RegistryObject<T> ret = INGREDIENTS.register(name, ingredient);
        INGREDIENTS_MAP.put(ingredient.get().getItem(), ingredient.get());
        return ret;
    }
}

Here, I'm trying to register a 'phosphorus' ingredient to the Ingredients registry, linking it to the phosphorus item I added in AlchemineItems.

The issue I get is an InvocationTargetException that says "Exception message: java.lang.NullPointerException: Registry Object not present: alchemine:phosphorus". It targets the line marked with an exclamation point in AlchemicalIngredients.

The full crash log is here.

I added the actual phosphorus item some time back; I can confirm that it is present in-game and has not posed any issues on its own. I don't think it's the issue, but I could always post my item-registration code if necessary. Phosphorus is just a new Item with no unique functionality.

Link to comment
Share on other sites

15 hours ago, diesieben07 said:

This is your problem. This happens in static init, where items are not yet registered.

Thank you! Removing that line does indeed fix the problem.

I then wrote a function that adds the contents of the registry to the map, and called it during commonSetup. Everything seems to be working.

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.