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