Basic Modding - First Item

Goal

I want to show you how to create an Item and give it a texture.

Difficulty

2/10 - Quite simple

Prerequisites

Forge Version

This Tutorial was created with Forge 10.13.0.1180 for Minecraft 1.7.10. If anything doesn't work with 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.item. In this tutorial I will use the package com.bedrockminer.tutorial.item.

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

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

Access modifiers (public etc.), final modifier and static modifier.

 

The class should now look like this:

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

public final class ModItems {

    public static final void init() {

    }

}

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

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

public final class ModItems {

    public static Item tutorialItem;

    public static final void init() {
    }
}

If we have done this we first 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.

 

We now have to create a new instance of the class Item and assign it to the object. This is done like this:

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

public final class ModItems {

    public static Item tutorialItem;

    public static final void init() {
        tutorialItem = new Item();
    }
}

This item actually has no specific properties because they have to be assigned manually.

First we should add a name to your item.

.setUnlocalizedName("tutorialItem");

Then we can place the item in a tab in the creative inventory.

.setCreativeTab(CreativeTabs.tabMisc);

 

These methods are created in a way that we can place them after each other.

Code:
tutorialItem = new Item().setUnlocalizedName("tutorialItem").setCreativeTab(CreativeTabs.tabMisc);

Registration and Texture

Your item is now correctly created but it actually isn't added to Minecraft. To change this we have to register  your item with the following method:

The first argument is your item and the second one an internal name for it. This should be the same name as the unlocalizedName we set before.

 

Your full code should now look like this:

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

public final class ModItems {

    public static Item tutorialItem;

    public static final void init() {
        tutorialItem = new Item().setUnlocalizedName("tutorialItem").setCreativeTab(CreativeTabs.tabMisc);
        GameRegistry.registerItem(tutorialItem, "tutorialItem");
    }

}

Finally we have to call the init() method of the ModItems class from the preInit method in your CommonProxy class, because the initialization should be done on both client and server side.

CommonProxy.java:
package com.bedrockminer.tutorial;

import com.bedrockminer.tutorial.item.ModItems;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

public class CommonProxy {

    public void preInit(FMLPreInitializationEvent e) {
        ModItems.init();
    }

    public void init(FMLInitializationEvent e) {

    }

    public void postInit(FMLPostInitializationEvent e) {

    }

}

If we now start minecraft and open the "miscellaneous" creative tab, we will see a new item which is named "item.tutorialItem.name" and has a pink-black texture. This is the MC Forge texture placeholder. Actually we have assigned no texture to the item, so it has got this one.

The name is the unlocalizedName with a pre- and a suffix. This is used to see the difference between items and blocks if we just look at the name.

In this tutorial we only add a texture, the language localization is part of another tutorial.

 

This will be the texture of the tutorialItem:

This texture has to be placed in this directory:

WorkspaceRoot/src/resources/assets/modid/textures/items/tutorialItem.png.

 

To tell Eclipse that something has changed in the file system, we select your project and press F5 or right-click > refresh.

Now there should be a white coloured package with a small folder symbol in the source folder "src/resources" named assets.modid.textures.items.

If we open this package we should see the texture file.

Picture - Show

To assign this texture to your item we have to add this method after setCreativeTab():

Code:
.setTextureName(Main.MODID + ":tutorialItem");

This sets the textureName of the item to a value which represents your texture file.

The modid in front of the colon is the name of the folder inside the assets folder and the text after the colon is the name of your texture.

Your ModItems class should now look like this:

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

public final class ModItems {

    public static Item tutorialItem;

    public static final void init() {
        tutorialItem = new Item().setUnlocalizedName("tutorialItem").setCreativeTab(CreativeTabs.tabMisc).setTextureName(Main.MODID + ":tutorialItem");
        GameRegistry.registerItem(tutorialItem, "tutorialItem");
    }

}

If we now start Minecraft we should have a textured item.

Picture - Show

You can download the code used in this tutorial as a .zip file 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.