Today I am going to show how to make a custom item, using the DeferredRegister.
The recommended way to register objects like items is through the DeferredRegister system. This way you will not use any annotation magic (@SubscribeEvent, @ObjectHolder) and your mod will automatically work with registry replacements.
You can choose where to put this, but I’ll create a new class for it called ModItems.
In here add a public static final DeferredRegister<Item> field called ITEMS and create a new instance of the DeferredRegister class. The DeferredRegister‘s constructor takes the registry you want to register to, which will be ForgeRegistries.ITEMS in this case, and your modid.
public static final DeferredRegister<Item> ITEMS = new DeferredRegister<>(ForgeRegistries.ITEMS, TutorialMod.MOD_ID);
Now to register the item itself add another field, this field will also be public static and final but will be of type RegistryObject<Item>. You can name it whatever you want but I recommend naming it after your item’s registry name so it is clear what it is.
Now you can register your item by calling ITEMS#register(String registryName, Supplier<? extends Item> supplier).
The first argument is simple, just your item’s registryName. The registryName has to be all lowercase and can only contain the following characters: a-z 0-9 / . _ –
The second argument is slightly more complicated. It takes a supplier (for technical reasons related to loading), this can be done in multiple ways but the easiest is to use a lambda. For example:
() -> new Item(..)
As you can see you need to create an item in the lambda/supplier. To create an instance of the Item class you need to pass in a Item.Properties instance. You can call methods on this instance to set things like the items creative tab, stacksize and maxDamage.
public static final RegistryObject<Item> TUTORIAL_DUST = ITEMS.register("tutorial_dust", () -> new Item(new Item.Properties()));
You may want to access the item instance later again, this is possible! Just reference the field you are creating and call “.get()”.
Now as a last step you need to register your DeferredRegister to forge.
In your main mod’s constructor you can register it by calling ModItems.ITEMS.register() and passing it FMLJavaModLoadingContext.get().getModEventBus() as argument:
This is technically enough for forge to load the item. When you start the game, and load a world you can give yourself the item with the command: “/give @p modid:itemname”. When you do this you will see that it doesn’t have a model and texture yet. So lets add those right now!
To add a model and texture create a some new folders in src/main/resources called assets/modid. In this folder create models/item and textures/item.
In textures/item you can add a texture (make sure its size is a power of 2, 16×16, 32×32, etc.). It doesn’t matter what you call it but it has to be lowercase and you have to remember the name. I will call it tutorial_dust.png and I will use this texture:
In the models/item package create a new json file. In the json file add the following json code:
Remember to save it as REGISTRYNAME.json. You have to replace the words in capslock with the corresponding values, REGISTRYNAME should be the same as the item’s registry name. The texture name shouldn’t contain the extension (.png).
You may have noticed that the name isn’t displaying correct. That is because we haven’t added a lang file yet, so lets do that. In assets/modid create a new folder called lang, in this folder create a new json file. In this json file add the following json:
And again, replace the words in caps with your own values. Save this file in the lang package and call it en_us.json.
Now you can start the game and you should have an item with texture and name!
This would be an example on how to make it stack to max 32 and add it to a creative tab (ItemGroup):
new Item(new Item.Properties().maxStackSize(32).group(ItemGroup.MISC))
Well, that was it for this part! In the next part, we will add a custom block. By clicking here you will be sent to the code of this tutorial.
As always if you have any question don’t hesitate to ask in the comments! Also if you saw any errors in the tutorial please let me know so I can fix it!