    001    package net.minecraft.src;
    002
    003    public class ItemFood extends Item
    004    {
    005        /** Number of ticks to run while 'EnumAction'ing until result. */
    006        public final int itemUseDuration;
    007
    008        /** The amount this food item heals the player. */
    009        private final int healAmount;
    010        private final float saturationModifier;
    011
    012        /** Whether wolves like this food (true for raw and cooked porkchop). */
    013        private final boolean isWolfsFavoriteMeat;
    014
    015        /**
    016         * If this field is true, the food can be consumed even if the player don't need to eat.
    017         */
    018        private boolean alwaysEdible;
    019
    020        /**
    021         * represents the potion effect that will occurr upon eating this food. Set by setPotionEffect
    022         */
    023        private int potionId;
    024
    025        /** set by setPotionEffect */
    026        private int potionDuration;
    027
    028        /** set by setPotionEffect */
    029        private int potionAmplifier;
    030
    031        /** probably of the set potion effect occurring */
    032        private float potionEffectProbability;
    033
    034        public ItemFood(int par1, int par2, float par3, boolean par4)
    035        {
    036            super(par1);
    037            this.itemUseDuration = 32;
    038            this.healAmount = par2;
    039            this.isWolfsFavoriteMeat = par4;
    040            this.saturationModifier = par3;
    041            this.setCreativeTab(CreativeTabs.tabFood);
    042        }
    043
    044        public ItemFood(int par1, int par2, boolean par3)
    045        {
    046            this(par1, par2, 0.6F, par3);
    047        }
    048
    049        public ItemStack onFoodEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    050        {
    051            --par1ItemStack.stackSize;
    052            par3EntityPlayer.getFoodStats().addStats(this);
    053            par2World.playSoundAtEntity(par3EntityPlayer, "random.burp", 0.5F, par2World.rand.nextFloat() * 0.1F + 0.9F);
    054            this.func_77849_c(par1ItemStack, par2World, par3EntityPlayer);
    055            return par1ItemStack;
    056        }
    057
    058        protected void func_77849_c(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    059        {
    060            if (!par2World.isRemote && this.potionId > 0 && par2World.rand.nextFloat() < this.potionEffectProbability)
    061            {
    062                par3EntityPlayer.addPotionEffect(new PotionEffect(this.potionId, this.potionDuration * 20, this.potionAmplifier));
    063            }
    064        }
    065
    066        /**
    067         * How long it takes to use or consume an item
    068         */
    069        public int getMaxItemUseDuration(ItemStack par1ItemStack)
    070        {
    071            return 32;
    072        }
    073
    074        /**
    075         * returns the action that specifies what animation to play when the items is being used
    076         */
    077        public EnumAction getItemUseAction(ItemStack par1ItemStack)
    078        {
    079            return EnumAction.eat;
    080        }
    081
    082        /**
    083         * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
    084         */
    085        public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    086        {
    087            if (par3EntityPlayer.canEat(this.alwaysEdible))
    088            {
    089                par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
    090            }
    091
    092            return par1ItemStack;
    093        }
    094
    095        public int getHealAmount()
    096        {
    097            return this.healAmount;
    098        }
    099
    100        /**
    101         * gets the saturationModifier of the ItemFood
    102         */
    103        public float getSaturationModifier()
    104        {
    105            return this.saturationModifier;
    106        }
    107
    108        /**
    109         * Whether wolves like this food (true for raw and cooked porkchop).
    110         */
    111        public boolean isWolfsFavoriteMeat()
    112        {
    113            return this.isWolfsFavoriteMeat;
    114        }
    115
    116        /**
    117         * sets a potion effect on the item. Args: int potionId, int duration (will be multiplied by 20), int amplifier,
    118         * float probability of effect happening
    119         */
    120        public ItemFood setPotionEffect(int par1, int par2, int par3, float par4)
    121        {
    122            this.potionId = par1;
    123            this.potionDuration = par2;
    124            this.potionAmplifier = par3;
    125            this.potionEffectProbability = par4;
    126            return this;
    127        }
    128
    129        /**
    130         * Set the field 'alwaysEdible' to true, and make the food edible even if the player don't need to eat.
    131         */
    132        public ItemFood setAlwaysEdible()
    133        {
    134            this.alwaysEdible = true;
    135            return this;
    136        }
    137    }