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 are going to create some packages and files.
- Create a package in your mod package and call it items. This package will contain all of the custom item classes.
- 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 items package create a class ItemBasic.
- Inside the init package create a class ModItems.
Your package structure should now look a little like this:
The ItemBasic class should extend Item (When importing make sure to import “net.minecraft.item.Item”). Now add a constructor with a String name. Inside of the constructor call the methods: “setUnlocalizedName and setRegistryName” and pass them the name variable. The setUnlocalizedName method will set the unlocalizedName of the item, this will add item. in front of the name (item.name). The setRegistryName will set the registryName of the item. This name is used internally and should never be changed after assigning the item.
This class will be the most basic item possible, it won’t have any special effects.
In the ModItems class we will be instantiating and registring our objects. In the ModItems class create a static variable of type Item, I will call it tutorial_item and a public static method called init. This method will be called in the preInit function of our Main class and initialize all of our items. It will return nothing and it doesn’t need any arguments.
In the init function initialize the tutorial_item variable. I will pass it the name tutorial_item.
Now we need to let forge know we have an item to register, this is done in the RegistryEvent. Add a public static method called registerItems it has to have an argument of type RegistryEvent.Register<Item>. Above the method add an annotation @SubscribeEvent and above the class declaration add an annotation @Mod.EventBusSubscriber and add an modid attribute.
In the registerItems method register your item by calling the register method like this:
In the Main class’ init method call the init method of the ModItems class.
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!
In the ModItems class we will use another event to register the model location of the item. This is done in the ModelRegistryEvent. Add a public static method called registerRenders with as argument ModelRegistryEvent. Don’t forget to add an @SubscribeEvent annotation above the method!
Add another method, this method is also static and takes an Item as argument, I will call it registerRender (without an “s”!). In this method add the following line:
ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation( item.getRegistryName(), "inventory"));
This line will set the location of the item model.
In the registerRenders method call the registerRender method and pass it your custom item.
Your item will now have a location registered, we haven’t added a model and texture yet so forge will change it with a custom model telling you the location.
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.items (make sure you don’t have a “s” behind item after models).
In the textures.items 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_item.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:
Save it under the same name as your item but make sure it is all lowercase or it won’t work, also remember to save it as NAME.json. You have to replace the words in capslock with the corresponding values. 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.lang.
One last thing before we can launch the game and admire our new item, in the src/main/resources folder create a new textfile, give it the following contents and save it as pack.mcmeta:
This will make sure forge uses version 3 of the pack versions. (This is recommended for 1.12)
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 forge is made you can chain method calls after eachother. Call the method setCreativeTab on the item, I will put it in the miscellaneous creative tab. So I will pass the method CreativeTabs.MISC. Just to show you how to chain methods I make it stack to 32. I will do this by calling setMaxStackSize. This is the line I have now:
tutorialItem = new ItemBasic("tutorial_item").setCreativeTab(CreativeTabs.MISC).setMaxStackSize(32);
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!