Smelting & Crafting Recipes

At this point, we have everything we need for our Red Diamond tools and weapons, except how to make them.  Sure we can get the them in creative, but what about playing in survival?  So we will need to turn our attention towards smelting and crafting.  Smelting is the act of putting something in a furnace and then getting something else from the furnace after heat is applied.  Crafting recipes are when you put items into a crafting table and get some other from the crafting table. 

Let’s start with the smelting, since that is just so much easier.  All you really need to is define three things and use the GameRegistry.addSmelting() method.  The three parameters are:

  • An input block/item id to smelt
  • A resulting item stack with the number to return
  • The experience points to get after smelting

 

ItemStack redDiamondStack = new ItemStack(redDiamondItem, 2);
GameRegistry.addSmelting(Item.diamond.itemID, redDiamondStack, 1.0F);

 

It is important to note that we need to use the integer id of the block or item.  As you can see above, it was necessary to chain together a set of references to get the id.  Next, youn will need to create an ItemStack object for the result.  You can change the number to get back by using the alternate constructor for the ItemStack that contains a parameter for the number of items.  The constructors are ItemStack(Block/Item) and ItemStack(Block/Item, stackSize).  Finally, the experience points should be in the range of 0.1 to 1.0, with the higher values for stuff that is harder to mine.  This is a convention set forward by Minecraft.  The idea being that you should only get very small amounts of experience from smelting. 

In the example above, you would be able to add a diamond to a fueled furnace and the result would be two red diamonds plus one experience point.  I know it is a bit generous, but it really is just to give you the idea of how it works. 

Crafting Recipes

There are two kinds of recipes, shaped and shapeless.  The shapeless recipes are pretty easy to create.  You can do them in a single line of code, and are in general much more flexible.  The shaped crafting recipes require that you create a template, and your output will depend upon matching that template exactly. 

Starting with the shapeless crafting recipes.  These will use the GameRegistry.addShapelessRecipe() method.  You will need to create an ItemStack object with the output item and the number to craft, followed by one or more items/blocks that are needed for the recipe.  This will generate any combination of the recipe anywhere on the crafting table. 

 

GameRegistry.addShapelessRecipe(new ItemStack(redDiamondItem, 1), Block.blockDiamond, Block.blockRedstone);

The above shapeless recipe will give you one red diamond item when you put a diamond block and a redstone block in a crafting table.  Since it is a shapeless recipe, the placement will not matter. 

Crafting Recipes – Shaped

Now let’s take a look at shaped crafting recipes.  These will use the GameRegistry.addShapedRecipe() method, but they will require a specific shaped to be defined and then matched in the crafting table. 

public static Object[] recipe = {
    " * ", " * ", " X ", 
    'X', Item.stick, 
    '*', RedDiamondMod.redDiamondItem
};

 

This is a shaped recipe for a Red Diamond sword.  The first three strings represent the crafting table.  The first string is the top row.  The second string is the middle row, and the third string is the bottom row.  Sometimes it helps to line them up in a block to get a better view of how it would look in the crafting table. 

In the crafting table, you will need to place characters in the different cells for each of the items to be used in the recipe.  In the recipe above, the top has an asterisk in the middle cell of that row, and there is an asterisk in the middle cell of the middle row.  In the bottom row, the middle cell has a X.  Next, there will be pairs.  The first part of the pair will be the character to replace, and the second will be the item to replace it with.  In this example, the X will be replaced with a stick and the * will be replaced with a Red Diamond. 

Finally, we would need to use the addRecipe() method to make the recipe available during the game. 

GameRegistry.addRecipe(new ItemStack(redDiamondSword, 1), RedDiamondSword.recipe);