Today we are going to make a custom item, I am going to show you how to register your items, register item models, and work with RegistryEvents.
First, we need to create a class where we are going to register all of our items. (Technically you don’t need this, but I find it easier to work with if all of my items are registered separately from other things like blocks)
- Create another package inside the mod package and call it init. In this package, we will put all of our item, block etc. initialization classes.
- Inside the init package create a class ModItems.
The item we will be creating will be the most basic item possible, it won’t have any special effects.
In the ModItems class, we will be instantiating and registering our objects.
Now we need to let forge know we want to register an item, this is done in a RegistryEvent.
Add a public static method called registerItems it has to have an argument of type RegistryEvent.Register<Item> I’ll refer to this argument as event but a better more descriptive name could be itemRegistryEvent. Above the method add an annotation @SubscribeEvent and above the class declaration add an annotation @Mod.EventBusSubscriber and set the modid argument to your modid and the bus value to Bus.MOD (In pre 1.13 versions you didnt need to set the bus, but this changed in 1.13).
- In the registerItems method, you can register the item by calling event.registerAll and passing in a new instance of the Item class.
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.
- Now you also need to set the registryname on for your item, you do this by calling setRegistryName on the item instance and passing in your modid for the first argument, and the item name as the second argument, the name has to be all lowercase and can only contain the following characters: a-z 0-9 / . _ –
- You may want to access the item instance later again, this is possible! Add the @ObjectHolder annotation above the class and pass in your modid. Now create a public static final Item variable, the name has to exactly match your item’s registry name (Because it is final you need to set it to null, but forge will set it to your item’s instance). It’s better to use ObjectHolder because people could possibly replace your item, and now you’ll automatically use that replacement.
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 new package in the src/main/resources folder called assets.modid and ofcourse replace modid with your modid. In this package, we have to create some more packages. Create: models.item and textures.item.
In the textures.item package 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 text file. In the text file add the following json code:
Remember to save it as NAME.json. You have to replace the words in capslock with the corresponding values, NAME 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 the assets.modid package create a new package called lang, in this package create a new text file. In this text file add the following line:
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!
It would probably be smart to add it to a creative tab, in the ModItems class’ init method you can change some simple things about the item. Because of the way minecraft is made you can chain method calls after eachother. Call the method group on the Item.Properties instance, I will put it in the miscellaneous creative tab. So I will pass the method ItemGroup.MISC. Just to show you how to chain methods I make it stack to 32. I will do this by calling maxStackSize. This is the line I have now:
new Item(new Item.Properties().maxStackSize(32).group(ItemGroup.MISC)).setRegistryName(Reference.MODID, "tutorial_dust")
Well, that was it for this part! In the next part, we will add a custom block. By clicking here you will be send to the code of this part.
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!