#So how do i make a food item give an effect when eaten? (latest version of forge 1.20)

1 messages · Page 1 of 1 (latest)

olive compass
#

So i just need to know ive tried everything i could find and nothing seemed to work. Tho i might just be dumb.

grim heartBOT
#

<@&987246652869971988> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

To make a food item give an effect when eaten in the latest version of Forge (1.20), you will need to create a custom food item class and override the onFoodEaten method to apply the desired effect.

Here's an example of how you can achieve this:

  1. Create a new class for your custom food item, for example CustomFoodItem.java, and make sure it extends ItemFood:
import net.minecraft.item.ItemFood;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;

public class CustomFoodItem extends ItemFood {

    public CustomFoodItem(int amount, float saturation, boolean isWolfFood) {
        super(amount, saturation, isWolfFood);
    }

    @Override
    protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player) {
        // Apply the desired effect here
        player.addPotionEffect(new PotionEffect(Potion.getPotionById(1), 200, 1)); // Example: Speed II for 10 seconds
        super.onFoodEaten(stack, worldIn, player);
    }
}
  1. Register your custom food item in your mod's main class or an appropriate registration class:
public static final Item CUSTOM_FOOD_ITEM = new CustomFoodItem(4, 0.6F, false)
        .setUnlocalizedName("custom_food_item")
        .setRegistryName("custom_food_item");

@Mod.EventHandler
public void init(FMLInitializationEvent event) {
    GameRegistry.register(CUSTOM_FOOD_ITEM);
}

Make sure to adjust the parameters of CustomFoodItem constructor according to your needs.

  1. Finally, you can use the registered custom food item in your mod or give it to players through other means (e.g., crafting recipes).

When players eat this custom food item, it will apply the specified effect. In the example above, it applies Speed II for 10 seconds (200 ticks).

Remember to adjust the effect and duration according to your desired outcome.

hushed breach
#

which error do you get when you hover over EffectInstance?

olive compass
#

Theres only one error in the list.

hushed breach
#

so you already need to handle that one.

olive compass
hushed breach
#
try {
  //  Block of code to try
}
catch(Exception e) {
  //  Block of code to handle errors
}

or add it to your method signature (throws IOException) and handle it elsewhere