    001    package net.minecraft.src;
    002
    003    import java.util.ArrayList;
    004    import java.util.List;
    005
    006    public class PotionEffect
    007    {
    008        /** ID value of the potion this effect matches. */
    009        private int potionID;
    010
    011        /** The duration of the potion effect */
    012        private int duration;
    013
    014        /** The amplifier of the potion effect */
    015        private int amplifier;
    016        private boolean field_82723_d;
    017        private boolean field_82724_e;
    018
    019        /** List of ItemStack that can cure the potion effect **/
    020        private List<ItemStack> curativeItems;
    021
    022        public PotionEffect(int par1, int par2)
    023        {
    024            this(par1, par2, 0);
    025        }
    026
    027        public PotionEffect(int par1, int par2, int par3)
    028        {
    029            this(par1, par2, par3, false);
    030        }
    031
    032        public PotionEffect(int par1, int par2, int par3, boolean par4)
    033        {
    034            this.potionID = par1;
    035            this.duration = par2;
    036            this.amplifier = par3;
    037            this.field_82724_e = par4;
    038            this.curativeItems = new ArrayList<ItemStack>();
    039            this.curativeItems.add(new ItemStack(Item.bucketMilk));
    040        }
    041
    042        public PotionEffect(PotionEffect par1PotionEffect)
    043        {
    044            this.potionID = par1PotionEffect.potionID;
    045            this.duration = par1PotionEffect.duration;
    046            this.amplifier = par1PotionEffect.amplifier;
    047            this.curativeItems = par1PotionEffect.getCurativeItems();
    048        }
    049
    050        /**
    051         * merges the input PotionEffect into this one if this.amplifier <= tomerge.amplifier. The duration in the supplied
    052         * potion effect is assumed to be greater.
    053         */
    054        public void combine(PotionEffect par1PotionEffect)
    055        {
    056            if (this.potionID != par1PotionEffect.potionID)
    057            {
    058                System.err.println("This method should only be called for matching effects!");
    059            }
    060
    061            if (par1PotionEffect.amplifier > this.amplifier)
    062            {
    063                this.amplifier = par1PotionEffect.amplifier;
    064                this.duration = par1PotionEffect.duration;
    065            }
    066            else if (par1PotionEffect.amplifier == this.amplifier && this.duration < par1PotionEffect.duration)
    067            {
    068                this.duration = par1PotionEffect.duration;
    069            }
    070            else if (!par1PotionEffect.field_82724_e && this.field_82724_e)
    071            {
    072                this.field_82724_e = par1PotionEffect.field_82724_e;
    073            }
    074        }
    075
    076        /**
    077         * Retrieve the ID of the potion this effect matches.
    078         */
    079        public int getPotionID()
    080        {
    081            return this.potionID;
    082        }
    083
    084        public int getDuration()
    085        {
    086            return this.duration;
    087        }
    088
    089        public int getAmplifier()
    090        {
    091            return this.amplifier;
    092        }
    093
    094        /***
    095         * Returns a list of curative items for the potion effect
    096         * @return The list (ItemStack) of curative items for the potion effect
    097         */
    098        public List<ItemStack> getCurativeItems()
    099        {
    100            return this.curativeItems;
    101        }
    102
    103        /***
    104         * Checks the given ItemStack to see if it is in the list of curative items for the potion effect
    105         * @param stack The ItemStack being checked against the list of curative items for the potion effect
    106         * @return true if the given ItemStack is in the list of curative items for the potion effect, false otherwise
    107         */
    108        public boolean isCurativeItem(ItemStack stack)
    109        {
    110            boolean found = false;
    111            for (ItemStack curativeItem : this.curativeItems)
    112            {
    113                if (curativeItem.isItemEqual(stack))
    114                {
    115                    found = true;
    116                }
    117            }
    118
    119            return found;
    120        }
    121
    122        /***
    123         * Sets the array of curative items for the potion effect
    124         * @param curativeItems The list of ItemStacks being set to the potion effect
    125         */
    126        public void setCurativeItems(List<ItemStack> curativeItems)
    127        {
    128            this.curativeItems = curativeItems;
    129        }
    130
    131        /***
    132         * Adds the given stack to list of curative items for the potion effect
    133         * @param stack The ItemStack being added to the curative item list
    134         */
    135        public void addCurativeItem(ItemStack stack)
    136        {
    137            boolean found = false;
    138            for (ItemStack curativeItem : this.curativeItems)
    139            {
    140                if (curativeItem.isItemEqual(stack))
    141                {
    142                    found = true;
    143                }
    144            }
    145            if (!found)
    146            {
    147                this.curativeItems.add(stack);
    148            }
    149        }
    150
    151        /**
    152         * Set whether this potion is a splash potion.
    153         */
    154        public void setSplashPotion(boolean par1)
    155        {
    156            this.field_82723_d = par1;
    157        }
    158
    159        public boolean func_82720_e()
    160        {
    161            return this.field_82724_e;
    162        }
    163
    164        public boolean onUpdate(EntityLiving par1EntityLiving)
    165        {
    166            if (this.duration > 0)
    167            {
    168                if (Potion.potionTypes[this.potionID].isReady(this.duration, this.amplifier))
    169                {
    170                    this.performEffect(par1EntityLiving);
    171                }
    172
    173                this.deincrementDuration();
    174            }
    175
    176            return this.duration > 0;
    177        }
    178
    179        private int deincrementDuration()
    180        {
    181            return --this.duration;
    182        }
    183
    184        public void performEffect(EntityLiving par1EntityLiving)
    185        {
    186            if (this.duration > 0)
    187            {
    188                Potion.potionTypes[this.potionID].performEffect(par1EntityLiving, this.amplifier);
    189            }
    190        }
    191
    192        public String getEffectName()
    193        {
    194            return Potion.potionTypes[this.potionID].getName();
    195        }
    196
    197        public int hashCode()
    198        {
    199            return this.potionID;
    200        }
    201
    202        public String toString()
    203        {
    204            String var1 = "";
    205
    206            if (this.getAmplifier() > 0)
    207            {
    208                var1 = this.getEffectName() + " x " + (this.getAmplifier() + 1) + ", Duration: " + this.getDuration();
    209            }
    210            else
    211            {
    212                var1 = this.getEffectName() + ", Duration: " + this.getDuration();
    213            }
    214
    215            if (this.field_82723_d)
    216            {
    217                var1 = var1 + ", Splash: true";
    218            }
    219
    220            return Potion.potionTypes[this.potionID].isUsable() ? "(" + var1 + ")" : var1;
    221        }
    222
    223        public boolean equals(Object par1Obj)
    224        {
    225            if (!(par1Obj instanceof PotionEffect))
    226            {
    227                return false;
    228            }
    229            else
    230            {
    231                PotionEffect var2 = (PotionEffect)par1Obj;
    232                return this.potionID == var2.potionID && this.amplifier == var2.amplifier && this.duration == var2.duration && this.field_82723_d == var2.field_82723_d && this.field_82724_e == var2.field_82724_e;
    233            }
    234        }
    235
    236        /**
    237         * Write a custom potion effect to a potion item's NBT data.
    238         */
    239        public NBTTagCompound writeCustomPotionEffectToNBT(NBTTagCompound par1NBTTagCompound)
    240        {
    241            par1NBTTagCompound.setByte("Id", (byte)this.getPotionID());
    242            par1NBTTagCompound.setByte("Amplifier", (byte)this.getAmplifier());
    243            par1NBTTagCompound.setInteger("Duration", this.getDuration());
    244            par1NBTTagCompound.setBoolean("Ambient", this.func_82720_e());
    245            return par1NBTTagCompound;
    246        }
    247
    248        /**
    249         * Read a custom potion effect from a potion item's NBT data.
    250         */
    251        public static PotionEffect readCustomPotionEffectFromNBT(NBTTagCompound par0NBTTagCompound)
    252        {
    253            byte var1 = par0NBTTagCompound.getByte("Id");
    254            byte var2 = par0NBTTagCompound.getByte("Amplifier");
    255            int var3 = par0NBTTagCompound.getInteger("Duration");
    256            boolean var4 = par0NBTTagCompound.getBoolean("Ambient");
    257            return new PotionEffect(var1, var3, var2, var4);
    258        }
    259    }