Modding 1.4.7: Basic Crafting Recipe

In this tutorial I will show you how to add a basic crafting recipe to Minecraft. All of this code is done in the mod file. This is the file I will start with.

package tutorial;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;

@Mod(modid = "Tutorial_Tutorialmod", name = "Tutorial", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       @SidedProxy(clientSide = "tutorial.ClientProxyTutorial", serverSide = "tutorial.CommonProxyTutorial")
       public static CommonProxyTutorial proxy;
      
       @Init
       public void load(FMLInitializationEvent event)
       {
             proxy.registerRenderThings();
       }
}

To add a recipe to your mod you will have to add some lines of code to the load method. This is an example of the code you need for a crafting recipe.

GameRegistry.addRecipe(new ItemStack(Item.arrow, 4), new Object[]{
                    "XXX",
                    "XXX",
                    "XXX",
                    'X', Item.coal
             });

The first thing you will see is GameRegistry. If there is an error under that you will have to import it so that you get this line of code below the other imports.

import cpw.mods.fml.common.registry.GameRegistry;

If you are also getting errors under ItemStack and Item you will also have to import them.
GameRegistry is the file  you are going to use to add a recipe.
addRecipe is a method inside of GameRegistry which will add the recipe to the game for you.
After that you see a pair of brackets and inside: new ItemStack(Item.arrow, 4). 4 is the amount of items you will get back from this recipe. Item.arrow is the type of item you will get.
new Object[] is some java, but you don't have to know how or why that works. Just copy that part.
Below you see "XXX" three times. This is the shape of the recipe.
At the bottom you see 'X', Item.coal. This code will set the value of 'X' to coal. This way all the X's in the shape of 3x3 are coal items. If you replace one of the X's by a space you have an empty spot in that recipe.

If you want to use something with metadata, for example a dye, you will have to make it look like this.

GameRegistry.addRecipe(new ItemStack(Item.dyePowder, 4, 2), new Object[]{
                    "XXX",
                    "XXX",
                    "XXX",
                    'X', Item.coal
             });

Now you will get 4 items of the dyepowder with metadata 2 which means you get green dye.

If you instead want to use metadata items in a recipe you can use something like this.

GameRegistry.addRecipe(new ItemStack(Item.dyePowder, 4, 2), new Object[]{
                    "XXX",
                    "XZX",
                    "XXX",
                    'X', Item.coal, 'Z', new ItemStack(Item.dyePowder, 1, 15)
             });

This recipe not only shows you how to use a metadata item, but also how to use 2 different items in one recipe.
To use a second item in a recipe you just add another character in the 3x3 shape. Then below after the Item.coal in this case you simply add that character like you did before with 'X'. After that comes the part where you set the item. This part is also different. There is an ItemStack() here. That is because you will need one of those when you want to use metadata in a recipe.
In this case it will need 1 bonemeal surrounded by 8 coal.
If you want to use Blocks instead of items you simply change it into something like this.

GameRegistry.addRecipe(new ItemStack(Block.blockGold, 2), new Object[]
             {
                    "XXX",
                    "XZX",
                    "XXX",
                    'X', Item.coal, 'Z', new ItemStack(Item.dyePowder, 1, 15)
             });

Below you can see where the recipe code goes.

package tutorial;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "Tutorial_Tutorialmod", name = "Tutorial", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       @SidedProxy(clientSide = "tutorial.ClientProxyTutorial", serverSide = "tutorial.CommonProxyTutorial")
       public static CommonProxyTutorial proxy;
      
       @Init
       public void load(FMLInitializationEvent event)
       {
             proxy.registerRenderThings();
            
             GameRegistry.addRecipe(new ItemStack(Block.blockGold, 2), new Object[]
             {
                    "XXX",
                    "XZX",
                    "XXX",
                    'X', Item.coal, 'Z', new ItemStack(Item.dyePowder, 1, 15)
             });
       }
}


When you are done you should go back to the tutorials list here.

6 opmerkingen:

  1. I used a custom item in the recipe, but the game crashed. Heres the code:

    GameRegistry.addRecipe(new ItemStack(cardboard, 4), new Object[]{
    "XXX",
    "XXX",
    "XXX",
    'X', Item.paper });

    And...

    GameRegistry.addRecipe(new ItemStack(woodencrate, 1), new Object[]{
    "XXX",
    "XXX",
    "XXX",
    'X', cardboard });

    BeantwoordenVerwijderen
    Reacties
    1. Like when you are ingame and when you try and craft your items and it crashes? Because thats the issue I'm having with 1.4.7.

      Verwijderen
    2. Ok if you wanted to get cardboard and a woodencrate you need to have that item already made in a item class. Also when you have done that use this as the code for the recipe:

      GameRegistry.addRecipe(new ItemStack(YourMod.cardboard,4), new Object[]
      {
      "XXX",
      "XXX",
      "XXX",
      'X', Item.paper
      });
      }

      Verwijderen
    3. Deze reactie is verwijderd door de auteur.

      Verwijderen
  2. I seem to have a crash at these lines of code:
    GameRegistry.addRecipe(new ItemStack(UnnamedModItem.magicRod, 1), new Object[]
    {
    "A B",
    " S ",
    "C D",
    'S', Item.stick, 'A', new ItemStack(Item.dyePowder, 1, 1), 'B', new ItemStack(Item.dyePowder, 1, 11), 'C', new ItemStack(Item.dyePowder, 1, 0), 'D', new ItemStack(Item.dyePowder, 1, 15)
    });

    Does anyone know what could be wrong?


    BeantwoordenVerwijderen
  3. ROBLOX is empowered by an ever growing community of more than 300,000 creators who generate an infinite variety of highly immersive experiences.

    These experiences range from 3D multiplayer games and contests, to interactive adventures where friends can take on new avatars to explore what it would be like to be a dinosaur, a miner working a mine or an astronaut out in space.

    BeantwoordenVerwijderen