How to make an item that fills the hunger bar (Minecraft mod)
The hunger bar lives in the player’s food data. Its eat(food, saturation) method adds food points (20 is full) and saturation. For an item that is real food with a bite animation, give it food properties instead; this is for an effect you control, such as a charm.
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.
- Override use and work only on the server.
- Add food and saturation to the player’s food data (getFoodData().eat(food, saturation)).
- Add a cooldown.
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Forge 1.17.1 to 1.20.1
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Forge 1.17.1 to 1.20.1”Uses:
Player.getFoodDataFoodData.eat
src/main/java/com/example/examplemod/item/HungerCharmItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobSpawnType;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Hunger Charm: right-click to restore 6 food points and some saturation.public class HungerCharmItem extends Item { public HungerCharmItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (!level.isClientSide()) { player.getFoodData().eat(6, 0.6f); player.getCooldowns().addCooldown(this, 40); } return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as HungerCharmItem: see a new item.
Fabric and NeoForge 1.21.2 to 26.3
Section titled “Fabric and NeoForge 1.21.2 to 26.3”Uses:
Player.getFoodDataFoodData.eat
src/main/java/com/example/examplemod/item/HungerCharmItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.EntitySpawnReason;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Hunger Charm: right-click to restore 6 food points and some saturation.public class HungerCharmItem extends Item { public HungerCharmItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { if (!level.isClientSide()) { player.getFoodData().eat(6, 0.6f); player.getCooldowns().addCooldown(player.getItemInHand(hand), 40); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as HungerCharmItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
PlayerEntity.getFoodDataFoodStats.eat
src/main/java/com/example/examplemod/item/HungerCharmItem.java
package com.example.examplemod.item;
import net.minecraft.util.Hand;import net.minecraft.util.ActionResult;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Hunger Charm: right-click to restore 6 food points and some saturation.public class HungerCharmItem extends Item { public HungerCharmItem(Item.Properties properties) { super(properties); }
@Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { if (!world.isClientSide()) { player.getFoodData().eat(6, 0.6f); player.getCooldowns().addCooldown(this, 40); } return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide()); }}It’s registered like any other item, as HungerCharmItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
EntityPlayer.getFoodDataFoodStats.eat
In 1.12.2 the food data is player.getFoodStats() and the method is addStats(food, saturation).
src/main/java/com/example/examplemod/item/HungerCharmItem.java
package com.example.examplemod.item;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;import net.minecraft.util.ActionResult;import net.minecraft.entity.player.EntityPlayer;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Hunger Charm: right-click to restore 6 food points and some saturation.public class HungerCharmItem extends Item { @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { if (!world.isRemote) { player.getFoodStats().addStats(6, 0.6f); player.getCooldownTracker().setCooldown(this, 40); } return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand)); }}It’s registered like any other item, as HungerCharmItem: see a new item.
Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/items/interactions
- https://docs.fabricmc.net/1.21.1/develop/items/first-item
Related how-tos
Section titled “Related how-tos”- An item that gives the player another item
- An item that heals you when you use it
- An item that launches you into the air
- An item that teleports you forward
- Show a message above the hotbar
In Linen
Section titled “In Linen”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.