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