Skip to content

How to make food that gives a potion effect in a Minecraft mod

Food is an item with food properties. The builder takes the hunger (nutrition) and saturation modifier, and .alwaysEdible() lets players eat it while full (alwaysEat before 1.20.5). Up to 1.21.1 the builder’s .effect(effect, probability) adds a potion effect that comes with some chance (1.0 is always); from 1.21.2 the effect goes on the Consumable that goes with the food. The effect’s length is in ticks, 20 to the second.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Build the food properties with the nutrition and saturation modifier.
  2. Add the effect with a chance of 1.0 for a sure effect (on the builder before 1.21.2, on the consumable after).
  3. Add alwaysEdible if it should be eaten while full.
  4. Pass it to the item’s properties with .food(…) and register the item.

The example uses these values. Change them in the code.

Setting In the example What it does
effect REGENERATION The effect eating gives.
seconds 10 How long the effect lasts, in seconds.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric and NeoForge 1.20.1 to 1.20.4, Forge 1.17.1 to 1.20.1, Fabric 1.16.5 to 1.20

Section titled “Fabric and NeoForge 1.20.1 to 1.20.4, Forge 1.17.1 to 1.20.1, Fabric 1.16.5 to 1.20”

Uses:

  • FoodProperties.Builder.effect
  • FoodProperties.Builder.alwaysEat
  • MobEffectInstance
  • MobEffects
  • Item.Properties.food

It’s registered like any other item: see a new item.

new Item.Properties().food(new FoodProperties.Builder().nutrition(4).saturationMod(0.3F).alwaysEat().effect(new MobEffectInstance(MobEffects.REGENERATION, 10 * 20), 1.0F).build())

Uses:

  • FoodProperties.Builder.effect
  • FoodProperties.Builder.alwaysEdible
  • MobEffectInstance
  • MobEffects
  • Item.Properties.food

It’s registered like any other item: see a new item.

new Item.Properties().food(new FoodProperties.Builder().nutrition(4).saturationModifier(0.3F).alwaysEdible().effect(new MobEffectInstance(MobEffects.REGENERATION, 10 * 20), 1.0F).build())

Uses:

  • FoodProperties.Builder.alwaysEdible
  • Consumable.builder
  • ApplyStatusEffectsConsumeEffect
  • MobEffectInstance
  • Item.Properties.food
  • Item.Properties.setId

Since 1.21.2 the effect is not on the FoodProperties but on the Consumable that goes with it: Item.Properties#food(food, consumable). An item’s properties need its id set before the item is made (setId); register the item with the same id, or the game crashes at startup.

It’s registered like any other item: see a new item.

new Item.Properties().setId(ResourceKey.create(Registries.ITEM, ResourceLocation.fromNamespaceAndPath("examplemod", "glow_berry_tart"))).food(new FoodProperties.Builder().nutrition(4).saturationModifier(0.3F).alwaysEdible().build(), Consumable.builder().onConsume(new ApplyStatusEffectsConsumeEffect(new MobEffectInstance(MobEffects.REGENERATION, 10 * 20), 1.0F)).build())

Uses:

  • FoodProperties.Builder.alwaysEdible
  • Consumable.builder
  • ApplyStatusEffectsConsumeEffect
  • MobEffectInstance
  • Item.Properties.food
  • Item.Properties.setId

Since 1.21.2 the effect is not on the FoodProperties but on the Consumable that goes with it: Item.Properties#food(food, consumable). An item’s properties need its id set before the item is made (setId); register the item with the same id, or the game crashes at startup.

It’s registered like any other item: see a new item.

new Item.Properties().setId(ResourceKey.create(Registries.ITEM, Identifier.fromNamespaceAndPath("examplemod", "glow_berry_tart"))).food(new FoodProperties.Builder().nutrition(4).saturationModifier(0.3F).alwaysEdible().build(), Consumable.builder().onConsume(new ApplyStatusEffectsConsumeEffect(new MobEffectInstance(MobEffects.REGENERATION, 10 * 20), 1.0F)).build())

Uses:

  • Food.Builder.effect
  • Food.Builder.alwaysEat
  • EffectInstance
  • Effects
  • Item.Properties.food

It’s registered like any other item: see a new item.

new Item.Properties().food(new Food.Builder().nutrition(4).saturationMod(0.3F).alwaysEat().effect(new EffectInstance(Effects.REGENERATION, 10 * 20), 1.0F).build())

Uses:

  • ItemFood
  • ItemFood.setPotionEffect
  • ItemFood.setAlwaysEdible

In 1.12.2 an ItemFood gives its effect with setPotionEffect(new PotionEffect(potion, ticks), 1.0F) and can be eaten when full with setAlwaysEdible().

src/main/java/com/example/examplemod/item/GlowBerryTartItem.java

package com.example.examplemod.item;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemFood;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
// Glow Berry Tart: food that always gives REGENERATION for 10 seconds, and can be eaten with a full stomach.
public class GlowBerryTartItem extends ItemFood {
public GlowBerryTartItem() {
super(4, 0.3F, false);
setAlwaysEdible();
setPotionEffect(new PotionEffect(effect("REGENERATION"), 10 * 20), 1.0F);
}
// 1.12.2 names its effects differently from newer versions (MOVEMENT_SPEED is SPEED, JUMP is JUMP_BOOST).
private static Potion effect(String name) {
switch (name) {
case "MOVEMENT_SPEED":
return MobEffects.SPEED;
case "NIGHT_VISION":
return MobEffects.NIGHT_VISION;
case "JUMP":
return MobEffects.JUMP_BOOST;
case "ABSORPTION":
return MobEffects.ABSORPTION;
case "FIRE_RESISTANCE":
return MobEffects.FIRE_RESISTANCE;
default:
return MobEffects.REGENERATION;
}
}
}

It’s registered like any other item, as GlowBerryTartItem: see a new item.

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.