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