    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 var1 = 3694022;
    069
    070        if (par0Collection != null && !par0Collection.isEmpty())
    071        {
    072            float var2 = 0.0F;
    073            float var3 = 0.0F;
    074            float var4 = 0.0F;
    075            float var5 = 0.0F;
    076            Iterator var6 = par0Collection.iterator();
    077
    078            while (var6.hasNext())
    079            {
    080                PotionEffect var7 = (PotionEffect)var6.next();
    081                int var8 = Potion.potionTypes[var7.getPotionID()].getLiquidColor();
    082
    083                for (int var9 = 0; var9 <= var7.getAmplifier(); ++var9)
    084                {
    085                    var2 += (float)(var8 >> 16 & 255) / 255.0F;
    086                    var3 += (float)(var8 >> 8 & 255) / 255.0F;
    087                    var4 += (float)(var8 >> 0 & 255) / 255.0F;
    088                    ++var5;
    089                }
    090            }
    091
    092            var2 = var2 / var5 * 255.0F;
    093            var3 = var3 / var5 * 255.0F;
    094            var4 = var4 / var5 * 255.0F;
    095            return (int)var2 << 16 | (int)var3 << 8 | (int)var4;
    096        }
    097        else
    098        {
    099            return var1;
    100        }
    101    }
    102
    103    public static boolean func_82817_b(Collection par0Collection)
    104    {
    105        Iterator var1 = par0Collection.iterator();
    106        PotionEffect var2;
    107
    108        do
    109        {
    110            if (!var1.hasNext())
    111            {
    112                return true;
    113            }
    114
    115            var2 = (PotionEffect)var1.next();
    116        }
    117        while (var2.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 var2 = calcPotionLiquidColor(getPotionEffects(par0, false));
    134                field_77925_n.put(Integer.valueOf(par0), Integer.valueOf(var2));
    135                return var2;
    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 var1 = func_77909_a(par0);
    147        return potionPrefixes[var1];
    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 var7 = 0;
    153
    154        if (par0)
    155        {
    156            var7 = isFlagUnset(par6, par4);
    157        }
    158        else if (par3 != -1)
    159        {
    160            if (par3 == 0 && countSetFlags(par6) == par4)
    161            {
    162                var7 = 1;
    163            }
    164            else if (par3 == 1 && countSetFlags(par6) > par4)
    165            {
    166                var7 = 1;
    167            }
    168            else if (par3 == 2 && countSetFlags(par6) < par4)
    169            {
    170                var7 = 1;
    171            }
    172        }
    173        else
    174        {
    175            var7 = isFlagSet(par6, par4);
    176        }
    177
    178        if (par1)
    179        {
    180            var7 *= par5;
    181        }
    182
    183        if (par2)
    184        {
    185            var7 *= -1;
    186        }
    187
    188        return var7;
    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 var1;
    197
    198        for (var1 = 0; par0 > 0; ++var1)
    199        {
    200            par0 &= par0 - 1;
    201        }
    202
    203        return var1;
    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 var4 = par0Str.indexOf(124, par1);
    211            int var5;
    212            int var17;
    213
    214            if (var4 >= 0 && var4 < par2)
    215            {
    216                var5 = parsePotionEffects(par0Str, par1, var4 - 1, par3);
    217
    218                if (var5 > 0)
    219                {
    220                    return var5;
    221                }
    222                else
    223                {
    224                    var17 = parsePotionEffects(par0Str, var4 + 1, par2, par3);
    225                    return var17 > 0 ? var17 : 0;
    226                }
    227            }
    228            else
    229            {
    230                var5 = par0Str.indexOf(38, par1);
    231
    232                if (var5 >= 0 && var5 < par2)
    233                {
    234                    var17 = parsePotionEffects(par0Str, par1, var5 - 1, par3);
    235
    236                    if (var17 <= 0)
    237                    {
    238                        return 0;
    239                    }
    240                    else
    241                    {
    242                        int var18 = parsePotionEffects(par0Str, var5 + 1, par2, par3);
    243                        return var18 <= 0 ? 0 : (var17 > var18 ? var17 : var18);
    244                    }
    245                }
    246                else
    247                {
    248                    boolean var6 = false;
    249                    boolean var7 = false;
    250                    boolean var8 = false;
    251                    boolean var9 = false;
    252                    boolean var10 = false;
    253                    byte var11 = -1;
    254                    int var12 = 0;
    255                    int var13 = 0;
    256                    int var14 = 0;
    257
    258                    for (int var15 = par1; var15 < par2; ++var15)
    259                    {
    260                        char var16 = par0Str.charAt(var15);
    261
    262                        if (var16 >= 48 && var16 <= 57)
    263                        {
    264                            if (var6)
    265                            {
    266                                var13 = var16 - 48;
    267                                var7 = true;
    268                            }
    269                            else
    270                            {
    271                                var12 *= 10;
    272                                var12 += var16 - 48;
    273                                var8 = true;
    274                            }
    275                        }
    276                        else if (var16 == 42)
    277                        {
    278                            var6 = true;
    279                        }
    280                        else if (var16 == 33)
    281                        {
    282                            if (var8)
    283                            {
    284                                var14 += func_77904_a(var9, var7, var10, var11, var12, var13, par3);
    285                                var9 = false;
    286                                var10 = false;
    287                                var6 = false;
    288                                var7 = false;
    289                                var8 = false;
    290                                var13 = 0;
    291                                var12 = 0;
    292                                var11 = -1;
    293                            }
    294
    295                            var9 = true;
    296                        }
    297                        else if (var16 == 45)
    298                        {
    299                            if (var8)
    300                            {
    301                                var14 += func_77904_a(var9, var7, var10, var11, var12, var13, par3);
    302                                var9 = false;
    303                                var10 = false;
    304                                var6 = false;
    305                                var7 = false;
    306                                var8 = false;
    307                                var13 = 0;
    308                                var12 = 0;
    309                                var11 = -1;
    310                            }
    311
    312                            var10 = true;
    313                        }
    314                        else if (var16 != 61 && var16 != 60 && var16 != 62)
    315                        {
    316                            if (var16 == 43 && var8)
    317                            {
    318                                var14 += func_77904_a(var9, var7, var10, var11, var12, var13, par3);
    319                                var9 = false;
    320                                var10 = false;
    321                                var6 = false;
    322                                var7 = false;
    323                                var8 = false;
    324                                var13 = 0;
    325                                var12 = 0;
    326                                var11 = -1;
    327                            }
    328                        }
    329                        else
    330                        {
    331                            if (var8)
    332                            {
    333                                var14 += func_77904_a(var9, var7, var10, var11, var12, var13, par3);
    334                                var9 = false;
    335                                var10 = false;
    336                                var6 = false;
    337                                var7 = false;
    338                                var8 = false;
    339                                var13 = 0;
    340                                var12 = 0;
    341                                var11 = -1;
    342                            }
    343
    344                            if (var16 == 61)
    345                            {
    346                                var11 = 0;
    347                            }
    348                            else if (var16 == 60)
    349                            {
    350                                var11 = 2;
    351                            }
    352                            else if (var16 == 62)
    353                            {
    354                                var11 = 1;
    355                            }
    356                        }
    357                    }
    358
    359                    if (var8)
    360                    {
    361                        var14 += func_77904_a(var9, var7, var10, var11, var12, var13, par3);
    362                    }
    363
    364                    return var14;
    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 var2 = null;
    380        Potion[] var3 = Potion.potionTypes;
    381        int var4 = var3.length;
    382
    383        for (int var5 = 0; var5 < var4; ++var5)
    384        {
    385            Potion var6 = var3[var5];
    386
    387            if (var6 != null && (!var6.isUsable() || par1))
    388            {
    389                String var7 = (String)potionRequirements.get(Integer.valueOf(var6.getId()));
    390
    391                if (var7 != null)
    392                {
    393                    int var8 = parsePotionEffects(var7, 0, var7.length(), par0);
    394
    395                    if (var8 > 0)
    396                    {
    397                        int var9 = 0;
    398                        String var10 = (String)potionAmplifiers.get(Integer.valueOf(var6.getId()));
    399
    400                        if (var10 != null)
    401                        {
    402                            var9 = parsePotionEffects(var10, 0, var10.length(), par0);
    403
    404                            if (var9 < 0)
    405                            {
    406                                var9 = 0;
    407                            }
    408                        }
    409
    410                        if (var6.isInstant())
    411                        {
    412                            var8 = 1;
    413                        }
    414                        else
    415                        {
    416                            var8 = 1200 * (var8 * 3 + (var8 - 1) * 2);
    417                            var8 >>= var9;
    418                            var8 = (int)Math.round((double)var8 * var6.getEffectiveness());
    419
    420                            if ((par0 & 16384) != 0)
    421                            {
    422                                var8 = (int)Math.round((double)var8 * 0.75D + 0.5D);
    423                            }
    424                        }
    425
    426                        if (var2 == null)
    427                        {
    428                            var2 = new ArrayList();
    429                        }
    430
    431                        PotionEffect var11 = new PotionEffect(var6.getId(), var8, var9);
    432
    433                        if ((par0 & 16384) != 0)
    434                        {
    435                            var11.setSplashPotion(true);
    436                        }
    437
    438                        var2.add(var11);
    439                    }
    440                }
    441            }
    442        }
    443
    444        return var2;
    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 var2 = 0;
    491        int var3 = par1Str.length();
    492        boolean var4 = false;
    493        boolean var5 = false;
    494        boolean var6 = false;
    495        boolean var7 = false;
    496        int var8 = 0;
    497
    498        for (int var9 = var2; var9 < var3; ++var9)
    499        {
    500            char var10 = par1Str.charAt(var9);
    501
    502            if (var10 >= 48 && var10 <= 57)
    503            {
    504                var8 *= 10;
    505                var8 += var10 - 48;
    506                var4 = true;
    507            }
    508            else if (var10 == 33)
    509            {
    510                if (var4)
    511                {
    512                    par0 = brewBitOperations(par0, var8, var6, var5, var7);
    513                    var7 = false;
    514                    var5 = false;
    515                    var6 = false;
    516                    var4 = false;
    517                    var8 = 0;
    518                }
    519
    520                var5 = true;
    521            }
    522            else if (var10 == 45)
    523            {
    524                if (var4)
    525                {
    526                    par0 = brewBitOperations(par0, var8, var6, var5, var7);
    527                    var7 = false;
    528                    var5 = false;
    529                    var6 = false;
    530                    var4 = false;
    531                    var8 = 0;
    532                }
    533
    534                var6 = true;
    535            }
    536            else if (var10 == 43)
    537            {
    538                if (var4)
    539                {
    540                    par0 = brewBitOperations(par0, var8, var6, var5, var7);
    541                    var7 = false;
    542                    var5 = false;
    543                    var6 = false;
    544                    var4 = false;
    545                    var8 = 0;
    546                }
    547            }
    548            else if (var10 == 38)
    549            {
    550                if (var4)
    551                {
    552                    par0 = brewBitOperations(par0, var8, var6, var5, var7);
    553                    var7 = false;
    554                    var5 = false;
    555                    var6 = false;
    556                    var4 = false;
    557                    var8 = 0;
    558                }
    559
    560                var7 = true;
    561            }
    562        }
    563
    564        if (var4)
    565        {
    566            par0 = brewBitOperations(par0, var8, var6, var5, var7);
    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}