Minecraft 1.12 modding with forge – 8 – Custom food

Hello everyone,

in this part I will show you how to make custom food and how to make food that gives effects when eaten.

Making a custom food item is almost the same as making a normal item, except you extend ItemFood instead of Item.
Create a new class in the items package, I will call it ItemCustomFood. Create a constructor that takes a String for the name, an int for the amount of food it fills, a float for the saturation and a boolean which is true when a wolf can eat it.

In the constructor call super with the amount, saturation and whether it is wolf food. Also call setUnlocalizedName and setRegistryName just like a normal item.

Now we can add it to our ModItems class. This is almost the same as with a normal item, except you use ItemCustomFood instead of ItemBasic. You also have to pass the custom food’s values like: The amount it fills, the staturation and whether a wolf can eat it. I will also set the creativeTab for it. I called mine tutorialApple.

 tutorialApple = new ItemCustomFood("tutorialApple", 5, 0.3f, false).setCreativeTab(CreativeTabs.FOOD); 

Don’t forget to register it just like the other items! Adding a model/texture is exactly the same as with a normal item.

An food item that gives you a custom effect (like an golden apple) is quite easy once you created a food item. Create a new class, I will call it ItemEffectFood and make it extend your ItemCustomFood class. Add the constructor and call the super constructor. If you want to make your food edible when the player has no hunger, call setAlwaysEdible(). Now to add the effects when eaten you have to override the onFoodEaten method:

The first thing we should do is check whether this is the client or the server calling the method, we should only add the effects when worldIn.isRemote is false. When you add effects on the client side desyncs can happen. After the check add an effect to the player by calling addPotionEffect on the player object. addPotionEffect takes a PotionEffect, create a new PotionEffect object, this takes an effect, the time it will last, the amplifier, whether it is a beacon effect and if the particles should show or not. For a vanilla effect you can get one from MobEffects. For example: MobEffects.JUMP_BOOST.

For more effects just call addPotionEffect again.

To make it glow just like an golden apple or an enchanted item override hasEffect. But this only exists on the client side so we have to add @SideOnly(Side.CLIENT). This makes sure this doesn’t get activated on the server. After overriding the method just return true and your item will have the “glow” effect.

Now you can just instantiate it like we did before but now with ItemEffectFood instead of ItemCustomFood.
Adding a texture and model is the same as with a normal item. I will use the same texture for both Items:

Classes


[collapse]

That was everything for this part, I know this part is a week too late but school started again so I have less time. As always if you have any question don’t hesitate to ask in the comments! Also if you saw any errors in the tutorial please let me know so I can fix it!

~suppergerrie2

Posted in Forge tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *