🇬🇧 English
🇬🇧 English
Appearance
🇬🇧 English
🇬🇧 English
Appearance
This page is written for version:
1.21.4
This page is written for version:
1.21.4
Food is a core aspect of survival Minecraft, so when creating edible items you have to consider the food's usage with other edible items.
Unless you're making a mod with overpowered items, you should consider:
To add a food component to an item, we can pass it to the Item.Settings
instance:
new Item.Settings().food(new FoodComponent.Builder().build())
Right now, this just makes the item edible and nothing more.
The FoodComponent.Builder
class has some methods that allow you to modify what happens when a player eats your item:
Method | Description |
---|---|
nutrition | Sets the amount of hunger points your item will replenish. |
saturationModifier | Sets the amount of saturation points your item will add. |
alwaysEdible | Allows your item to be eaten regardless of hunger level. |
When you've modified the builder to your liking, you can call the build()
method to get the FoodComponent
.
If you want to add status effects to the player when they eat your food, you will need to use the ConsumableComponent
alongside the FoodComponent
class as seen in the following example:
public static final ConsumableComponent POISON_FOOD_CONSUMABLE_COMPONENT = ConsumableComponents.food()
// The duration is in ticks, 20 ticks = 1 second
.consumeEffect(new ApplyEffectsConsumeEffect(new StatusEffectInstance(StatusEffects.POISON, 6 * 20, 1), 1.0f))
.build();
public static final FoodComponent POISON_FOOD_COMPONENT = new FoodComponent.Builder()
.alwaysEdible()
.build();
Similar to the example in the Creating Your First Item page, I'll be using the above component:
public static final RegistryKey<Item> POISONOUS_APPLE_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of(FabricDocsReference.MOD_ID, "poisonous_apple"));
public static final Item POISONOUS_APPLE = register(
new Item(new Item.Settings().registryKey(POISONOUS_APPLE_KEY).food(POISON_FOOD_COMPONENT, POISON_FOOD_CONSUMABLE_COMPONENT)),
POISONOUS_APPLE_KEY
);
This makes the item: