Basic Modding - First Item

Goal

I want to show you how to create an Item with a simple model file in Minecraft 1.8.

Difficulty

2/10 - Quite simple

Prerequisites

Forge Version

This Tutorial was created using Forge version 11.14.0.1299 for Minecraft 1.8. If anything does not work for other versions, please contact me!


Creating an Item

To create Items we first want to have a special class to access them easily. We create a new class named "ModItems" in a new package named com.yourname.yourmodname.items. In this tutorial I will use the package com.bedrockminer.tutorial.items.

We make the ModItems class final so that it can't be subclassed. Then we create a method called "createItems()" and make it public and static.

Public makes the method accessible from everywhere and static ensures that we can access it even without an instance of the class. If you want to know more about these Java keywords please check these tutorials:

Access modifiers (public etc.) and static modifier.

 

The class should now look like this:

ModItems.java:
package com.bedrockminer.tutorial.items;

public final class ModItems {

    public static void createItems() {
    }
}

Now we can create the object for our item. To do this, we just write this code in your class:

ModItems.java:
public final class ModItems {

    public static Item tutorialItem;

    public static void createItems() {
    }
}

Once we have done this, we have to import the class Item. Press Ctrl+Shift+O and select net.minecraft.item.Item.

Now we have an object of the class Item which represents the item we are creating.

 

The method createItems has to be called inside the preInit method of the CommonProxy, because the Items need to be registered both on server and on client side.

CommonProxy.java:
public void preInit(FMLPreInitializationEvent e) {
    ModItems.createItems();
}

We now have to create another class, this time to define the item itself. We'll create a class that can be used for several items with the same specifications. The class will be called BasicItem and is located in the items package as well:

BasicItem.java:
package com.bedrockminer.tutorial.items;

public class BasicItem {
}

This class should extend the Item class. Also, we'll need a new constructor for our class to make the creation of Items easier:

BasicItem.java:
package com.bedrockminer.tutorial.items;
import net.minecraft.item.Item;

public class BasicItem extends Item {

    public BasicItem() {
        super();
    }
}

The constructor should take the name of the item as an argument. If we don't do this here, we would have to add .setUnlocalizedName() after each item that was created with this class. Also, the constructor can be used to assign the item to a creative tab directly. If we don't add this here, we would have to append the method .setCreativeTab() to every created Item.

BasicItem.java:
public BasicItem(String unlocalizedName) {
    super();

    this.setUnlocalizedName(unlocalizedName);
    this.setCreativeTab(CreativeTabs.tabMaterials);
}

Our Item class is already finished. We can now create our first Item.

To do this, we switch back into the ModItems class and add this line to the createItems method.

ModItems.java:
GameRegistry.registerItem(tutorialItem = new BasicItem("tutorial_item"), "tutorial_item");

This line does several different things at once.

First, a new instance of our BasicItem class is created using the constructor with the unlocalizedName as an argument (Shown in red above). Our item receives the name "tutorial_item". This new instance is assigned to our object tutorialItem.

Next, the registerItem method from the GameRegistry is called to register the Item with the game (Showed in green above). During registration, the Item gets a second name, the registration name (Showed in blue above). This should be the same as the unlocalized name of the item.


Of course, you can create more than one Item with this class. The class just defines the type of item and its general behaviour.

 

Once our Item is registered, it is available ingame. It can be acquired from the creative tabs (if .setCreativeTab has been called at least once) or with the /give command. To get the item with the /give command, you need to use the following command:

/give @p modid:registration_name

In our mod, this would be:

/give @p tutorial:tutorial_item

Registering a model file

If we try to give ourselves the Item, we see that it has no texture attached to it. Instead, it is rendered as a purple-black cube.

To change this, we need to add a model file for the item. Before we do this, we need to register the model. Therefore, it's useful to create a special class. The one I created is called ItemRenderRegister and is located in com.bedrockminer.tutorial.client.render.items. This class is final and contains a single static method named registerItemRenderer().

ItemRenderRegister.java:
package com.bedrockminer.tutorial.client.render.items;

public final class ItemRenderRegister {

    public static void registerItemRenderer() {
    }
}

The registerItemRenderer method is only used on client side, of course, thus we call it from the Client Proxy. It needs to be called during the init phase.

ClientProxy.java:
@Override
public void init(FMLInitializationEvent e) {
    super.init(e);

    ItemRenderRegister.registerItemRenderer();
}

If we want to register a model file for an Item, we need to run this line of code in the ItemRenderRegister:

ItemRenderRegister.java:
Minecraft.getMinecraft().getRenderItem().getItemModelMesher()
    .register(item, meta, new ModelResourceLocation("modid:itemname", "inventory"));

The first line above is used to get Minecraft's ItemModelMesher. This object is used to handle the different model files.

The register method takes several parameters: First, the item we want to register. Then the metadata for which this registration should be valid. By default, this is 0. Finally, we need a ModelResourceLocation to define the location of the model file. The constructor of the ModelResourceLocation class itself takes two arguments: First, the identifier for the item, which is the modid, a colon and the unlocalized name. Second, the String "inventory".

 

For our tutorial Item this line would look like this:

ItemRenderRegister.java:
Minecraft.getMinecraft().getRenderItem().getItemModelMesher()
    .register(ModItems.tutorialItem, 0, new ModelResourceLocation("tutorial:tutorial_item", "inventory"));

If we have multiple Items its useful to create a method for registering the model file. This saves a lot of code because the long method chain above is reduced to a single method call.

The method looks like this:

ItemRenderRegister.java:
public static String modid = Main.MODID;

public static void reg(Item item) {
    Minecraft.getMinecraft().getRenderItem().getItemModelMesher()
    .register(item, 0, new ModelResourceLocation(modid + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}

This method runs the same code as above, with the only difference that the identifier is created using a static variable for the modid and the unlocalized name of the Item. For this one we need to call .substring(5) because the method getUnlocalizedName() returns "item.unlocalizedName". The .substring(5) method cuts of the first five characters and leaves us with the unlocalized name we need. With this method, the registration process is as easy as calling the method:

ItemRenderRegister.java:
public static String modid = Main.MODID;

public static void registerItemRenderer() {
    reg(ModItems.tutorialItem);
}

public static void reg(Item item) {
    Minecraft.getMinecraft().getRenderItem().getItemModelMesher()
    .register(item, 0, new ModelResourceLocation(modid + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}

The model file

The model file itself looks just like the models from Minecraft.

You can find information about the structure of the item model files on the minecraft wiki.

 

For the tutorial Item I'll use the "default" model data that every normal Item is using. The file I want to create is called tutorial_item.json, like the name we registered the item with. It will be located in the "src/resources" source folder in the package assets.<modid>.models.item.

tutorial_item.json:
{
    "parent":"builtin/generated",
    "textures": {
        "layer0":"tutorial:items/tutorial_item"
    },
    "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 ]
        }
    }
}

This file defines the complete layout for our item.

The first line is called parent. This is the model this one inherits from. For the standard Item look, this is "builtin/generated". You can also specify another model.

The second line is the most important one for items: The assignment of the textures. A normal item has one single layer, so the texture that has to be assigned is named "layer0". Later, we will create Items with multiple layers.

The third line is the definition how the Item should be displayed. It contains assignments for the different display possibilities of an item. The possibilities are "thirdperson", "firstperson", "gui" or "head". Every possibility can contain data for rotation, translation and scale. The different transformations are applied in this order, but can be written in a different one. The data for each of the transformation possibilities is an array of three double values, defining the exact transformation.

 

A useful tool to experiment with the different transformations is the Item transform helper mod. It can be found at planetminecraft.com.

 

The file above is a simple copy from a normal Minecraft item (in this case redstone). I only edited the texture.

If you want to save some code, you can create a "standard_item.json" file, which contains the basic display data. Every normal item model can inherit from this one. The file looks like this:

standard_item.json:
{
    "parent":"builtin/generated",
    "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 ]
        }
    }
}

This file is located in assets.<modid>.models.item of course. The reduced tutorial_item.json file which refers to this file looks like this:

tutorial_item.json:
{
    "parent":"tutorial:item/standard_item",
    "textures": {
        "layer0":"tutorial:items/tutorial_item"
    }
}

We still need the texture the model file is pointing to. According to the value of "layer0" this file has to be in the package assets.tutorial.textures.items and has to be named tutorial_item.png. The texture I'll be using is this one:

If we place it in the corresponding package, refresh the workspace and run Minecraft, we should have a textured Item.

If you are annoyed by the item's name (item.tutorial_item.name) you should check out the tutorial about language files (link below).

Picture - Show

You can download the code used in this tutorial as a .zip file from here.


Recommended tutorials to continue with


Comments and Questions:

If you want to report modding problems, please make sure to include the code in a pastebin link or something else! Don't just write "It doesn't work", otherwise your post will be deleted. For more complicated problems, please use the troubleshooter form.