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:
- A lang file entry,
src/main/resources/assets/examplemod/lang/en_us.json:{ "item.examplemod.ruby": "Ruby" } - An item model,
src/main/resources/assets/examplemod/models/item/ruby.json, pointing at a texture inassets/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.