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