Tutorials: Items

Items are entities within Minecraft that only exist in the player's inventory and in their hand. They cannot be placed, but can create blocks when used. With the release of 1.8, you now need to use .json files to define the models and assign the textures for every item you add.

Overview
Basically what we are going to do to make an item is:
 * 1) Register the model with the game using the GameRegistry class. (The model is defined by the shape of the texture.)
 * 2) Create a .json file to bind the model with the texture.

Registering the Item
First, make a new package called init in you com.name.mod package and put a class called ModItems inside it. Within this class, put an init method: public static void init { } This is the method that we will initialize the item's variable we will create now. To do this, you'll want to create an item object that will actually create the instance of the item that we can edit in the code. Think of it as the raw item that properties such as material, lighting, etc. can be added to. In the ModItems class, create an Item object and call it whatever you want. Make sure to import it and select the net.minecraft.item.Item option if it gives you a choice. public static Item test_item; We will use this uninitialized variable in the init method to give it an UnlocalizedName. This is the name that the code uses to reference the item. This is not the name that the item will have in-game. Enter this line of code within the register method: public static void init { test_item = new Item.setUnlocalizedName("test_item"); } Now we are going to create the register method, which is the method used to give the item a place in the GameRegistry and allow us to see it in-game, as well as let us give it properties. Below the init method somewhere, put this method into the code and register the item with an UnlocalizedName: public static void register { GameRegistry.registerItem(test_item, test_item.getUnlocalizedName.substring(5)); } Immediate after this, create a registerRenders method, which will allow us to see the item, whether it has a texture or not, in the game. public static void registerRenders { } After that, make a registerRender method (without the 's') and put the following line of code in it:

public static void registerRender(Item item) { Minecraft.getMinecraft.getRenderItem.getItemModelMesher.register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName.substring(5),"inventory")); Minecraft:agent Make sure to take out the indents if you copy/past this and import all the unimported items.

Next, inside the registerRenders method, put the final register line: registerRender(test_item); Lastly, go to the ClientProxy class and add the following line in the registerRenders method: ModItems.registerRenders;

Initializing the Item
Now that we have the item registered and ready to render, we need to tell the mod to do so when the game starts. To do this, go into the ModClass and call the register and init methods inside the preInit method: ModItems.init; ModItems.register; Now we need to tell the mod to render the items on a server, so go into the init method in the ModClass and put a render line for proxies: proxy.registerRenders; Since the items are rendered automatically through the registerRender method in the items class, we need a command that will tell the mod to also render the items to render on a server, hence the proxy.registerRenders line.

Testing the Items
So now if you run the game using the ? you have installed, so any worlds here will not show up on your version of Minecraft installed on your computer.

Once you are in the world, press the command key and enter the command: /give  : Since this is a testing instance of Minecraft, your player is given a random number to use commands. You can get your player number by pressing TAB after the space after 'give', and be sure to replace the modID and item name with your modID and item name.

This command should give you an untextured block. This is also where the .json files come in. They are the files that determine the shape of the item's model based on it's texture. So if your texture was a circle, it would be a circle with one unit of depth added to it. If it was a rectangle, it would be an rectangle, and so on.

Making a texture
You'll need to create a texture for your item. Make sure the dimensions are 16 pixels by 16 pixels otherwise the texture will clip off of the model. Now we need to create a new directory in which to save textures for both blocks and items. Go into the following directory: /src/main/resources/ Create a new folder and call it  'assets'. Then in that folder create a folder with the name of your modID. Within that make two folders: one called  'textures' and one called  'models'. Inside of the textures folder make a folder called  'items'. The items folder is where you will save all the textures for your items. The final directory for item textures should be: /src/main/resources/assets//textures/items/ Now save the file as a .png file with the same name as your Item variable name. So for me it would be 'test_item'.

Binding the texture
Now go back to Eclipse and right-click on the entire project and go down to 'Refresh'. Now if you open the src/main/resources package, you should be able to trace the package down to the image you saved. It's time to create the .json file for the model of the item. Go to assets.tm package and go to the models section. Right click on the package and create a new folder. Call it items, this is where we will put the .json file for the item. Right-click on the items folder and make a new Untitled Text File.

Inside this new text file we are going to put our rendering code. To do this we are going to copy some of the default code from another model. Go to the Referenced Libraries archive in the Package Explorer and go to the very first library called forgeSrc and go to assets/minecraft/models/items and select a simple item from the game. I am going to be using the apple item data for this example. So when you've opened one you like, copy all of the text therein into your untitled file. The unedited file should look something like this if you chose the same item I did: {   "parent": "builtin/generated", "textures": { "layer0": "items/apple" },   "display": { "thirdperson": { "rotation": [ -90, 0, 0 ], "translation": [ 0, 1, -3 ], "scale": [ 0.55, 0.55, 0.55 ] },       "firstperson": { "rotation": [ 0, -135, 25 ], "translation": [ 0, 4, 2 ], "scale": [ 1.7, 1.7, 1.7 ] }   } }

If you are having trouble opening the file, go to Window > Preferences and then within the file tree, go to General > Editors > File Association and use the 'Add' buttons to add the '.json' extension and associate it with the default text editor. 

Now you need to slightly modify the file to your item. Paste the data into your file and change the line where it says  '"layer0": "items"'  and change it to  '"layer0": ":items/ "'  so the final product should be: {   "parent": "builtin/generated", "textures": { "layer0": "id:items/test_item" }, You can now press Ctrl S and save it in the items directory you made earlier by going to Minecraft > src > main > resources > assets >  > models > items and save it with your item name with .json after it. Mine would be test_item.json. Now if you look in the folder it should have your new json file in it.

There you go! You've created your very first item. You can now start the game and give yourself the item via the command mentioned earlier. You can create as many items as you want by repeating these steps and I will be giving later tutorials on how to give items properties and make them craftable. Happy modding!