Magic Cookie!
Custom Food & Effects
Add a Magic Cookie to Minecraft eat it and you'll zoom around with Speed II and leap with Jump Boost! Learn how food, hunger and status effects work in Fabric.
Add a Magic Cookie to Minecraft eat it and you'll zoom around with Speed II and leap with Jump Boost! Learn how food, hunger and status effects work in Fabric.
Understand the three food properties we'll set: nutrition, saturation and status effects.
Add the Magic Cookie item to ModItems.java with food properties and status effects.
registerModItems(), add: entries.add(MAGIC_COOKIE); inside the lambda..nutrition().saturationModifier().statusEffect()...) is new syntax for most students. Remind them every line except the last ends in a dot, and the final line is .build() with no dot after it. Missing a dot or adding an extra one is the most common compile error here.net.minecraft.component.type.FoodComponent.net.minecraft.entity.effect.StatusEffects.net.minecraft.entity.effect.StatusEffectInstance..build(). Missing or extra dots break the whole chain.statusEffect(instance, probability) is 1.0f = 100% chance. If you put 0.0f it never applies!Create a glowing cookie texture and add the model + language files.
assets/crystalmod/textures/item/.assets/crystalmod/models/item/:"item.crystalmod.magic_cookie": "Magic Cookie"Create a shapeless crafting recipe any arrangement of ingredients will work.
data/crystalmod/recipe/, create magic_cookie.json:Launch Minecraft, eat your Magic Cookie and zoom around with speed and jump boost!
/give @p crystalmod:magic_cookie to get some cookies.Experiment with adding more status effects to the Magic Cookie's FoodComponent.
.statusEffect(...) calls as you want! Here are some fun ones to try:Add the alwaysEdible() modifier so the Magic Cookie can be eaten any time even with a full hunger bar.
.alwaysEdible() lets you eat it any time just like golden apples! This is useful for utility food that gives effects rather than just restoring hunger..nutrition(4), add .alwaysEdible():Add a second food item using the same FoodComponent pattern. Make it your own invention!
Review what you built and understand what's coming next.
FoodComponent.Builder to define hunger, saturation and effectsStatusEffectInstance sets the effect, duration and level.alwaysEdible() overrides hunger requirements