    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.Iterator;
    006    import java.util.List;
    007
    008    public class EntityPotion extends EntityThrowable
    009    {
    010        /**
    011         * The damage value of the thrown potion that this EntityPotion represents.
    012         */
    013        private ItemStack potionDamage;
    014
    015        public EntityPotion(World par1World)
    016        {
    017            super(par1World);
    018        }
    019
    020        public EntityPotion(World par1World, EntityLiving par2EntityLiving, int par3)
    021        {
    022            this(par1World, par2EntityLiving, new ItemStack(Item.potion, 1, par3));
    023        }
    024
    025        public EntityPotion(World par1World, EntityLiving par2EntityLiving, ItemStack par3ItemStack)
    026        {
    027            super(par1World, par2EntityLiving);
    028            this.potionDamage = par3ItemStack;
    029        }
    030
    031        @SideOnly(Side.CLIENT)
    032        public EntityPotion(World par1World, double par2, double par4, double par6, int par8)
    033        {
    034            this(par1World, par2, par4, par6, new ItemStack(Item.potion, 1, par8));
    035        }
    036
    037        public EntityPotion(World par1World, double par2, double par4, double par6, ItemStack par8ItemStack)
    038        {
    039            super(par1World, par2, par4, par6);
    040            this.potionDamage = par8ItemStack;
    041        }
    042
    043        /**
    044         * Gets the amount of gravity to apply to the thrown entity with each tick.
    045         */
    046        protected float getGravityVelocity()
    047        {
    048            return 0.05F;
    049        }
    050
    051        protected float func_70182_d()
    052        {
    053            return 0.5F;
    054        }
    055
    056        protected float func_70183_g()
    057        {
    058            return -20.0F;
    059        }
    060
    061        public void func_82340_a(int par1)
    062        {
    063            if (this.potionDamage == null)
    064            {
    065                this.potionDamage = new ItemStack(Item.potion, 1, 0);
    066            }
    067
    068            this.potionDamage.setItemDamage(par1);
    069        }
    070
    071        /**
    072         * Returns the damage value of the thrown potion that this EntityPotion represents.
    073         */
    074        public int getPotionDamage()
    075        {
    076            if (this.potionDamage == null)
    077            {
    078                this.potionDamage = new ItemStack(Item.potion, 1, 0);
    079            }
    080
    081            return this.potionDamage.getItemDamage();
    082        }
    083
    084        /**
    085         * Called when this EntityThrowable hits a block or entity.
    086         */
    087        protected void onImpact(MovingObjectPosition par1MovingObjectPosition)
    088        {
    089            if (!this.worldObj.isRemote)
    090            {
    091                List var2 = Item.potion.getEffects(this.potionDamage);
    092
    093                if (var2 != null && !var2.isEmpty())
    094                {
    095                    AxisAlignedBB var3 = this.boundingBox.expand(4.0D, 2.0D, 4.0D);
    096                    List var4 = this.worldObj.getEntitiesWithinAABB(EntityLiving.class, var3);
    097
    098                    if (var4 != null && !var4.isEmpty())
    099                    {
    100                        Iterator var5 = var4.iterator();
    101
    102                        while (var5.hasNext())
    103                        {
    104                            EntityLiving var6 = (EntityLiving)var5.next();
    105                            double var7 = this.getDistanceSqToEntity(var6);
    106
    107                            if (var7 < 16.0D)
    108                            {
    109                                double var9 = 1.0D - Math.sqrt(var7) / 4.0D;
    110
    111                                if (var6 == par1MovingObjectPosition.entityHit)
    112                                {
    113                                    var9 = 1.0D;
    114                                }
    115
    116                                Iterator var11 = var2.iterator();
    117
    118                                while (var11.hasNext())
    119                                {
    120                                    PotionEffect var12 = (PotionEffect)var11.next();
    121                                    int var13 = var12.getPotionID();
    122
    123                                    if (Potion.potionTypes[var13].isInstant())
    124                                    {
    125                                        Potion.potionTypes[var13].affectEntity(this.thrower, var6, var12.getAmplifier(), var9);
    126                                    }
    127                                    else
    128                                    {
    129                                        int var14 = (int)(var9 * (double)var12.getDuration() + 0.5D);
    130
    131                                        if (var14 > 20)
    132                                        {
    133                                            var6.addPotionEffect(new PotionEffect(var13, var14, var12.getAmplifier()));
    134                                        }
    135                                    }
    136                                }
    137                            }
    138                        }
    139                    }
    140                }
    141
    142                this.worldObj.playAuxSFX(2002, (int)Math.round(this.posX), (int)Math.round(this.posY), (int)Math.round(this.posZ), this.getPotionDamage());
    143                this.setDead();
    144            }
    145        }
    146
    147        /**
    148         * (abstract) Protected helper method to read subclass entity data from NBT.
    149         */
    150        public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    151        {
    152            super.readEntityFromNBT(par1NBTTagCompound);
    153
    154            if (par1NBTTagCompound.hasKey("Potion"))
    155            {
    156                this.potionDamage = ItemStack.loadItemStackFromNBT(par1NBTTagCompound.getCompoundTag("Potion"));
    157            }
    158            else
    159            {
    160                this.func_82340_a(par1NBTTagCompound.getInteger("potionValue"));
    161            }
    162
    163            if (this.potionDamage == null)
    164            {
    165                this.setDead();
    166            }
    167        }
    168
    169        /**
    170         * (abstract) Protected helper method to write subclass entity data to NBT.
    171         */
    172        public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    173        {
    174            super.writeEntityToNBT(par1NBTTagCompound);
    175
    176            if (this.potionDamage != null)
    177            {
    178                par1NBTTagCompound.setCompoundTag("Potion", this.potionDamage.writeToNBT(new NBTTagCompound()));
    179            }
    180        }
    181    }