Modding: Adding a food item

In this tutorial I will teach you how to add a food to the game. I will start out with this file.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = “YourName_ModName”, name = “ModName”, version = “Version number”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
public static Block oreblock;

public static Item youritem;

@SidedProxy(clientSide = “Tutorial.client.ClientProxyTutorial”, serverSide = “Tutorial.common.CommonProxyTutorial”)
    public static CommonProxyTutorial proxy;

@Init
public void load(FMLInitializationEvent event) 
{
oreblock = new BlockOres(230, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);

GameRegistry.registerBlock(oreblock);

LanguageRegistry.addName(oreblock, “Your Ore”);

youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);

LanguageRegistry.addName(youritem, “Your Item”);

proxy.registerRenderThings();

DungeonHooks.addDungeonLoot(new ItemStack(youritem), 10, 2, 5);
DungeonHooks.setDungeonLootTries(12);
}
}


To add a a food item you should add this.


public static Item yourfood;

yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
LanguageRegistry.addName(yourfood, “Your Food”);


And the whole file will end up like this.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = “YourName_ModName”, name = “ModName”, version = “Version number”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
public static Block oreblock;

public static Item youritem;

public static Item yourfood;

@SidedProxy(clientSide = “Tutorial.client.ClientProxyTutorial”, serverSide = “Tutorial.common.CommonProxyTutorial”)
    public static CommonProxyTutorial proxy;

@Init
public void load(FMLInitializationEvent event) 
{
oreblock = new BlockOres(230, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);

GameRegistry.registerBlock(oreblock);

LanguageRegistry.addName(oreblock, “Your Ore”);

youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);

LanguageRegistry.addName(youritem, “Your Item”);

proxy.registerRenderThings();

DungeonHooks.addDungeonLoot(new ItemStack(youritem), 10, 2, 5);
DungeonHooks.setDungeonLootTries(50);

yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
LanguageRegistry.addName(yourfood, “Your Food”);
}
}


You should know what most of what you just added means. If not I suggest you read the item tutorial.
This is the only new piece of code.


new ItemTutorialFood(551, 8, true)


ItemTutorialFood is a new class you will have to create. There is a normal ItemFood class in the minecraft source code, but for the item to work with a texture from your sprite sheet you will have to create a new file.
551 is the id, 8 is the amount of food it will regenerate after eating it in half hearts.
The value after 8 is a boolean. A boolean can be either true or false. If this boolean is true a wolf will eat this food. If it is false the wolf will not want it.

Now for the ItemTutorialFood.
You should go to the net.minecraft.src package and find the file called ItemFood. Copy everything that is in there and paste it in your ItemTutorialFood.
There are 2 things that you will have to change.
First you will have to make it extend ItemFood like this.


public class ItemTutorialFood extends ItemFood


You will also have to add the texture file method like any normal item or block.


public String getTextureFile()
    {
            return “/TutTextures.png”;
    }


I will not be copying the file here, because you should be able to copy this. However, if you can’t get it working you can find it here. I suggest that you don’t copy it from there though. A big part of modding for minecraft is finding the right things to copy and fixing them so they fit your mod.
The item you have created will be shown in the foodstuffs tab of your creative inventory.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">