    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 list = Item.potion.getEffects(this.potionDamage);
    101
    102            if (list != null && !list.isEmpty())
    103            {
    104                AxisAlignedBB axisalignedbb = this.boundingBox.expand(4.0D, 2.0D, 4.0D);
    105                List list1 = this.worldObj.getEntitiesWithinAABB(EntityLiving.class, axisalignedbb);
    106
    107                if (list1 != null && !list1.isEmpty())
    108                {
    109                    Iterator iterator = list1.iterator();
    110
    111                    while (iterator.hasNext())
    112                    {
    113                        EntityLiving entityliving = (EntityLiving)iterator.next();
    114                        double d0 = this.getDistanceSqToEntity(entityliving);
    115
    116                        if (d0 < 16.0D)
    117                        {
    118                            double d1 = 1.0D - Math.sqrt(d0) / 4.0D;
    119
    120                            if (entityliving == par1MovingObjectPosition.entityHit)
    121                            {
    122                                d1 = 1.0D;
    123                            }
    124
    125                            Iterator iterator1 = list.iterator();
    126
    127                            while (iterator1.hasNext())
    128                            {
    129                                PotionEffect potioneffect = (PotionEffect)iterator1.next();
    130                                int i = potioneffect.getPotionID();
    131
    132                                if (Potion.potionTypes[i].isInstant())
    133                                {
    134                                    Potion.potionTypes[i].affectEntity(this.getThrower(), entityliving, potioneffect.getAmplifier(), d1);
    135                                }
    136                                else
    137                                {
    138                                    int j = (int)(d1 * (double)potioneffect.getDuration() + 0.5D);
    139
    140                                    if (j > 20)
    141                                    {
    142                                        entityliving.addPotionEffect(new PotionEffect(i, j, potioneffect.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}