001package net.minecraft.potion;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.ArrayList;
006import java.util.Collection;
007import java.util.HashMap;
008import java.util.Iterator;
009import java.util.List;
010
011public class PotionHelper
012{
013    public static final String field_77924_a = null;
014    public static final String sugarEffect;
015    public static final String ghastTearEffect = "+0-1-2-3&4-4+13";
016    public static final String spiderEyeEffect;
017    public static final String fermentedSpiderEyeEffect;
018    public static final String speckledMelonEffect;
019    public static final String blazePowderEffect;
020    public static final String magmaCreamEffect;
021    public static final String redstoneEffect;
022    public static final String glowstoneEffect;
023    public static final String gunpowderEffect;
024    public static final String field_82818_l;
025    private static final HashMap potionRequirements = new HashMap();
026
027    /** Potion effect amplifier map */
028    private static final HashMap potionAmplifiers = new HashMap();
029    private static final HashMap field_77925_n;
030
031    /** An array of possible potion prefix names, as translation IDs. */
032    private static final String[] potionPrefixes;
033
034    /**
035     * Is the bit given set to 1?
036     */
037    public static boolean checkFlag(int par0, int par1)
038    {
039        return (par0 & 1 << par1) != 0;
040    }
041
042    /**
043     * Returns 1 if the flag is set, 0 if it is not set.
044     */
045    private static int isFlagSet(int par0, int par1)
046    {
047        return checkFlag(par0, par1) ? 1 : 0;
048    }
049
050    /**
051     * Returns 0 if the flag is set, 1 if it is not set.
052     */
053    private static int isFlagUnset(int par0, int par1)
054    {
055        return checkFlag(par0, par1) ? 0 : 1;
056    }
057
058    public static int func_77909_a(int par0)
059    {
060        return func_77908_a(par0, 5, 4, 3, 2, 1);
061    }
062
063    /**
064     * Given a {@link Collection}<{@link PotionEffect}> will return an Integer color.
065     */
066    public static int calcPotionLiquidColor(Collection par0Collection)
067    {
068        int i = 3694022;
069
070        if (par0Collection != null && !par0Collection.isEmpty())
071        {
072            float f = 0.0F;
073            float f1 = 0.0F;
074            float f2 = 0.0F;
075            float f3 = 0.0F;
076            Iterator iterator = par0Collection.iterator();
077
078            while (iterator.hasNext())
079            {
080                PotionEffect potioneffect = (PotionEffect)iterator.next();
081                int j = Potion.potionTypes[potioneffect.getPotionID()].getLiquidColor();
082
083                for (int k = 0; k <= potioneffect.getAmplifier(); ++k)
084                {
085                    f += (float)(j >> 16 & 255) / 255.0F;
086                    f1 += (float)(j >> 8 & 255) / 255.0F;
087                    f2 += (float)(j >> 0 & 255) / 255.0F;
088                    ++f3;
089                }
090            }
091
092            f = f / f3 * 255.0F;
093            f1 = f1 / f3 * 255.0F;
094            f2 = f2 / f3 * 255.0F;
095            return (int)f << 16 | (int)f1 << 8 | (int)f2;
096        }
097        else
098        {
099            return i;
100        }
101    }
102
103    public static boolean func_82817_b(Collection par0Collection)
104    {
105        Iterator iterator = par0Collection.iterator();
106        PotionEffect potioneffect;
107
108        do
109        {
110            if (!iterator.hasNext())
111            {
112                return true;
113            }
114
115            potioneffect = (PotionEffect)iterator.next();
116        }
117        while (potioneffect.getIsAmbient());
118
119        return false;
120    }
121
122    @SideOnly(Side.CLIENT)
123    public static int func_77915_a(int par0, boolean par1)
124    {
125        if (!par1)
126        {
127            if (field_77925_n.containsKey(Integer.valueOf(par0)))
128            {
129                return ((Integer)field_77925_n.get(Integer.valueOf(par0))).intValue();
130            }
131            else
132            {
133                int j = calcPotionLiquidColor(getPotionEffects(par0, false));
134                field_77925_n.put(Integer.valueOf(par0), Integer.valueOf(j));
135                return j;
136            }
137        }
138        else
139        {
140            return calcPotionLiquidColor(getPotionEffects(par0, par1));
141        }
142    }
143
144    public static String func_77905_c(int par0)
145    {
146        int j = func_77909_a(par0);
147        return potionPrefixes[j];
148    }
149
150    private static int func_77904_a(boolean par0, boolean par1, boolean par2, int par3, int par4, int par5, int par6)
151    {
152        int i1 = 0;
153
154        if (par0)
155        {
156            i1 = isFlagUnset(par6, par4);
157        }
158        else if (par3 != -1)
159        {
160            if (par3 == 0 && countSetFlags(par6) == par4)
161            {
162                i1 = 1;
163            }
164            else if (par3 == 1 && countSetFlags(par6) > par4)
165            {
166                i1 = 1;
167            }
168            else if (par3 == 2 && countSetFlags(par6) < par4)
169            {
170                i1 = 1;
171            }
172        }
173        else
174        {
175            i1 = isFlagSet(par6, par4);
176        }
177
178        if (par1)
179        {
180            i1 *= par5;
181        }
182
183        if (par2)
184        {
185            i1 *= -1;
186        }
187
188        return i1;
189    }
190
191    /**
192     * Count the number of bits in an integer set to ON.
193     */
194    private static int countSetFlags(int par0)
195    {
196        int j;
197
198        for (j = 0; par0 > 0; ++j)
199        {
200            par0 &= par0 - 1;
201        }
202
203        return j;
204    }
205
206    private static int parsePotionEffects(String par0Str, int par1, int par2, int par3)
207    {
208        if (par1 < par0Str.length() && par2 >= 0 && par1 < par2)
209        {
210            int l = par0Str.indexOf(124, par1);
211            int i1;
212            int j1;
213
214            if (l >= 0 && l < par2)
215            {
216                i1 = parsePotionEffects(par0Str, par1, l - 1, par3);
217
218                if (i1 > 0)
219                {
220                    return i1;
221                }
222                else
223                {
224                    j1 = parsePotionEffects(par0Str, l + 1, par2, par3);
225                    return j1 > 0 ? j1 : 0;
226                }
227            }
228            else
229            {
230                i1 = par0Str.indexOf(38, par1);
231
232                if (i1 >= 0 && i1 < par2)
233                {
234                    j1 = parsePotionEffects(par0Str, par1, i1 - 1, par3);
235
236                    if (j1 <= 0)
237                    {
238                        return 0;
239                    }
240                    else
241                    {
242                        int k1 = parsePotionEffects(par0Str, i1 + 1, par2, par3);
243                        return k1 <= 0 ? 0 : (j1 > k1 ? j1 : k1);
244                    }
245                }
246                else
247                {
248                    boolean flag = false;
249                    boolean flag1 = false;
250                    boolean flag2 = false;
251                    boolean flag3 = false;
252                    boolean flag4 = false;
253                    byte b0 = -1;
254                    int l1 = 0;
255                    int i2 = 0;
256                    int j2 = 0;
257
258                    for (int k2 = par1; k2 < par2; ++k2)
259                    {
260                        char c0 = par0Str.charAt(k2);
261
262                        if (c0 >= 48 && c0 <= 57)
263                        {
264                            if (flag)
265                            {
266                                i2 = c0 - 48;
267                                flag1 = true;
268                            }
269                            else
270                            {
271                                l1 *= 10;
272                                l1 += c0 - 48;
273                                flag2 = true;
274                            }
275                        }
276                        else if (c0 == 42)
277                        {
278                            flag = true;
279                        }
280                        else if (c0 == 33)
281                        {
282                            if (flag2)
283                            {
284                                j2 += func_77904_a(flag3, flag1, flag4, b0, l1, i2, par3);
285                                flag3 = false;
286                                flag4 = false;
287                                flag = false;
288                                flag1 = false;
289                                flag2 = false;
290                                i2 = 0;
291                                l1 = 0;
292                                b0 = -1;
293                            }
294
295                            flag3 = true;
296                        }
297                        else if (c0 == 45)
298                        {
299                            if (flag2)
300                            {
301                                j2 += func_77904_a(flag3, flag1, flag4, b0, l1, i2, par3);
302                                flag3 = false;
303                                flag4 = false;
304                                flag = false;
305                                flag1 = false;
306                                flag2 = false;
307                                i2 = 0;
308                                l1 = 0;
309                                b0 = -1;
310                            }
311
312                            flag4 = true;
313                        }
314                        else if (c0 != 61 && c0 != 60 && c0 != 62)
315                        {
316                            if (c0 == 43 && flag2)
317                            {
318                                j2 += func_77904_a(flag3, flag1, flag4, b0, l1, i2, par3);
319                                flag3 = false;
320                                flag4 = false;
321                                flag = false;
322                                flag1 = false;
323                                flag2 = false;
324                                i2 = 0;
325                                l1 = 0;
326                                b0 = -1;
327                            }
328                        }
329                        else
330                        {
331                            if (flag2)
332                            {
333                                j2 += func_77904_a(flag3, flag1, flag4, b0, l1, i2, par3);
334                                flag3 = false;
335                                flag4 = false;
336                                flag = false;
337                                flag1 = false;
338                                flag2 = false;
339                                i2 = 0;
340                                l1 = 0;
341                                b0 = -1;
342                            }
343
344                            if (c0 == 61)
345                            {
346                                b0 = 0;
347                            }
348                            else if (c0 == 60)
349                            {
350                                b0 = 2;
351                            }
352                            else if (c0 == 62)
353                            {
354                                b0 = 1;
355                            }
356                        }
357                    }
358
359                    if (flag2)
360                    {
361                        j2 += func_77904_a(flag3, flag1, flag4, b0, l1, i2, par3);
362                    }
363
364                    return j2;
365                }
366            }
367        }
368        else
369        {
370            return 0;
371        }
372    }
373
374    /**
375     * Returns a list of effects for the specified potion damage value.
376     */
377    public static List getPotionEffects(int par0, boolean par1)
378    {
379        ArrayList arraylist = null;
380        Potion[] apotion = Potion.potionTypes;
381        int j = apotion.length;
382
383        for (int k = 0; k < j; ++k)
384        {
385            Potion potion = apotion[k];
386
387            if (potion != null && (!potion.isUsable() || par1))
388            {
389                String s = (String)potionRequirements.get(Integer.valueOf(potion.getId()));
390
391                if (s != null)
392                {
393                    int l = parsePotionEffects(s, 0, s.length(), par0);
394
395                    if (l > 0)
396                    {
397                        int i1 = 0;
398                        String s1 = (String)potionAmplifiers.get(Integer.valueOf(potion.getId()));
399
400                        if (s1 != null)
401                        {
402                            i1 = parsePotionEffects(s1, 0, s1.length(), par0);
403
404                            if (i1 < 0)
405                            {
406                                i1 = 0;
407                            }
408                        }
409
410                        if (potion.isInstant())
411                        {
412                            l = 1;
413                        }
414                        else
415                        {
416                            l = 1200 * (l * 3 + (l - 1) * 2);
417                            l >>= i1;
418                            l = (int)Math.round((double)l * potion.getEffectiveness());
419
420                            if ((par0 & 16384) != 0)
421                            {
422                                l = (int)Math.round((double)l * 0.75D + 0.5D);
423                            }
424                        }
425
426                        if (arraylist == null)
427                        {
428                            arraylist = new ArrayList();
429                        }
430
431                        PotionEffect potioneffect = new PotionEffect(potion.getId(), l, i1);
432
433                        if ((par0 & 16384) != 0)
434                        {
435                            potioneffect.setSplashPotion(true);
436                        }
437
438                        arraylist.add(potioneffect);
439                    }
440                }
441            }
442        }
443
444        return arraylist;
445    }
446
447    /**
448     * Does bit operations for brewPotionData, given data, the index of the bit being operated upon, whether the bit
449     * will be removed, whether the bit will be toggled (NOT), or whether the data field will be set to 0 if the bit is
450     * not present.
451     */
452    private static int brewBitOperations(int par0, int par1, boolean par2, boolean par3, boolean par4)
453    {
454        if (par4)
455        {
456            if (!checkFlag(par0, par1))
457            {
458                return 0;
459            }
460        }
461        else if (par2)
462        {
463            par0 &= ~(1 << par1);
464        }
465        else if (par3)
466        {
467            if ((par0 & 1 << par1) == 0)
468            {
469                par0 |= 1 << par1;
470            }
471            else
472            {
473                par0 &= ~(1 << par1);
474            }
475        }
476        else
477        {
478            par0 |= 1 << par1;
479        }
480
481        return par0;
482    }
483
484    /**
485     * Generate a data value for a potion, given its previous data value and the encoded string of new effects it will
486     * receive
487     */
488    public static int applyIngredient(int par0, String par1Str)
489    {
490        byte b0 = 0;
491        int j = par1Str.length();
492        boolean flag = false;
493        boolean flag1 = false;
494        boolean flag2 = false;
495        boolean flag3 = false;
496        int k = 0;
497
498        for (int l = b0; l < j; ++l)
499        {
500            char c0 = par1Str.charAt(l);
501
502            if (c0 >= 48 && c0 <= 57)
503            {
504                k *= 10;
505                k += c0 - 48;
506                flag = true;
507            }
508            else if (c0 == 33)
509            {
510                if (flag)
511                {
512                    par0 = brewBitOperations(par0, k, flag2, flag1, flag3);
513                    flag3 = false;
514                    flag1 = false;
515                    flag2 = false;
516                    flag = false;
517                    k = 0;
518                }
519
520                flag1 = true;
521            }
522            else if (c0 == 45)
523            {
524                if (flag)
525                {
526                    par0 = brewBitOperations(par0, k, flag2, flag1, flag3);
527                    flag3 = false;
528                    flag1 = false;
529                    flag2 = false;
530                    flag = false;
531                    k = 0;
532                }
533
534                flag2 = true;
535            }
536            else if (c0 == 43)
537            {
538                if (flag)
539                {
540                    par0 = brewBitOperations(par0, k, flag2, flag1, flag3);
541                    flag3 = false;
542                    flag1 = false;
543                    flag2 = false;
544                    flag = false;
545                    k = 0;
546                }
547            }
548            else if (c0 == 38)
549            {
550                if (flag)
551                {
552                    par0 = brewBitOperations(par0, k, flag2, flag1, flag3);
553                    flag3 = false;
554                    flag1 = false;
555                    flag2 = false;
556                    flag = false;
557                    k = 0;
558                }
559
560                flag3 = true;
561            }
562        }
563
564        if (flag)
565        {
566            par0 = brewBitOperations(par0, k, flag2, flag1, flag3);
567        }
568
569        return par0 & 32767;
570    }
571
572    public static int func_77908_a(int par0, int par1, int par2, int par3, int par4, int par5)
573    {
574        return (checkFlag(par0, par1) ? 16 : 0) | (checkFlag(par0, par2) ? 8 : 0) | (checkFlag(par0, par3) ? 4 : 0) | (checkFlag(par0, par4) ? 2 : 0) | (checkFlag(par0, par5) ? 1 : 0);
575    }
576
577    static
578    {
579        potionRequirements.put(Integer.valueOf(Potion.regeneration.getId()), "0 & !1 & !2 & !3 & 0+6");
580        sugarEffect = "-0+1-2-3&4-4+13";
581        potionRequirements.put(Integer.valueOf(Potion.moveSpeed.getId()), "!0 & 1 & !2 & !3 & 1+6");
582        magmaCreamEffect = "+0+1-2-3&4-4+13";
583        potionRequirements.put(Integer.valueOf(Potion.fireResistance.getId()), "0 & 1 & !2 & !3 & 0+6");
584        speckledMelonEffect = "+0-1+2-3&4-4+13";
585        potionRequirements.put(Integer.valueOf(Potion.heal.getId()), "0 & !1 & 2 & !3");
586        spiderEyeEffect = "-0-1+2-3&4-4+13";
587        potionRequirements.put(Integer.valueOf(Potion.poison.getId()), "!0 & !1 & 2 & !3 & 2+6");
588        fermentedSpiderEyeEffect = "-0+3-4+13";
589        potionRequirements.put(Integer.valueOf(Potion.weakness.getId()), "!0 & !1 & !2 & 3 & 3+6");
590        potionRequirements.put(Integer.valueOf(Potion.harm.getId()), "!0 & !1 & 2 & 3");
591        potionRequirements.put(Integer.valueOf(Potion.moveSlowdown.getId()), "!0 & 1 & !2 & 3 & 3+6");
592        blazePowderEffect = "+0-1-2+3&4-4+13";
593        potionRequirements.put(Integer.valueOf(Potion.damageBoost.getId()), "0 & !1 & !2 & 3 & 3+6");
594        field_82818_l = "-0+1+2-3+13&4-4";
595        potionRequirements.put(Integer.valueOf(Potion.nightVision.getId()), "!0 & 1 & 2 & !3 & 2+6");
596        potionRequirements.put(Integer.valueOf(Potion.invisibility.getId()), "!0 & 1 & 2 & 3 & 2+6");
597        glowstoneEffect = "+5-6-7";
598        potionAmplifiers.put(Integer.valueOf(Potion.moveSpeed.getId()), "5");
599        potionAmplifiers.put(Integer.valueOf(Potion.digSpeed.getId()), "5");
600        potionAmplifiers.put(Integer.valueOf(Potion.damageBoost.getId()), "5");
601        potionAmplifiers.put(Integer.valueOf(Potion.regeneration.getId()), "5");
602        potionAmplifiers.put(Integer.valueOf(Potion.harm.getId()), "5");
603        potionAmplifiers.put(Integer.valueOf(Potion.heal.getId()), "5");
604        potionAmplifiers.put(Integer.valueOf(Potion.resistance.getId()), "5");
605        potionAmplifiers.put(Integer.valueOf(Potion.poison.getId()), "5");
606        redstoneEffect = "-5+6-7";
607        gunpowderEffect = "+14&13-13";
608        field_77925_n = new HashMap();
609        potionPrefixes = new String[] {"potion.prefix.mundane", "potion.prefix.uninteresting", "potion.prefix.bland", "potion.prefix.clear", "potion.prefix.milky", "potion.prefix.diffuse", "potion.prefix.artless", "potion.prefix.thin", "potion.prefix.awkward", "potion.prefix.flat", "potion.prefix.bulky", "potion.prefix.bungling", "potion.prefix.buttered", "potion.prefix.smooth", "potion.prefix.suave", "potion.prefix.debonair", "potion.prefix.thick", "potion.prefix.elegant", "potion.prefix.fancy", "potion.prefix.charming", "potion.prefix.dashing", "potion.prefix.refined", "potion.prefix.cordial", "potion.prefix.sparkling", "potion.prefix.potent", "potion.prefix.foul", "potion.prefix.odorless", "potion.prefix.rank", "potion.prefix.harsh", "potion.prefix.acrid", "potion.prefix.gross", "potion.prefix.stinky"};
610    }
611}