Wuppy’s Minecraft Forge Modding Tutorials for 1.6.2: Recipes

In this tutorial I’m going to show you how to add custom recipes to your mod. All of the code for this tutorial is done in the mod file. This is the file I will start with.


package tutorial;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = “YourName_ModName”;
      
       public static Block tutorialBlock;
      
       public static Item tutorialItem;
      
       @EventHandler
       public void load(FMLInitializationEvent event)
       {
             tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
            
             GameRegistry.registerBlock(tutorialBlockmodid + (tutorialBlock.getUnlocalizedName().substring(5)));
            
             LanguageRegistry.addName(tutorialBlock“Tutorial Block”);
            
             tutorialItem = new ItemTutorial(5000).setUnlocalizedName(“tutorial”);
            
             LanguageRegistry.addName(tutorialItem“Tutorial Item”);
       }
}

There are 3 types of Recipes in Minecraft. Number one is a Shapeless Recipe, used to craft for example bonemeal from bones. The second is a Shaped or normal Recipe. Those are used to craft things like pickaxes etc. The last type of recipe is a smelting recipe.
The first Recipe I will show you is a Shapeless Recipe. This is the code used for one.


GameRegistry.addShapelessRecipe(new ItemStack(tutorialBlock), new Object[]{

                    new ItemStack(Item.diamond), new ItemStack(Block.stone), new ItemStack(Item.dyePowder, 1, 15)
             });

This code goes into your load method. It looks pretty complicated, but it is actually pretty easy.
addShapelessRecipes is a method inside of GameRegistry. It takes 2 parameters. Number 1 is the returned Item or Block from the recipe. The second parameter is an Object array to get the shape of the recipe.
ItemStack() has 3 parameters. The first parameter is the item or block used. The second parameter is the stack size and the last one is the metadata of the Item or Block. This is the same for every single ItemStack everywhere in the coming code.
Make sure you don’t forget the comma’s in between the ItemStacks.
The things you add in the Object array are the things used for the recipe. It now uses a diamond a stone block and bonemeal to create one tutorialBlock. You can put the 3 items in any shape or form in the crafting table. If you don’t want that, but you want it to have a set shape you need a (Shaped)Recipe.
The code for a shaped recipe is quite a bit the same. It looks like this.


GameRegistry.addRecipe(new ItemStack(tutorialBlock), new Object[]{

                    “XZX”,
                    “CXC”,
                    ‘X’, Item.ingotGold‘C’, Block.stone‘Z’new ItemStack(Item.dyePowder, 1, 15)
             });

Instead of addShapelessRecipe you use addRecipe. The other difference is in the Object array.
The thing in here is that you have “XZX, “CXC”. This is the shape of the recipe used. The 3 letters are a code for an item, block or Itemstack. The letters are set to something below. X is a golden ingot, C is stone and Z is bonemeal. Right now this recipe uses 2 lines of the crafting table and you can use the top 2, but also the bottom 2 rows in the table to craft the recipe. You don’t have to add anything for that. If you want to use a 3×3 recipe instead of a 3×2 you just add one more “CCC” below the “CXC”. Make sure you don’t forget the comma’s. Those are important in the recipes. There is always one after every crafting table line, every letter and item, block or itemstack.

A lot of mods want or are already using enchantments in their crafting code. This is how you do that.


ItemStack enchanted = new ItemStack(Item.pickaxeStone);

enchanted.addEnchantment(Enchantment.sharpness, 2);
            
GameRegistry.addShapelessRecipe(enchanted, new Object[]{
            new ItemStack(Item.pickaxeStone), new ItemStack(Item.flint), new ItemStack(tutorialBlock)
});

What you do here is create an ItemStack variable with any name. In this case enchanted. Then you set this variable to an ItemStack like you would normally. Then you use enchanted.addEnchantment() to add the enchantment to the item. The first parameter is the enchantment type. The second is the level of it.
Now you can simply use this variable like any other ItemStack in the crafting recipe as shown in the code above.

The last type of recipe is a smelting recipe. This is the code you need for that.


FurnaceRecipes.smelting().addSmelting(Item.beefCooked.itemID, 0, new ItemStack(tutorialItem), 0.1F);


FurnaceRecipes is the file that contains all the recipes that you can use in Minecraft for the Furnace. .smelting() is a method that returns an instance of that file. With that you can acces non-static methods in there. addSmelting is one of those methods. It takes 4 parameters.
The first paramter is an item or block id which will be the input thing. You can change an Item into a number by adding itemID behind it. Same for a Block. Simply add .blockID.
The second parameter is the metadata for the input item.
The third parameter is an ItemStack which you can use like any other ItemStack which means it can have metadata, size, enchantments etc.
The last parameter is the Xp value you get from smelting. 0.1F is what you get from cobblestone. 1F is for diamond ore.
Now the mod file should look something like this.


package tutorial;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = “YourName_ModName”;
      
       public static Block tutorialBlock;
      
       public static Item tutorialItem;
      
       @EventHandler
       public void load(FMLInitializationEvent event)
       {
             tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
            
             GameRegistry.registerBlock(tutorialBlockmodid + (tutorialBlock.getUnlocalizedName().substring(5)));
            
             LanguageRegistry.addName(tutorialBlock“Tutorial Block”);
            
             tutorialItem = new ItemTutorial(5000).setUnlocalizedName(“tutorial”);
            
             LanguageRegistry.addName(tutorialItem“Tutorial Item”);
            
             GameRegistry.addShapelessRecipe(new ItemStack(tutorialBlock), new Object[]{
                    new ItemStack(Item.diamond), new ItemStack(Block.stone), new ItemStack(Item.dyePowder, 1, 15)
             });
            
             GameRegistry.addRecipe(new ItemStack(tutorialBlock), new Object[]{
                    “XZX”,
                    “CXC”,
                    ‘X’, Item.ingotGold‘C’, Block.stone‘Z’new ItemStack(Item.dyePowder, 1, 15)
             });
            
             ItemStack enchanted = new ItemStack(Item.pickaxeStone);
             enchanted.addEnchantment(Enchantment.sharpness, 2);
            
             GameRegistry.addShapelessRecipe(enchanted, new Object[]{
                           new ItemStack(Item.pickaxeStone), new ItemStack(Item.flint), new ItemStack(tutorialBlock)
             });
            
             FurnaceRecipes.smelting().addSmelting(Item.beefCooked.itemID, 0, new ItemStack(tutorialItem), 0.1F);
       }
}

Obviously this gets very big very fast. That is why most mods are using a serparate file to add the Recipes. To add that to your own mod you need something like this in the load method.


TutorialCrafting.addRecipes();


TutorialCrafting is the file that will contain the recipes for me, but it can have any name. addRecipes() is the method that will actually have the code that registers the Recipe, but also this can have any name you want.
Now you have an error under TutorialCrafting. To fix it hover your mouse over it and create the class. Then you will get an error under addRecipes. To fix that one you need to hover your mouse over there and select add method.
Then you have to copy over all the recipes you have in the mod file and paste them in that method. When you do that you will get errors under every Item and Block you use from your own mod. To fix that you will have to add your mod file name before it. For me it’s tutorial. The TutorialCrafting file should look something like this now.


package tutorial;

import net.minecraft.block.Block;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import cpw.mods.fml.common.registry.GameRegistry;
public class TutorialCrafting
{
       public static void addRecipes()
       {
             GameRegistry.addShapelessRecipe(new ItemStack(Tutorial.tutorialBlock), new Object[]{
                    new ItemStack(Item.diamond), new ItemStack(Block.stone), new ItemStack(Item.dyePowder, 1, 15)
             });
            
             GameRegistry.addRecipe(new ItemStack(Tutorial.tutorialBlock), new Object[]{
                    “XZX”,
                    “CXC”,
                    ‘X’, Item.ingotGold‘C’, Block.stone‘Z’new ItemStack(Item.dyePowder, 1, 15)
             });
            
             ItemStack enchanted = new ItemStack(Item.pickaxeStone);
             enchanted.addEnchantment(Enchantment.sharpness, 2);
            
             GameRegistry.addShapelessRecipe(enchanted, new Object[]{
                           new ItemStack(Item.pickaxeStone), new ItemStack(Item.flint), new ItemStack(Tutorial.tutorialBlock)
             });
            
             FurnaceRecipes.smelting().addSmelting(Item.beefCooked.itemID, 0, new ItemStack(Tutorial.tutorialItem), 0.1F);
       }
}

And now the mod file looks like this.


package tutorial;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = “YourName_ModName”;
      
       public static Block tutorialBlock;
      
       public static Item tutorialItem;
      
       @EventHandler
       public void load(FMLInitializationEvent event)
       {
             tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
            
             GameRegistry.registerBlock(tutorialBlockmodid + (tutorialBlock.getUnlocalizedName().substring(5)));
            
             LanguageRegistry.addName(tutorialBlock“Tutorial Block”);
            
             tutorialItem = new ItemTutorial(5000).setUnlocalizedName(“tutorial”);
            
             LanguageRegistry.addName(tutorialItem“Tutorial Item”);
            
             TutorialCrafting.addRecipes();
       }
}

This makes it look a lot better and easier to read. You can do the same technique for registering blocks, items etc. in other files, but I don’t feel like that is required in most of the smaller to medium sized mods.

You can download the source code over here.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">