Crafting a Recipe
Now you will finally start modding the game. Recipes may not be that big of a change, but several combined can change the way the game is played, especially in combination with custom Items, which are explained in Hour 4, “Making Your First Item.”
Recipe code has to be added in the mod file. Listing 3.1 is the mod file from Hour 2 without the System.out.println lines.
LISTING 3.1 Clean Mod File
package com.wuppy.samsmod;
import net.minecraft.init.Blocks;
import net.minecraftforge.mods.fml.common.Mod;
import net.minecraftforge.mods.fml.common.Mod.EventHandler;
import net.minecraftforge.mods.fml.common.event.FMLInitializationEvent;
@Mod(modid = SamsMod.MODID, version = SamsMod.VERSION)
public class SamsMod
{
public static final String MODID = "wuppy29_samsmod";
public static final String VERSION = "1.0";
@EventHandler
public void init(FMLInitializationEvent event)
{
}
}
The code for recipes can look quite complicated to start with and is really hard to understand without starting off with a code example. In the code that follows, you can see an example of a recipe. When you add this code, make sure you import the new files.
GameRegistry.addRecipe(new ItemStack(Items.apple),
"XXX",
"XXX",
"XXX",
'X', Blocks.leaves
);
This recipe will look like Figure 3.1.
FIGURE 3.1 The Apple recipe in game.
Using GameRegistry
The first part of this line is GameRegistry. GameRegistry is a file you will frequently use when coding Minecraft Forge mods. This file contains the registry of every recipe, and also items and blocks.
Because there are so many things you can do with this file, it has many methods that all do different things. To create a basic shaped recipe, you need the addRecipe method. This method takes two parameters. The first parameter is an ItemStack. An ItemStack is another class you will use frequently. It’s used to handle items or blocks in stacks. This recipe shows the most basic type of ItemStack, but you can add two more numbers as parameters. What those do will be shown in a different recipe. When you just use an Item or Block as parameter, it will create an ItemStack with that object with a stack size of 1 and a metadata of 0. Metadata and how to code it are explained in Hour 5, but examples of metadata are dyes and wool blocks.
The first parameter is the Item or Block the recipe will return. In this case it will return a single apple.
The second parameter in the addRecipe mode is a weird one. If you take a look at the parameter in the GameRegistry class, it’s written as follows:
Object... params
The ... means that it’s a parameter that can take any amount of values you want. This parameter can be interpreted as some sort of list. Everything you put after the first parameter of the addRecipe method will be added in this list and used in the recipe.
The Shape of the Recipes
The way these Recipes are done in Minecraft is by typing the shape of the crafting table in the recipe using three strings. Each of the three strings contains three spaces to put an Item or a Block. Another example of the crafting table space:
"ABC",
"DEF",
"GHI",
Each letter represents one of the usable crafting areas in the crafting grid in the crafting table If you want one of the spots to be empty, you replace one of the lines with a space.
Up to this point in the code, it’s just letters. Somehow those letters have to be changed into Items and Blocks. That is done just below the crafting grid shape. After the last row that ends with a comma, the letter X is surrounded by apostrophes, which turns it into a character. After this character you need another comma, followed by the Block that you want it to represent, which are leaves in this example. The recipe created by the preceding code would require each area in the crafting grid to be filled with a leaf and it will then return an apple.
Alternative Shapes
Sometimes you don’t require a three-by-three space to create a crafting recipe. In some cases you just need a two-by-two area. This is done using the following code.
"AB",
"CD",
With this code, the two-by-two shape can be placed anywhere in the crafting table. Another example of a differently shaped recipe is bread, which would look something like this:
"AAA",
In here the letter is the same each time, because bread is crafted using three wheat, but you can use different letters.
If you have more than a single Item or Block that is used in the shape, you have to add a comma behind the last Item or Block, write another pair of apostrophes, and follow that up by the other Item or Block.
An example of using a smaller grid, empty spaces, and using multiple components is shown next:
GameRegistry.addRecipe(new ItemStack(Items.arrow),
"YZ",
"X ",
'X', Items.flint, 'Y', Items.stick, 'Z', Blocks.leaves
);
This will allow you to craft an arrow in your player inventory using leaves. Figure 3.2 shows the crafting of the recipe.
FIGURE 3.2 A smaller recipe in the player’s inventory.
Another thing you may want to have in your recipes is that the returned ItemStack has a size of more than 1. Two other things that may be interesting for a recipe are metadata for the used and the resulting Item or Block. You can see all of these things in a single recipe, as follows:
GameRegistry.addRecipe(new ItemStack(Items.dye, 2, 1),
"XY",
'X', Items.redstone, 'Y', new ItemStack(Items.dye, 1, 1)
);
Each of the ItemStacks used in this recipe have three parameters now. The first is still an Item or Block. The second is the stack size. You can return a stack size of more than one, but you can’t use a higher stack size in a used item. The third parameter is the damage or metadata of the item. Metadata starts counting at 0 and goes up to 15. This recipe will combine one redstone and one red dye to make two red dyes. Red dye is the second metadata for dyes. Therefore the damage is 1.
This recipe may seem a little illogical, because this recipe will work only when the dye is on the right. For recipes like this, generally a Shapeless Recipe is used.