    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.ArrayList;
    006    import java.util.HashMap;
    007    import java.util.Iterator;
    008    import java.util.LinkedHashMap;
    009    import java.util.List;
    010    import java.util.Map;
    011
    012    public class ItemPotion extends Item
    013    {
    014        /** maps potion damage values to lists of effect names */
    015        private HashMap effectCache = new HashMap();
    016        private static final Map field_77835_b = new LinkedHashMap();
    017
    018        public ItemPotion(int par1)
    019        {
    020            super(par1);
    021            this.setMaxStackSize(1);
    022            this.setHasSubtypes(true);
    023            this.setMaxDamage(0);
    024            this.setCreativeTab(CreativeTabs.tabBrewing);
    025        }
    026
    027        /**
    028         * Returns a list of potion effects for the specified itemstack.
    029         */
    030        public List getEffects(ItemStack par1ItemStack)
    031        {
    032            if (par1ItemStack.hasTagCompound() && par1ItemStack.getTagCompound().hasKey("CustomPotionEffects"))
    033            {
    034                ArrayList var6 = new ArrayList();
    035                NBTTagList var3 = par1ItemStack.getTagCompound().getTagList("CustomPotionEffects");
    036
    037                for (int var4 = 0; var4 < var3.tagCount(); ++var4)
    038                {
    039                    NBTTagCompound var5 = (NBTTagCompound)var3.tagAt(var4);
    040                    var6.add(PotionEffect.func_82722_b(var5));
    041                }
    042
    043                return var6;
    044            }
    045            else
    046            {
    047                List var2 = (List)this.effectCache.get(Integer.valueOf(par1ItemStack.getItemDamage()));
    048
    049                if (var2 == null)
    050                {
    051                    var2 = PotionHelper.getPotionEffects(par1ItemStack.getItemDamage(), false);
    052                    this.effectCache.put(Integer.valueOf(par1ItemStack.getItemDamage()), var2);
    053                }
    054
    055                return var2;
    056            }
    057        }
    058
    059        /**
    060         * Returns a list of effects for the specified potion damage value.
    061         */
    062        public List getEffects(int par1)
    063        {
    064            List var2 = (List)this.effectCache.get(Integer.valueOf(par1));
    065
    066            if (var2 == null)
    067            {
    068                var2 = PotionHelper.getPotionEffects(par1, false);
    069                this.effectCache.put(Integer.valueOf(par1), var2);
    070            }
    071
    072            return var2;
    073        }
    074
    075        public ItemStack onFoodEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    076        {
    077            if (!par3EntityPlayer.capabilities.isCreativeMode)
    078            {
    079                --par1ItemStack.stackSize;
    080            }
    081
    082            if (!par2World.isRemote)
    083            {
    084                List var4 = this.getEffects(par1ItemStack);
    085
    086                if (var4 != null)
    087                {
    088                    Iterator var5 = var4.iterator();
    089
    090                    while (var5.hasNext())
    091                    {
    092                        PotionEffect var6 = (PotionEffect)var5.next();
    093                        par3EntityPlayer.addPotionEffect(new PotionEffect(var6));
    094                    }
    095                }
    096            }
    097
    098            if (!par3EntityPlayer.capabilities.isCreativeMode)
    099            {
    100                if (par1ItemStack.stackSize <= 0)
    101                {
    102                    return new ItemStack(Item.glassBottle);
    103                }
    104
    105                par3EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Item.glassBottle));
    106            }
    107
    108            return par1ItemStack;
    109        }
    110
    111        /**
    112         * How long it takes to use or consume an item
    113         */
    114        public int getMaxItemUseDuration(ItemStack par1ItemStack)
    115        {
    116            return 32;
    117        }
    118
    119        /**
    120         * returns the action that specifies what animation to play when the items is being used
    121         */
    122        public EnumAction getItemUseAction(ItemStack par1ItemStack)
    123        {
    124            return EnumAction.drink;
    125        }
    126
    127        /**
    128         * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
    129         */
    130        public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    131        {
    132            if (isSplash(par1ItemStack.getItemDamage()))
    133            {
    134                if (!par3EntityPlayer.capabilities.isCreativeMode)
    135                {
    136                    --par1ItemStack.stackSize;
    137                }
    138
    139                par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
    140
    141                if (!par2World.isRemote)
    142                {
    143                    par2World.spawnEntityInWorld(new EntityPotion(par2World, par3EntityPlayer, par1ItemStack));
    144                }
    145
    146                return par1ItemStack;
    147            }
    148            else
    149            {
    150                par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
    151                return par1ItemStack;
    152            }
    153        }
    154
    155        /**
    156         * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
    157         * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
    158         */
    159        public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    160        {
    161            return false;
    162        }
    163
    164        @SideOnly(Side.CLIENT)
    165
    166        /**
    167         * Gets an icon index based on an item's damage value
    168         */
    169        public int getIconFromDamage(int par1)
    170        {
    171            return isSplash(par1) ? 154 : 140;
    172        }
    173
    174        @SideOnly(Side.CLIENT)
    175
    176        /**
    177         * Gets an icon index based on an item's damage value and the given render pass
    178         */
    179        public int getIconFromDamageForRenderPass(int par1, int par2)
    180        {
    181            return par2 == 0 ? 141 : super.getIconFromDamageForRenderPass(par1, par2);
    182        }
    183
    184        /**
    185         * returns wether or not a potion is a throwable splash potion based on damage value
    186         */
    187        public static boolean isSplash(int par0)
    188        {
    189            return (par0 & 16384) != 0;
    190        }
    191
    192        @SideOnly(Side.CLIENT)
    193        public int getColorFromDamage(int par1)
    194        {
    195            return PotionHelper.func_77915_a(par1, false);
    196        }
    197
    198        @SideOnly(Side.CLIENT)
    199        public int func_82790_a(ItemStack par1ItemStack, int par2)
    200        {
    201            return par2 > 0 ? 16777215 : this.getColorFromDamage(par1ItemStack.getItemDamage());
    202        }
    203
    204        @SideOnly(Side.CLIENT)
    205        public boolean requiresMultipleRenderPasses()
    206        {
    207            return true;
    208        }
    209
    210        @SideOnly(Side.CLIENT)
    211        public boolean isEffectInstant(int par1)
    212        {
    213            List var2 = this.getEffects(par1);
    214
    215            if (var2 != null && !var2.isEmpty())
    216            {
    217                Iterator var3 = var2.iterator();
    218                PotionEffect var4;
    219
    220                do
    221                {
    222                    if (!var3.hasNext())
    223                    {
    224                        return false;
    225                    }
    226
    227                    var4 = (PotionEffect)var3.next();
    228                }
    229                while (!Potion.potionTypes[var4.getPotionID()].isInstant());
    230
    231                return true;
    232            }
    233            else
    234            {
    235                return false;
    236            }
    237        }
    238
    239        public String getItemDisplayName(ItemStack par1ItemStack)
    240        {
    241            if (par1ItemStack.getItemDamage() == 0)
    242            {
    243                return StatCollector.translateToLocal("item.emptyPotion.name").trim();
    244            }
    245            else
    246            {
    247                String var2 = "";
    248
    249                if (isSplash(par1ItemStack.getItemDamage()))
    250                {
    251                    var2 = StatCollector.translateToLocal("potion.prefix.grenade").trim() + " ";
    252                }
    253
    254                List var3 = Item.potion.getEffects(par1ItemStack);
    255                String var4;
    256
    257                if (var3 != null && !var3.isEmpty())
    258                {
    259                    var4 = ((PotionEffect)var3.get(0)).getEffectName();
    260                    var4 = var4 + ".postfix";
    261                    return var2 + StatCollector.translateToLocal(var4).trim();
    262                }
    263                else
    264                {
    265                    var4 = PotionHelper.func_77905_c(par1ItemStack.getItemDamage());
    266                    return StatCollector.translateToLocal(var4).trim() + " " + super.getItemDisplayName(par1ItemStack);
    267                }
    268            }
    269        }
    270
    271        @SideOnly(Side.CLIENT)
    272
    273        /**
    274         * allows items to add custom lines of information to the mouseover description
    275         */
    276        public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
    277        {
    278            if (par1ItemStack.getItemDamage() != 0)
    279            {
    280                List var5 = Item.potion.getEffects(par1ItemStack);
    281
    282                if (var5 != null && !var5.isEmpty())
    283                {
    284                    Iterator var9 = var5.iterator();
    285
    286                    while (var9.hasNext())
    287                    {
    288                        PotionEffect var7 = (PotionEffect)var9.next();
    289                        String var8 = StatCollector.translateToLocal(var7.getEffectName()).trim();
    290
    291                        if (var7.getAmplifier() > 0)
    292                        {
    293                            var8 = var8 + " " + StatCollector.translateToLocal("potion.potency." + var7.getAmplifier()).trim();
    294                        }
    295
    296                        if (var7.getDuration() > 20)
    297                        {
    298                            var8 = var8 + " (" + Potion.getDurationString(var7) + ")";
    299                        }
    300
    301                        if (Potion.potionTypes[var7.getPotionID()].isBadEffect())
    302                        {
    303                            par3List.add("\u00a7c" + var8);
    304                        }
    305                        else
    306                        {
    307                            par3List.add("\u00a77" + var8);
    308                        }
    309                    }
    310                }
    311                else
    312                {
    313                    String var6 = StatCollector.translateToLocal("potion.empty").trim();
    314                    par3List.add("\u00a77" + var6);
    315                }
    316            }
    317        }
    318
    319        @SideOnly(Side.CLIENT)
    320        public boolean hasEffect(ItemStack par1ItemStack)
    321        {
    322            List var2 = this.getEffects(par1ItemStack);
    323            return var2 != null && !var2.isEmpty();
    324        }
    325
    326        @SideOnly(Side.CLIENT)
    327
    328        /**
    329         * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
    330         */
    331        public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
    332        {
    333            super.getSubItems(par1, par2CreativeTabs, par3List);
    334
    335            if (field_77835_b.isEmpty())
    336            {
    337                for (int var4 = 0; var4 <= 32767; ++var4)
    338                {
    339                    List var5 = PotionHelper.getPotionEffects(var4, false);
    340
    341                    if (var5 != null && !var5.isEmpty())
    342                    {
    343                        field_77835_b.put(var5, Integer.valueOf(var4));
    344                    }
    345                }
    346            }
    347
    348            Iterator var6 = field_77835_b.values().iterator();
    349
    350            while (var6.hasNext())
    351            {
    352                int var7 = ((Integer)var6.next()).intValue();
    353                par3List.add(new ItemStack(par1, 1, var7));
    354            }
    355        }
    356    }