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.setTabToDisplayOn(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        public boolean tryPlaceIntoWorld(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
131        {
132            return false;
133        }
134    
135        @SideOnly(Side.CLIENT)
136    
137        /**
138         * Gets an icon index based on an item's damage value
139         */
140        public int getIconFromDamage(int par1)
141        {
142            return isSplash(par1) ? 154 : 140;
143        }
144    
145        @SideOnly(Side.CLIENT)
146    
147        /**
148         * Gets an icon index based on an item's damage value and the given render pass
149         */
150        public int getIconFromDamageForRenderPass(int par1, int par2)
151        {
152            return par2 == 0 ? 141 : super.getIconFromDamageForRenderPass(par1, par2);
153        }
154    
155        /**
156         * returns wether or not a potion is a throwable splash potion based on damage value
157         */
158        public static boolean isSplash(int par0)
159        {
160            return (par0 & 16384) != 0;
161        }
162    
163        @SideOnly(Side.CLIENT)
164        public int getColorFromDamage(int par1, int par2)
165        {
166            return par2 > 0 ? 16777215 : PotionHelper.func_77915_a(par1, false);
167        }
168    
169        @SideOnly(Side.CLIENT)
170        public boolean requiresMultipleRenderPasses()
171        {
172            return true;
173        }
174    
175        @SideOnly(Side.CLIENT)
176        public boolean isEffectInstant(int par1)
177        {
178            List var2 = this.getEffects(par1);
179    
180            if (var2 != null && !var2.isEmpty())
181            {
182                Iterator var3 = var2.iterator();
183                PotionEffect var4;
184    
185                do
186                {
187                    if (!var3.hasNext())
188                    {
189                        return false;
190                    }
191    
192                    var4 = (PotionEffect)var3.next();
193                }
194                while (!Potion.potionTypes[var4.getPotionID()].isInstant());
195    
196                return true;
197            }
198            else
199            {
200                return false;
201            }
202        }
203    
204        @SideOnly(Side.CLIENT)
205        public String getItemDisplayName(ItemStack par1ItemStack)
206        {
207            if (par1ItemStack.getItemDamage() == 0)
208            {
209                return StatCollector.translateToLocal("item.emptyPotion.name").trim();
210            }
211            else
212            {
213                String var2 = "";
214    
215                if (isSplash(par1ItemStack.getItemDamage()))
216                {
217                    var2 = StatCollector.translateToLocal("potion.prefix.grenade").trim() + " ";
218                }
219    
220                List var3 = Item.potion.getEffects(par1ItemStack);
221                String var4;
222    
223                if (var3 != null && !var3.isEmpty())
224                {
225                    var4 = ((PotionEffect)var3.get(0)).getEffectName();
226                    var4 = var4 + ".postfix";
227                    return var2 + StatCollector.translateToLocal(var4).trim();
228                }
229                else
230                {
231                    var4 = PotionHelper.func_77905_c(par1ItemStack.getItemDamage());
232                    return StatCollector.translateToLocal(var4).trim() + " " + super.getItemDisplayName(par1ItemStack);
233                }
234            }
235        }
236    
237        @SideOnly(Side.CLIENT)
238    
239        /**
240         * allows items to add custom lines of information to the mouseover description
241         */
242        public void addInformation(ItemStack par1ItemStack, List par2List)
243        {
244            if (par1ItemStack.getItemDamage() != 0)
245            {
246                List var3 = Item.potion.getEffects(par1ItemStack);
247    
248                if (var3 != null && !var3.isEmpty())
249                {
250                    Iterator var7 = var3.iterator();
251    
252                    while (var7.hasNext())
253                    {
254                        PotionEffect var5 = (PotionEffect)var7.next();
255                        String var6 = StatCollector.translateToLocal(var5.getEffectName()).trim();
256    
257                        if (var5.getAmplifier() > 0)
258                        {
259                            var6 = var6 + " " + StatCollector.translateToLocal("potion.potency." + var5.getAmplifier()).trim();
260                        }
261    
262                        if (var5.getDuration() > 20)
263                        {
264                            var6 = var6 + " (" + Potion.getDurationString(var5) + ")";
265                        }
266    
267                        if (Potion.potionTypes[var5.getPotionID()].isBadEffect())
268                        {
269                            par2List.add("\u00a7c" + var6);
270                        }
271                        else
272                        {
273                            par2List.add("\u00a77" + var6);
274                        }
275                    }
276                }
277                else
278                {
279                    String var4 = StatCollector.translateToLocal("potion.empty").trim();
280                    par2List.add("\u00a77" + var4);
281                }
282            }
283        }
284    
285        @SideOnly(Side.CLIENT)
286        public boolean hasEffect(ItemStack par1ItemStack)
287        {
288            List var2 = this.getEffects(par1ItemStack);
289            return var2 != null && !var2.isEmpty();
290        }
291    
292        @SideOnly(Side.CLIENT)
293    
294        /**
295         * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
296         */
297        public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
298        {
299            super.getSubItems(par1, par2CreativeTabs, par3List);
300    
301            if (field_77835_b.isEmpty())
302            {
303                for (int var4 = 0; var4 <= 32767; ++var4)
304                {
305                    List var5 = PotionHelper.getPotionEffects(var4, false);
306    
307                    if (var5 != null && !var5.isEmpty())
308                    {
309                        field_77835_b.put(var5, Integer.valueOf(var4));
310                    }
311                }
312            }
313    
314            Iterator var6 = field_77835_b.values().iterator();
315    
316            while (var6.hasNext())
317            {
318                int var7 = ((Integer)var6.next()).intValue();
319                par3List.add(new ItemStack(par1, 1, var7));
320            }
321        }
322    }