Adding Items

Adding items is fairly easy with Forge. In this tutorial I will show you how to add items into your mod. My tutorial might be different with others, but my tutorials are made to be the most organized.

Instead of putting everything in your main mod class, we will create a new class solely for the items. And then, add an init method into it, like so:

package com.emx.tutorial.init;

public class ModItems
{
    public static void init()
    {

    }
}

As you can see, I put it in the init method. The key is, I am trying to make the mod as similar as the Minecraft code in terms of organization. This will make your mod easier to read for yourself and other modders, if you are planning to make it open source.

To add a basic item, you don’t need a separate class. You only want a separate class if you have custom functions to the item. But for now, we won’t do that. In this tutorial I will add a red diamond item.

package com.emx.tutorial.init;

public class ModItems
{
    public static Item red_diamond;

    public static void init()
    {
        red_diamond = new Item().setUnlocalizedName("red_diamond").setTextureName(TutorialMod.MODID + ":red_diamond").setCreativeTab(CreativeTabs.tabMaterials);
    }
}

There are a few basic methods needed to make the item:

  • .setUnlocalizedName() :: This sets the mod-unique ID of the item. No two items can have the same unlocalized name.
  • .setTextureName() :: This sets the image texture of your mod. In this case, I used TutorialMod.MODID instead of rewriting the mod ID. But the real value is “tutorial:red_diamond”, and it points to the file “red_diamond.png” inside package “assets.tutorial.textures.items”, which we will make in a minute.
  • .setCreativeTab :: This sets which tab your item will be in in the creative menu. This is not needed if you don’t want your item to be obtainable in the creative menu.

Like I said before, let’s add the actual texture into the project. For this tutorial, I will use this texture:

 

Create a new package in the “src/resources” source folder. You’ll need to name it “assets.modid.textures.items”, so in our case it will be “assets.tutorial.textures.items”.In it, drag and drop the image from your file explorer, and make sure it is named “red_diamond.png”, just like how we coded it.

Alright, now we have to register the item so that Minecraft knows we added an item to it. Add this line to your init() method:

GameRegistry.registerItem(red_diamond, red_diamond.getUnlocalizedName());

So, your final code will be like this:

package com.emx.tutorial.init;

public class ModItems
{
    public static Item red_diamond;

    public static void init()
    {
        red_diamond = new Item().setUnlocalizedName("red_diamond").setTextureName(TutorialMod.MODID + ":red_diamond").setCreativeTab(CreativeTabs.tabMaterials);

        GameRegistry.registerItem(red_diamond, red_diamond.getUnlocalizedName());
    }
}

Alright, we are done. Now, we need our main mod class to call this method, so what we coded will be executed. Go to the main mod class, and enter this line in the preInit method:

ModItems.init();

So, your main mod class will now look like this:

package com.emx.tutorial;

@Mod(modid = TutorialMod.MODID, name = TutorialMod.NAME, version = TutorialMod.VERSION)

public class TutorialMod
{
    public static final String NAME = "Tutorial Mod";
    public static final String MODID = "tutorial";
    public static final String VERSION = "1.7.10-R1";

    @Mod.Instance("tutorial")
    public static TutorialMod instance;

    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        ModItems.init();
    }

    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {}

    @Mod.EventHandler
    public void postInit(FMLPostInitializationEvent event) {}
}

Alright! Seems finished, but if you try and run Minecraft, the item will show up as “item.red_diamond.name” instead of Red Diamond, that’s because we haven’t added a language for it.

If you haven’t already, you need to make a language file. To add a language file, create a new package called “assets.tutorial.lang” in the resources folder, and create a new file called “en_US.lang”. In there type in the following:

item.red_diamond.name=Red Diamond

That’s it for this tutorial! In the next tutorial I will teach you how to add blocks.

24 thoughts on “Adding Items

  1. True May 25, 2015 / 12:10 AM

    When I add the init method to ModItems.java, it gives me an error because it doesn’t match the existing package.

    Like

    • Emx2000 May 25, 2015 / 5:12 PM

      Are you sure your code is the same as mine? You might want to share your code with pastebin.com

      Like

  2. Tstomix July 9, 2015 / 9:21 PM

    .setTextureName doesn’t work in 1.8 ;( New model systems! GG.

    Like

    • Emx2000 July 9, 2015 / 11:23 PM

      Of course it doesn’t! The model system practically removes all textures within the code, so everything is set in the models.

      Like

  3. Night July 15, 2015 / 7:42 PM

    Eclipse gives me errors if I don’t explicitly state to import things like Mod from forge or Item from minecraft. After I import those things it runs fine and shows the item, but otherwise the item is missing. Was I supposed to do this all along or did I just set Eclipse up incorrectly?

    Like

    • Emx2000 July 17, 2015 / 1:02 AM

      That is how Eclipse works, and virtually any IDE in existence. But with Eclipse, you can type what you want halfway, and proceed to press Ctrl+Space on your keyboard, that will use the closest object to what you’re currently typing, and also import it, if not yet. Another way to do it easily is when you’re done with a class, simply press Ctrl+Shift+O to import everything you haven’t imported.

      Like

  4. creeperhostyoutube September 14, 2015 / 1:48 AM

    Im at this site because i cant get me texture to work (my json code
    {
    “parent”: “builtin/generated”,
    “textures”: {
    “layer1”: “tm:items/test_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 ]
    }
    }
    }

    )

    Like

    • Emx2000 January 10, 2016 / 5:43 PM

      Sorry, you’ll have to look somewhere else because this is a 1.7.10 tutorial, and those are JSON models, which is a 1.8 feature.

      Like

  5. Blue Ice November 30, 2015 / 12:42 PM

    this keeps happening and i dont know why:

    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: The following texture errors were found.
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: ==================================================
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: DOMAIN mytestmod
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: ————————————————–
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: domain mytestmod is missing 1 texture
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: domain mytestmod is missing a resource manager – it is probably a side-effect of automatic texture processing
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: ————————-
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: The missing resources for domain mytestmod are:
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: textures/items/red_diamond.png
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: ————————-
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: No other errors exist for domain mytestmod
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: ==================================================
    [21:37:28] [Client thread/ERROR] [TEXTURE ERRORS]: +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

    When I load the game, the item is there, but it is a missing texture

    Like

    • Emx2000 January 10, 2016 / 5:38 PM

      Then you have a broken texture code. Show me, via pastebin.com.

      Like

  6. Anonymous January 30, 2016 / 5:10 AM

    item.red_diamond.name=Red Diamond

    is it supposed to be as it is (without quotes)?

    Like

  7. KoolDude214 February 1, 2016 / 12:49 AM

    So are all these packages in src/java or src/resoruces or just “in the project”

    Like

    • KoolDude214 February 1, 2016 / 3:27 AM

      Also, could you add how to do textures in 1.8 pls

      Like

    • Emx2000 February 1, 2016 / 4:50 PM

      Everything that has code is supposed to be in the src/main/java

      Like

  8. Alex February 23, 2016 / 5:49 AM

    Did you ever finish those tutorials on 1.8 textures?

    Like

      • Alex February 23, 2016 / 10:58 PM

        Yeah

        Like

      • Emx2000 February 23, 2016 / 10:59 PM

        okay, noted. lets hope i have the will to do that 😊

        Like

  9. ANONYMOUS March 26, 2016 / 8:26 PM

    Your code is wrong at “GameRegistry.registerItem(red_diamond, red_diamond.getUnlocalizedName();”.
    You need to edit the text to “GameRegistry.registerItem(red_diamond, red_diamond.getUnlocalizedName());”
    It’s not a big thing, but small things can ruin the code.
    Not that anyone would get stuck (for a long time), but change it.

    Like

    • Emx2000 March 26, 2016 / 8:55 PM

      Thanks for pointing it out! Will change when I get to my PC.

      EDIT: Fixed!

      Like

Leave a comment