Modding Minecraft with Forge

Lesson 3 of 6

Registering a Custom Item

Forge mods don't create game objects directly, everything (items, blocks, sounds, and more) goes through a registry, so the game knows about it consistently across saves, mods, and multiplayer.

DeferredRegister

DeferredRegister is the standard, modern pattern for registering content:

package com.example.examplemod;

import net.minecraft.world.item.Item;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public class ModItems {
    public static final DeferredRegister<Item> ITEMS =
        DeferredRegister.create(ForgeRegistries.ITEMS, "examplemod");

    public static final RegistryObject<Item> RUBY =
        ITEMS.register("ruby", () -> new Item(new Item.Properties()));
}

Then hook ModItems.ITEMS into the mod event bus from the main class constructor:

ModItems.ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());

RegistryObject<Item> is a lazy reference, it isn't a real, usable Item until registration actually completes, call .get() on it later once the game has finished loading (never at class-load time).

Making it show up in-game

A registered item exists, but without two more pieces it's invisible and unnamed in the inventory:

  1. A lang file entry, src/main/resources/assets/examplemod/lang/en_us.json:
    { "item.examplemod.ruby": "Ruby" }
  2. An item model, src/main/resources/assets/examplemod/models/item/ruby.json, pointing at a texture in assets/examplemod/textures/item/ruby.png.

Creative tab

To make the item easy to find in creative mode, add it to a CreativeModeTab via a BuildCreativeModeTabContentsEvent listener, or simply add it to an existing vanilla tab for now, that's a detail you can refine once the basics work.

📝 Registering Items Quiz

Passing score: 70%
  1. 1.What's the standard modern pattern for registering a new item in Forge?

  2. 2.A RegistryObject is a ____ reference, it isn't a usable item until registration completes.

  3. 3.An item's display name in the inventory comes from a lang file, not from its registry name.