Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

  • Author

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.

  • Author
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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.