3. Creating a item

Now that we have a good base created in tutorial 2, it's time to actually start modding. In this chapter you will learn how to create items, how to translate them and how to add textures for your items!

Creating item for spawning NPCs

Creating items is pretty easy in Forge. First create a package called items in your followermod package. It is generally a good idea to separate different things into their own packages, like items, blocks and proxies / event handlers as seen in part 2 of this tutorial.

You will be creating a item for spawning NPC's. What would you name it? It is generally good thing to first append in the name what it is. So: Item. Now, what does the item do? It spawns NPC's. So let's call it ItemSpawnNPC. Notice also how I separated all different subjects with uppercase letter, that is called camelcase notation. You should use it.

Ok, create class ItemSpawnNPC inside your items package. Open the newly created file. Right after public class ItemSpawnNPC add extends Item. Once you are done writing Item, IDE will automatically suggest you what to import. Select net.minecraft.item from the list and hit enter. Your file should look like this:

package com.followermod.items;

import net.minecraft.item.Item;

public class ItemSpawnNPC extends Item

Great, now we just need to register that item in the game. To do that open your FollowerMod class. First add new variable in the class. That variable is called itemSpawnNPC and it is type of Item. To do that, add this line after your proxy variable: public static Item itemSpawnNPC;. That variable will hold the item that you'll create. Code should then look something like this:

@SidedProxy(clientSide = "com.followermod.proxy.ProxyClient", serverSide = "com.followermod.proxy.ProxyServer")
public static IProxy proxy;
public static Item itemSpawnNPC;

Our FollowerMod class contains the init method which we would presume that it can be used to inialize our item, but nope. We cannot use init method to add items in the game as we need to add textures for the items before the init method gets called. That's why we'll Forge's preInit method. Before the method init() add this method preInit():

    public void preInit(FMLPreInitializationEvent event)

And to actually register our item in the game, add these two lines inside the preInit method:

itemSpawnNPC = new Item().setUnlocalizedName("itemSpawnNPC").setCreativeTab(CreativeTabs.tabTools);
GameRegistry.registerItem(itemSpawnNPC, "itemSpawnNPC");

First line of the code will instantiate your itemSpawnNPC variable and call couple methods inside. First the method setUnlocalizedName will set unlocalized name for the item. After that the call to setCreativeTab will set in which tab you can find the item while browsing your creative inventory. Currently the item will be added visible in the "Tools" tab but you can add it to any tab you'd like to. Should you want to experiment with the tab, just remove the CreativeTabs.tabTools part and type: CreativeTabs.tab and you'll get a dropdown menu where you can choose the tab.

Second line of code will add the the item into your game's registry of items.

Are we there yet?

Now your item should be in the game and you can actually start your game and you can even view it in-game. To do that again, make sure the dropdown menu on topright corner of your IDE has "Minecraft Client" selected and press the button that looks like a bug next to the run button. Once your game has started, press "Singleplayer", start new creative world, open your creative inventory, press the "Tools" tab and you should see a weird looking pink item in the menu with name item.itemSpawnNPC.name. Item does not yet have a name and texture, so that's why it looks so weird. You should however be able to drag the item into your hotbar inventory and use the item in-game. Nice! Now time to add translated name for your item and texture.

Adding translated name for the item

This step is fairly simple. On the left of your IDE you'll see a folder resources. Inside that folder, you will need to create this kind of structure for Forge to detect your language file: resources.assets.modid.lang. Modid is the ID that you gave for your mod, our mod has that inside the FollowerMod class inside variable MODID. That is followermod in our case.

So to create that kind of structure, left click on Resources folder and create new package called assets. Inside the package assets create package followermod. Inside the package followermod create package lang. Yo dawg! Now you just need to add one more file where game gets your translations from, so create file called en_US.lang in your assets.followermod.lang package. Inside that package add translations for your item, so your en_US.lang file will look like this:

item.itemSpawnNPC.name=Followermod NPC Wand

item.itemSpawnNPC.name is actually your item's name that is in the FollowerMod class. Text after the equals sign is the translations for the item, you can tweak it around if you want to. Now start your client and verify that the item has translation.

Adding texture for the item

I have nothing against having weird looking pink item as the texture for the item, but we could make it look more meaning. To do that, first you'll need to create a 16x16px PNG image containing your item texture. Make sure the background color is transparent. You can use adobe photoshop and do it yourself, but in this tutorial we will be using premade icon from silk icon set. The texture can be downloaded from here.

Name of the file should be the same as unlocalized name for the item. In this case it is itemSpawnNPC.png. Now you will need to add that file in right folder for Forge to detect it. Package where the file goes into is resources.assets.followermod.textures.items. As you already have resources.assets.followermod.lang you'll have to toggle option in your IDE to add package into middle of that.

In the left panel, press the small settings cog icon on top and remove mark from the option "Compact Empty Middle Packages". Now all packages should come visible for you. Now just select the package assets.followermod and add new package inside that called textures. Inside that textures package add another package called items. Now drag & drop your itemSpawnNPC.png file from your computer into the textures.images package.

Now one more thing and we are all set! Open your FollowerMod class again and find the row where you create the itemSpawnNPC. Add one more method call there to initialize which texture you want the item to have. Line will then look like this:

itemSpawnNPC = new Item().setUnlocalizedName("itemSpawnNPC").setCreativeTab(CreativeTabs.tabTools).setTextureName(MODID + ":" + "itemSpawnNPC");

That tells for Forge what texture to use.

And now just launch the game and enjoy your fancy new item!

What's next?

In the next part we will be making so that by right clicking your game shows up a GUI where you will have one label and button to spawn a NPC in the world.

Download sources for this part

Click here to download sources.