Creating New Recipes


While an important part of Minecraft is mining, another major part is crafting, hence the name! By combining items within the inventory screen or on a crafting bench into a recipe, we can create new items, like a pickaxe, or a redstone repeater. This allows us to do many more things within the game, and is a mechanic we can easily modify using Forge. Within Minecraft, there are three major types of crafting:

  1. Shapeless - Recipes without a defined shape (rose red + wool -> red-dyed wool)
  2. (Shaped) Crafting - Recipes with a defined shape (sticks + cobblestone -> pickaxe)
  3. Smelting - Recipes with one input to a furnace (iron ore -> iron ingots)

We can add our own custom recipes by utilizing a class called GameRegistry. This class is a collection of every recipe, item, and block in Minecraft, and provides methods to add or modify them. The first one we will look at is addRecipe(), which quite clearly will add a recipe to the game.

Shaped Crafting

addRecipe() is unique to the methods we have looked at before, other than perhaps System.out.printf(), in that it takes a variable number of parameters. The first parameter is an instance of class ItemStack, which represents a stack of items in the game. The second parameter is the recipe itself, made up of up to three Strings of alphabetic characters. The last parameters are the specifiers of the items in the recipe. Let's look at an example of an added recipe, this case to make diamonds from dirt.

GameRegistry.addRecipe(new ItemStack(Items.diamond),
    "AAA",
    "AAA",
    "AAA",
    'A', Blocks.dirt);

By changing the parameter string, we can change the recipe we create! Now, creating diamonds from nine dirt is nice, but let's make a more complex recipe. This time, we will make a new arrow recipe that we can create without a crafting bench!

GameRegistry.addRecipe(new ItemStack(Items.arrow, 5),
    "AB",
    "C ",
    'A', Items.stick, 'B', Blocks.dirt, 'C', Items.feather);
Shapeless Crafting

But what if our recipe didn't make sense as a shaped recipe? We could instead add a shapeless recipe! Here, we just specify what we want, and how much of what we need, and as long as the requisite items are in a crafting area the recipe will work. GameRegistry provides a method addShapelessRecipe() whose first parameter is what we want (just like addRecipe()), while the next up to nine parameters are the required Items or Blocks.

Now, so far we have just been making recipes with simple outputs, with the only modification being the amount in the returned stack of items. However, say we wanted to make a recipe that returned a special items, such as one with enchantments? Well, we can simply instantiate our ItemStack ahead of time, and use that! In fact, let's make a recipe that upgrades a stone sword with 10x knockback (for comedic effect) when combined with flint:

ItemStack enchantedSword = new ItemStack(Items.stone_sword);
enchantedSword.addEnchantment(Enchantment.knockback, 10);

// Appending a decimal number with 'F' makes it a float, not a double
GameRegistry.addShapelessRecipe(enchantedSword, 
    Items.stone_sword, Items.flint);
Smelting Recipes

Smelting recipes are unique in that they require one input, one output, and a level of experience. Every time you use a furnace in Minecraft to smelt an ore, you receive some experience. We will need to take this into account when we create smelting recipes.

Let's make a smelting recipe that makes cobblestone from flint:

GameRegistry.addSmelting(Blocks.cobblestone, new ItemsStack(Items.flint), 0.1F);

Example Mod

package edu.govschool.minecraft.example;

import net.minecraft.enchantment.Enchantment;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
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.registry.GameRegistry;

@Mod(modid = CraftingExample.MODID, version = CraftingExample.VERSION)
public class CraftingExample 
{   
    // Mod ID for our mod
    public static final String MODID = "mrdavis_crafting";
    // Our mod's version
    public static final String VERSION = "1.0";
    
    @EventHandler
    public void init(FMLInitializationEvent e)
    {
        // Add a recipe for nine dirt -> diamond
        GameRegistry.addRecipe(new ItemStack(Items.diamond),
                "AAA",
                "AAA",
                "AAA",
                'A', Blocks.dirt);
        
        // Add a recipe for stick + dirt + feather -> 5 arrows
        GameRegistry.addRecipe(new ItemStack(Items.arrow, 5),
                "AB",
                "C ",
                'A', Items.stick, 'B', Blocks.dirt, 'C', Items.feather);
        
        // Create a stone sword with Knockback X enchantment
        ItemStack enchantedSword = new ItemStack(Items.stone_sword);
        enchantedSword.addEnchantment(Enchantment.knockback, 10);

        // Add a shapeless recipe to add our Knockback X
        // enchantment to a stone sword with a flint
        GameRegistry.addShapelessRecipe(enchantedSword, 
                Items.stone_sword, Items.flint);
        
        // Add a smelting recipe to turn flint into cobblestone
        // Appending a decimal number with 'F' makes it a float, not a double
        GameRegistry.addSmelting(Items.flint, new ItemStack(Blocks.cobblestone), 0.1F);
    }
}