What Does The Code for a Crafting Recipe Look Like?

The code below makes a recipe for mySword to be crafted out  of emeralds and a stick:

GameRegistry.addShapedRecipe(new ItemStack(mySword), 
" E ",
" E ",
" S ",
'S', Items.stick,
'E', Items.emerald);

In game the recipe will look like this:

What Are Some Common Mistakes When Writing Code For a Crafting Recipe?

In the middle of the code on these lines:

" E ",
" E ",
" S ",

This is where you set the shape of your recipe, notice how the placement of the letters matches up with the recipe in game.

For it to work properly there needs to be exactly 3 spaces and letters combined between the quotation marks on each line, such as:

"   " or " A " or "A  " or "  A"  or "AA " or " AA" or "A A" or "AAA"

At end of the code on these lines:

'S', Items.stick,
'E', Items.emerald);

Notice there is a comma , at the end of the first line,
and a close parentheses and semicolon ); on the last line.

If I add a D for a dirt block to the middle section I would change this part to look like this:

'S', Items.stick,
'E', Items.emerald,
'D', Blocks.dirt);

Now the 'E' line ends in a comma and the new line at the end has the close parentheses and semicolon.

The order of the lines doesn't matter, but the order of the endings does:

  • The last line always ends in  );

  • And the other lines all end in ,

Did this answer your question?