Basic Modding - Custom Food


I want to show you how to create food which gives the Player potion effects when eaten.


2/10 - Quite Simple


Forge Version

This Tutorial was created with Forge for Minecraft 1.7.10. If anything doesn't work with other versions, please contact me!

Creating Custom Food

Creating Food is as simple as creating an Item, we only need to add a special Item class. This is what we will do first. Our class should extend the class ItemFood.

public class ItemModFood extends ItemFood {

    public ItemModFood(int p_i45339_1_, float p_i45339_2_, boolean p_i45339_3_) {
        super(p_i45339_1_, p_i45339_2_, p_i45339_3_);

To make life easier when creating the Items we want to have a constructor which takes a few more arguments than the normal one, for example PotionEffects which are applied when the food is eaten and of course the unlocalized Name. The Constructor should look like this:

ItemModFood(String unlocalizedName, int healAmount, 
float saturationModifier, boolean wolvesFavourite,
PotionEffect... effects);

The three dots behind PotionEffect mean that you can add as many potion effects as you want. They will be converted automatically in an array of PotionEffect[].

To save those potion effects we need to add a field to store it:

private PotionEffect[] effects;

Then we can create the constructor we wanted to have:

public ItemModFood(String unlocalizedName, int healAmount, float saturationModifier, boolean wolvesFavorite, PotionEffect... effects) {
    super(healAmount, saturationModifier, wolvesFavorite);
    this.setTextureName(Main.MODID + ":" + unlocalizedName);
    this.effects = effects;

If you created an Item with this code now and ate it, you won't get potion effects at all, even if you added some to the Constuctor. This is because we have stored them in the field, but we don't apply them to the player when the food is eaten. To do this we need to override the method onFoodEaten(...);.

To do this in eclipse, press Alt+Shift+S and select "Override/Implement Methods". Then a dialog will open where you can checkmark onFoodEaten. Now hit OK and the method will be added to your Code.

public ItemStack onFoodEaten(ItemStack p_77654_1_, World p_77654_2_,
        EntityPlayer p_77654_3_) {
    return super.onFoodEaten(p_77654_1_, p_77654_2_, p_77654_3_);

We now change the variable names (so you can read it easier) and add some new lines so that the code looks like this:

protected void onFoodEaten(ItemStack stack, World world, EntityPlayer player) {
    super.onFoodEaten(stack, world, player);
    for (int i = 0; i < effects.length; i ++) {
        if (!world.isRemote && effects[i] != null && effects[i].getPotionID() > 0)
            player.addPotionEffect(new PotionEffect(this.effects[i].getPotionID(), this.effects[i].getDuration(), this.effects[i].getAmplifier(), this.effects[i].getIsAmbient()));

This is a loop which will iterate over every PotionEffect in the list we set, check whether the effect is valid (green statements) and apply it to the player who had eaten the food. To do this, the PotionEffect in the array is cloned with the blue statement. This is necessary, because if you applied a potion effect from the array multiple times (because you eat the food again) it would not work, because the Effect in the array is already used up (Its duration is zero). Thats why we need to clone the PotionEffect.


You only apply the PotionEffect on Server side, so if we are on Client side the red statement is false and nothing happens.


Lets create an Item with several potion effects. I'll show you the creation of the "chocolate" item from my mod WeaponCraft.

This Item will give the player two hunger points, a rather low saturation factor and some potion effects:

  • Speed effect for 1200 Ticks (= 60 seconds) with the amplifier of 1 (Potionlevel 2)
  • Jumpboost effect for 600 Ticks (= 30 seconds) with the amplifier of 0 (Potionlevel 1)
  • Regeneration effect for 200 Ticks (= 10 seconds) with the amplifier of 1 (Potionlevel 2)

Kind of overpowered chocolate, but who cares?



Below you can find some common values for the healAmount and the saturationModifer. The healAmount is the amount of hunger points that is added to the player's hunger bar. The saturationModifier determines how fast the player gets hungry again. The higher this modifier, the slower the player gets hungry.


Item healAmount saturationModifier
Apple 4 0.3f
Bread 5 0.6f
Raw Porkchop or Beef 3 0.3f
Cooked Porkchop or Beef 8 0.8f
Golden Apple 4 1.2f


You can find the other values in the Class Items.class where all the Items of Minecraft are created.

The creation of this item looks like this:

GameRegistry.registerItem(tutorialChocolate = new ItemModFood("tutorial_chocolate", 2, 0.2f, false,
    new PotionEffect(, 1200, 1),
    new PotionEffect(, 600, 0),
    new PotionEffect(, 200, 1))
    .setAlwaysEdible(), "tutorial_chocolate");

The method setAlwaysEdible() makes the chocolate edible even if the player is not hungry.

The texture of the chocolate item is this one:


If you want to apply the effects only with a certain propability, you can either use the method provided by the vanilla ItemFood or use the class ItemModAdvancedFood I created (you can, of course, rename it). You can find this class in the downloadable code. I won't explain it here, but you will understand it if you look at it.

If you want to use the vanilla method, you can only use one single effect with a propability. The method call is this one:

Effect with Propability:
itemObject.setPotionEffect(, 10, 0, 0.75F);

This code adds a blindness effect to the item which lasts for 10 seconds (!), has an amplifier of 0 (Level 1 of the potion) and a propability of 75% to be applied to the Player.


If you want to use more than one effect with a propability then you need to use my class. With this the adding of multiple effects with a propability is quite easy, as you can see here where I create the "lucky candy":

GameRegistry.registerItem(tutorialLuckyCandy = new ItemModAdvancedFood("tutorial_lucky_candy", 1, 0.5f, false)
    .addPotionEffect(new PotionEffect(, 200, 4), 0.5)
    .addPotionEffect(new PotionEffect(, 200, 4), 0.5).setAlwaysEdible(), "tutorial_lucky_candy");

This code creates a food item wich gives both regeneration V and wither V for 200 ticks (10 seconds) with a propability of 50% on each of them, so you either get one of them, both or none.


The texture of the lucky candy is this one here:

You can download the code used in this tutorial as a .zip file from here.

Recommended tutorials to continue with

Take a look at the tutorial overview and find out what you want to do next!

Comments and Questions:

If you want to report modding problems, please make sure to include the code in a pastebin link or something else! Don't just write "It doesn't work", otherwise your post will be deleted. For more complicated problems, please use the troubleshooter form.