PotionHelper .java

net.minecraft.potion.PotionHelper (class, 605 lines): members: getPotionPrefixIndexFlags, fermentedSpiderEyeEffect, calcPotionLiquidColor, checkFlag.

net.minecraft.potionclass605 linescommon20.7 KB

API Surface (9)

boolean checkFlag(int p_77914_0_, int p_77914_1_)
int getPotionPrefixIndex(int dataValue)
int calcPotionLiquidColor(Collection<PotionEffect> p_77911_0_)
boolean getAreAmbient(Collection<PotionEffect> potionEffects)
int getLiquidColor(int dataValue, boolean bypassCache)
String getPotionPrefix(int dataValue)
List<PotionEffect> getPotionEffects(int p_77917_0_, boolean p_77917_1_)
int applyIngredient(int p_77913_0_, String p_77913_1_)
int getPotionPrefixIndexFlags(int p_77908_0_, int p_77908_1_, int p_77908_2_, int p_77908_3_, int p_77908_4_, int p_77908_5_)

Fields & Constants (18)

unusedString
sugarEffect
ghastTearEffect
spiderEyeEffect
fermentedSpiderEyeEffect
speckledMelonEffect
blazePowderEffect
magmaCreamEffect
redstoneEffect
glowstoneEffect
gunpowderEffect
goldenCarrotEffect
pufferfishEffect
rabbitFootEffect
potionRequirements
potionAmplifiers
DATAVALUE_COLORS
potionPrefixes

String literals (32)

"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"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

PotionHelper.java605 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.potion;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import java.util.Collection;
6
import java.util.List;
7
import java.util.Map;
8
import net.minecraft.util.IntegerCache;
9
10
public class PotionHelper
11
{
12
    public static final String unusedString = null;
13
    public static final String sugarEffect = "-0+1-2-3&4-4+13";
14
    public static final String ghastTearEffect = "+0-1-2-3&4-4+13";
15
    public static final String spiderEyeEffect = "-0-1+2-3&4-4+13";
16
    public static final String fermentedSpiderEyeEffect = "-0+3-4+13";
17
    public static final String speckledMelonEffect = "+0-1+2-3&4-4+13";
18
    public static final String blazePowderEffect = "+0-1-2+3&4-4+13";
19
    public static final String magmaCreamEffect = "+0+1-2-3&4-4+13";
20
    public static final String redstoneEffect = "-5+6-7";
21
    public static final String glowstoneEffect = "+5-6-7";
22
    public static final String gunpowderEffect = "+14&13-13";
23
    public static final String goldenCarrotEffect = "-0+1+2-3+13&4-4";
24
    public static final String pufferfishEffect = "+0-1+2+3+13&4-4";
25
    public static final String rabbitFootEffect = "+0+1-2+3&4-4+13";
26
    private static final Map<Integer, String> potionRequirements = Maps.<Integer, String>newHashMap();
27
    private static final Map<Integer, String> potionAmplifiers = Maps.<Integer, String>newHashMap();
28
    private static final Map<Integer, Integer> DATAVALUE_COLORS = Maps.<Integer, Integer>newHashMap();
29
30
    /** An array of possible potion prefix names, as translation IDs. */
31
    private static final String[] 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"};
32
33
    /**
34
     * Checks if the bit at 1 << j is on in i.
35
     */
36
    public static boolean checkFlag(int p_77914_0_, int p_77914_1_)
37
    {
38
        return (p_77914_0_ & 1 << p_77914_1_) != 0;
39
    }
40
41
    /**
42
     * Returns 1 if the flag is set, 0 if it is not set.
43
     */
44
    private static int isFlagSet(int p_77910_0_, int p_77910_1_)
45
    {
46
        return checkFlag(p_77910_0_, p_77910_1_) ? 1 : 0;
47
    }
48
49
    /**
50
     * Returns 0 if the flag is set, 1 if it is not set.
51
     */
52
    private static int isFlagUnset(int p_77916_0_, int p_77916_1_)
53
    {
54
        return checkFlag(p_77916_0_, p_77916_1_) ? 0 : 1;
55
    }
56
57
    /**
58
     * Given a potion data value, get its prefix index number.
59
     */
60
    public static int getPotionPrefixIndex(int dataValue)
61
    {
62
        return getPotionPrefixIndexFlags(dataValue, 5, 4, 3, 2, 1);
63
    }
64
65
    /**
66
     * Given a {@link Collection}<{@link PotionEffect}> will return an Integer color.
67
     */
68
    public static int calcPotionLiquidColor(Collection<PotionEffect> p_77911_0_)
69
    {
70
        int i = 3694022;
71
72
        if (p_77911_0_ != null && !p_77911_0_.isEmpty())
73
        {
74
            float f = 0.0F;
75
            float f1 = 0.0F;
76
            float f2 = 0.0F;
77
            float f3 = 0.0F;
78
79
            for (PotionEffect potioneffect : p_77911_0_)
80
            {
81
                if (potioneffect.getIsShowParticles())
82
                {
83
                    int j = Potion.potionTypes[potioneffect.getPotionID()].getLiquidColor();
84
85
                    for (int k = 0; k <= potioneffect.getAmplifier(); ++k)
86
                    {
87
                        f += (float)(j >> 16 & 255) / 255.0F;
88
                        f1 += (float)(j >> 8 & 255) / 255.0F;
89
                        f2 += (float)(j >> 0 & 255) / 255.0F;
90
                        ++f3;
91
                    }
92
                }
93
            }
94
95
            if (f3 == 0.0F)
96
            {
97
                return 0;
98
            }
99
            else
100
            {
101
                f = f / f3 * 255.0F;
102
                f1 = f1 / f3 * 255.0F;
103
                f2 = f2 / f3 * 255.0F;
104
                return (int)f << 16 | (int)f1 << 8 | (int)f2;
105
            }
106
        }
107
        else
108
        {
109
            return i;
110
        }
111
    }
112
113
    /**
114
     * Check whether a {@link Collection}<{@link PotionEffect}> are all ambient.
115
     */
116
    public static boolean getAreAmbient(Collection<PotionEffect> potionEffects)
117
    {
118
        for (PotionEffect potioneffect : potionEffects)
119
        {
120
            if (!potioneffect.getIsAmbient())
121
            {
122
                return false;
123
            }
124
        }
125
126
        return true;
127
    }
128
129
    /**
130
     * Given a potion data value, get the associated liquid color (optionally bypassing the cache)
131
     */
132
    public static int getLiquidColor(int dataValue, boolean bypassCache)
133
    {
134
        Integer integer = IntegerCache.getInteger(dataValue);
135
136
        if (!bypassCache)
137
        {
138
            if (DATAVALUE_COLORS.containsKey(integer))
139
            {
140
                return ((Integer)DATAVALUE_COLORS.get(integer)).intValue();
141
            }
142
            else
143
            {
144
                int i = calcPotionLiquidColor(getPotionEffects(integer.intValue(), false));
145
                DATAVALUE_COLORS.put(integer, Integer.valueOf(i));
146
                return i;
147
            }
148
        }
149
        else
150
        {
151
            return calcPotionLiquidColor(getPotionEffects(integer.intValue(), true));
152
        }
153
    }
154
155
    /**
156
     * Given a potion data value, get its prefix as a translation ID.
157
     */
158
    public static String getPotionPrefix(int dataValue)
159
    {
160
        int i = getPotionPrefixIndex(dataValue);
161
        return potionPrefixes[i];
162
    }
163
164
    private static int getPotionEffect(boolean p_77904_0_, boolean p_77904_1_, boolean p_77904_2_, int p_77904_3_, int p_77904_4_, int p_77904_5_, int p_77904_6_)
165
    {
166
        int i = 0;
167
168
        if (p_77904_0_)
169
        {
170
            i = isFlagUnset(p_77904_6_, p_77904_4_);
171
        }
172
        else if (p_77904_3_ != -1)
173
        {
174
            if (p_77904_3_ == 0 && countSetFlags(p_77904_6_) == p_77904_4_)
175
            {
176
                i = 1;
177
            }
178
            else if (p_77904_3_ == 1 && countSetFlags(p_77904_6_) > p_77904_4_)
179
            {
180
                i = 1;
181
            }
182
            else if (p_77904_3_ == 2 && countSetFlags(p_77904_6_) < p_77904_4_)
183
            {
184
                i = 1;
185
            }
186
        }
187
        else
188
        {
189
            i = isFlagSet(p_77904_6_, p_77904_4_);
190
        }
191
192
        if (p_77904_1_)
193
        {
194
            i *= p_77904_5_;
195
        }
196
197
        if (p_77904_2_)
198
        {
199
            i *= -1;
200
        }
201
202
        return i;
203
    }
204
205
    /**
206
     * Returns the number of 1 bits in the given integer.
207
     */
208
    private static int countSetFlags(int p_77907_0_)
209
    {
210
        int i;
211
212
        for (i = 0; p_77907_0_ > 0; ++i)
213
        {
214
            p_77907_0_ &= p_77907_0_ - 1;
215
        }
216
217
        return i;
218
    }
219
220
    private static int parsePotionEffects(String p_77912_0_, int p_77912_1_, int p_77912_2_, int p_77912_3_)
221
    {
222
        if (p_77912_1_ < p_77912_0_.length() && p_77912_2_ >= 0 && p_77912_1_ < p_77912_2_)
223
        {
224
            int i = p_77912_0_.indexOf(124, p_77912_1_);
225
226
            if (i >= 0 && i < p_77912_2_)
227
            {
228
                int l1 = parsePotionEffects(p_77912_0_, p_77912_1_, i - 1, p_77912_3_);
229
230
                if (l1 > 0)
231
                {
232
                    return l1;
233
                }
234
                else
235
                {
236
                    int j2 = parsePotionEffects(p_77912_0_, i + 1, p_77912_2_, p_77912_3_);
237
                    return j2 > 0 ? j2 : 0;
238
                }
239
            }
240
            else
241
            {
242
                int j = p_77912_0_.indexOf(38, p_77912_1_);
243
244
                if (j >= 0 && j < p_77912_2_)
245
                {
246
                    int i2 = parsePotionEffects(p_77912_0_, p_77912_1_, j - 1, p_77912_3_);
247
248
                    if (i2 <= 0)
249
                    {
250
                        return 0;
251
                    }
252
                    else
253
                    {
254
                        int k2 = parsePotionEffects(p_77912_0_, j + 1, p_77912_2_, p_77912_3_);
255
                        return k2 <= 0 ? 0 : (i2 > k2 ? i2 : k2);
256
                    }
257
                }
258
                else
259
                {
260
                    boolean flag = false;
261
                    boolean flag1 = false;
262
                    boolean flag2 = false;
263
                    boolean flag3 = false;
264
                    boolean flag4 = false;
265
                    int k = -1;
266
                    int l = 0;
267
                    int i1 = 0;
268
                    int j1 = 0;
269
270
                    for (int k1 = p_77912_1_; k1 < p_77912_2_; ++k1)
271
                    {
272
                        char c0 = p_77912_0_.charAt(k1);
273
274
                        if (c0 >= 48 && c0 <= 57)
275
                        {
276
                            if (flag)
277
                            {
278
                                i1 = c0 - 48;
279
                                flag1 = true;
280
                            }
281
                            else
282
                            {
283
                                l = l * 10;
284
                                l = l + (c0 - 48);
285
                                flag2 = true;
286
                            }
287
                        }
288
                        else if (c0 == 42)
289
                        {
290
                            flag = true;
291
                        }
292
                        else if (c0 == 33)
293
                        {
294
                            if (flag2)
295
                            {
296
                                j1 += getPotionEffect(flag3, flag1, flag4, k, l, i1, p_77912_3_);
297
                                flag3 = false;
298
                                flag4 = false;
299
                                flag = false;
300
                                flag1 = false;
301
                                flag2 = false;
302
                                i1 = 0;
303
                                l = 0;
304
                                k = -1;
305
                            }
306
307
                            flag3 = true;
308
                        }
309
                        else if (c0 == 45)
310
                        {
311
                            if (flag2)
312
                            {
313
                                j1 += getPotionEffect(flag3, flag1, flag4, k, l, i1, p_77912_3_);
314
                                flag3 = false;
315
                                flag4 = false;
316
                                flag = false;
317
                                flag1 = false;
318
                                flag2 = false;
319
                                i1 = 0;
320
                                l = 0;
321
                                k = -1;
322
                            }
323
324
                            flag4 = true;
325
                        }
326
                        else if (c0 != 61 && c0 != 60 && c0 != 62)
327
                        {
328
                            if (c0 == 43 && flag2)
329
                            {
330
                                j1 += getPotionEffect(flag3, flag1, flag4, k, l, i1, p_77912_3_);
331
                                flag3 = false;
332
                                flag4 = false;
333
                                flag = false;
334
                                flag1 = false;
335
                                flag2 = false;
336
                                i1 = 0;
337
                                l = 0;
338
                                k = -1;
339
                            }
340
                        }
341
                        else
342
                        {
343
                            if (flag2)
344
                            {
345
                                j1 += getPotionEffect(flag3, flag1, flag4, k, l, i1, p_77912_3_);
346
                                flag3 = false;
347
                                flag4 = false;
348
                                flag = false;
349
                                flag1 = false;
350
                                flag2 = false;
351
                                i1 = 0;
352
                                l = 0;
353
                                k = -1;
354
                            }
355
356
                            if (c0 == 61)
357
                            {
358
                                k = 0;
359
                            }
360
                            else if (c0 == 60)
361
                            {
362
                                k = 2;
363
                            }
364
                            else if (c0 == 62)
365
                            {
366
                                k = 1;
367
                            }
368
                        }
369
                    }
370
371
                    if (flag2)
372
                    {
373
                        j1 += getPotionEffect(flag3, flag1, flag4, k, l, i1, p_77912_3_);
374
                    }
375
376
                    return j1;
377
                }
378
            }
379
        }
380
        else
381
        {
382
            return 0;
383
        }
384
    }
385
386
    public static List<PotionEffect> getPotionEffects(int p_77917_0_, boolean p_77917_1_)
387
    {
388
        List<PotionEffect> list = null;
389
390
        for (Potion potion : Potion.potionTypes)
391
        {
392
            if (potion != null && (!potion.isUsable() || p_77917_1_))
393
            {
394
                String s = (String)potionRequirements.get(Integer.valueOf(potion.getId()));
395
396
                if (s != null)
397
                {
398
                    int i = parsePotionEffects(s, 0, s.length(), p_77917_0_);
399
400
                    if (i > 0)
401
                    {
402
                        int j = 0;
403
                        String s1 = (String)potionAmplifiers.get(Integer.valueOf(potion.getId()));
404
405
                        if (s1 != null)
406
                        {
407
                            j = parsePotionEffects(s1, 0, s1.length(), p_77917_0_);
408
409
                            if (j < 0)
410
                            {
411
                                j = 0;
412
                            }
413
                        }
414
415
                        if (potion.isInstant())
416
                        {
417
                            i = 1;
418
                        }
419
                        else
420
                        {
421
                            i = 1200 * (i * 3 + (i - 1) * 2);
422
                            i = i >> j;
423
                            i = (int)Math.round((double)i * potion.getEffectiveness());
424
425
                            if ((p_77917_0_ & 16384) != 0)
426
                            {
427
                                i = (int)Math.round((double)i * 0.75D + 0.5D);
428
                            }
429
                        }
430
431
                        if (list == null)
432
                        {
433
                            list = Lists.<PotionEffect>newArrayList();
434
                        }
435
436
                        PotionEffect potioneffect = new PotionEffect(potion.getId(), i, j);
437
438
                        if ((p_77917_0_ & 16384) != 0)
439
                        {
440
                            potioneffect.setSplashPotion(true);
441
                        }
442
443
                        list.add(potioneffect);
444
                    }
445
                }
446
            }
447
        }
448
449
        return list;
450
    }
451
452
    /**
453
     * Manipulates the specified bit of the potion damage value according to the rules passed from applyIngredient.
454
     */
455
    private static int brewBitOperations(int p_77906_0_, int p_77906_1_, boolean p_77906_2_, boolean p_77906_3_, boolean p_77906_4_)
456
    {
457
        if (p_77906_4_)
458
        {
459
            if (!checkFlag(p_77906_0_, p_77906_1_))
460
            {
461
                return 0;
462
            }
463
        }
464
        else if (p_77906_2_)
465
        {
466
            p_77906_0_ &= ~(1 << p_77906_1_);
467
        }
468
        else if (p_77906_3_)
469
        {
470
            if ((p_77906_0_ & 1 << p_77906_1_) == 0)
471
            {
472
                p_77906_0_ |= 1 << p_77906_1_;
473
            }
474
            else
475
            {
476
                p_77906_0_ &= ~(1 << p_77906_1_);
477
            }
478
        }
479
        else
480
        {
481
            p_77906_0_ |= 1 << p_77906_1_;
482
        }
483
484
        return p_77906_0_;
485
    }
486
487
    /**
488
     * Returns the new potion damage value after the specified ingredient info is applied to the specified potion.
489
     */
490
    public static int applyIngredient(int p_77913_0_, String p_77913_1_)
491
    {
492
        int i = 0;
493
        int j = p_77913_1_.length();
494
        boolean flag = false;
495
        boolean flag1 = false;
496
        boolean flag2 = false;
497
        boolean flag3 = false;
498
        int k = 0;
499
500
        for (int l = i; l < j; ++l)
501
        {
502
            char c0 = p_77913_1_.charAt(l);
503
504
            if (c0 >= 48 && c0 <= 57)
505
            {
506
                k = k * 10;
507
                k = k + (c0 - 48);
508
                flag = true;
509
            }
510
            else if (c0 == 33)
511
            {
512
                if (flag)
513
                {
514
                    p_77913_0_ = brewBitOperations(p_77913_0_, k, flag2, flag1, flag3);
515
                    flag3 = false;
516
                    flag1 = false;
517
                    flag2 = false;
518
                    flag = false;
519
                    k = 0;
520
                }
521
522
                flag1 = true;
523
            }
524
            else if (c0 == 45)
525
            {
526
                if (flag)
527
                {
528
                    p_77913_0_ = brewBitOperations(p_77913_0_, k, flag2, flag1, flag3);
529
                    flag3 = false;
530
                    flag1 = false;
531
                    flag2 = false;
532
                    flag = false;
533
                    k = 0;
534
                }
535
536
                flag2 = true;
537
            }
538
            else if (c0 == 43)
539
            {
540
                if (flag)
541
                {
542
                    p_77913_0_ = brewBitOperations(p_77913_0_, k, flag2, flag1, flag3);
543
                    flag3 = false;
544
                    flag1 = false;
545
                    flag2 = false;
546
                    flag = false;
547
                    k = 0;
548
                }
549
            }
550
            else if (c0 == 38)
551
            {
552
                if (flag)
553
                {
554
                    p_77913_0_ = brewBitOperations(p_77913_0_, k, flag2, flag1, flag3);
555
                    flag3 = false;
556
                    flag1 = false;
557
                    flag2 = false;
558
                    flag = false;
559
                    k = 0;
560
                }
561
562
                flag3 = true;
563
            }
564
        }
565
566
        if (flag)
567
        {
568
            p_77913_0_ = brewBitOperations(p_77913_0_, k, flag2, flag1, flag3);
569
        }
570
571
        return p_77913_0_ & 32767;
572
    }
573
574
    public static int getPotionPrefixIndexFlags(int p_77908_0_, int p_77908_1_, int p_77908_2_, int p_77908_3_, int p_77908_4_, int p_77908_5_)
575
    {
576
        return (checkFlag(p_77908_0_, p_77908_1_) ? 16 : 0) | (checkFlag(p_77908_0_, p_77908_2_) ? 8 : 0) | (checkFlag(p_77908_0_, p_77908_3_) ? 4 : 0) | (checkFlag(p_77908_0_, p_77908_4_) ? 2 : 0) | (checkFlag(p_77908_0_, p_77908_5_) ? 1 : 0);
577
    }
578
579
    static
580
    {
581
        potionRequirements.put(Integer.valueOf(Potion.regeneration.getId()), "0 & !1 & !2 & !3 & 0+6");
582
        potionRequirements.put(Integer.valueOf(Potion.moveSpeed.getId()), "!0 & 1 & !2 & !3 & 1+6");
583
        potionRequirements.put(Integer.valueOf(Potion.fireResistance.getId()), "0 & 1 & !2 & !3 & 0+6");
584
        potionRequirements.put(Integer.valueOf(Potion.heal.getId()), "0 & !1 & 2 & !3");
585
        potionRequirements.put(Integer.valueOf(Potion.poison.getId()), "!0 & !1 & 2 & !3 & 2+6");
586
        potionRequirements.put(Integer.valueOf(Potion.weakness.getId()), "!0 & !1 & !2 & 3 & 3+6");
587
        potionRequirements.put(Integer.valueOf(Potion.harm.getId()), "!0 & !1 & 2 & 3");
588
        potionRequirements.put(Integer.valueOf(Potion.moveSlowdown.getId()), "!0 & 1 & !2 & 3 & 3+6");
589
        potionRequirements.put(Integer.valueOf(Potion.damageBoost.getId()), "0 & !1 & !2 & 3 & 3+6");
590
        potionRequirements.put(Integer.valueOf(Potion.nightVision.getId()), "!0 & 1 & 2 & !3 & 2+6");
591
        potionRequirements.put(Integer.valueOf(Potion.invisibility.getId()), "!0 & 1 & 2 & 3 & 2+6");
592
        potionRequirements.put(Integer.valueOf(Potion.waterBreathing.getId()), "0 & !1 & 2 & 3 & 2+6");
593
        potionRequirements.put(Integer.valueOf(Potion.jump.getId()), "0 & 1 & !2 & 3 & 3+6");
594
        potionAmplifiers.put(Integer.valueOf(Potion.moveSpeed.getId()), "5");
595
        potionAmplifiers.put(Integer.valueOf(Potion.digSpeed.getId()), "5");
596
        potionAmplifiers.put(Integer.valueOf(Potion.damageBoost.getId()), "5");
597
        potionAmplifiers.put(Integer.valueOf(Potion.regeneration.getId()), "5");
598
        potionAmplifiers.put(Integer.valueOf(Potion.harm.getId()), "5");
599
        potionAmplifiers.put(Integer.valueOf(Potion.heal.getId()), "5");
600
        potionAmplifiers.put(Integer.valueOf(Potion.resistance.getId()), "5");
601
        potionAmplifiers.put(Integer.valueOf(Potion.poison.getId()), "5");
602
        potionAmplifiers.put(Integer.valueOf(Potion.jump.getId()), "5");
603
    }
604
}
605

Deobfuscated MCP 1.8.9 source of PotionHelper from Marcelektro/MavenMCP-1.8.9 (net/minecraft/potion/PotionHelper.java), stored verbatim.