ItemPotion .java

net.minecraft.item.ItemPotion (class): members: getColorFromDamage, SUB_ITEMS_CACHE, isEffectInstant, effectCache, getEffects, isSplash, getItemUseAction.

net.minecraft.itemclass396 linescommonextends Item13.2 KB

API Surface (14)

List<PotionEffect> getEffects(ItemStack stack)
List<PotionEffect> getEffects(int meta)
ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityPlayer playerIn)
int getMaxItemUseDuration(ItemStack stack)
EnumAction getItemUseAction(ItemStack stack)
ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
boolean isSplash(int meta)
int getColorFromDamage(int meta)
int getColorFromItemStack(ItemStack stack, int renderPass)
boolean isEffectInstant(int meta)
String getItemStackDisplayName(ItemStack stack)
void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
boolean hasEffect(ItemStack stack)
void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)

Fields & Constants (2)

SUB_ITEMS_CACHE
effectCache

String literals (11)

"CustomPotionEffects"
"random.bow"
"item.emptyPotion.name"
"potion.prefix.grenade"
".postfix"
"potion.potency."
"potion.empty"
"potion.effects.whenDrank"
"attribute.modifier.plus."
"attribute.name."
"attribute.modifier.take."

Full Source

ItemPotion.java396 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.item;
2
3
import com.google.common.collect.HashMultimap;
4
import com.google.common.collect.Lists;
5
import com.google.common.collect.Maps;
6
import com.google.common.collect.Multimap;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.Map.Entry;
11
import net.minecraft.creativetab.CreativeTabs;
12
import net.minecraft.entity.ai.attributes.AttributeModifier;
13
import net.minecraft.entity.ai.attributes.IAttribute;
14
import net.minecraft.entity.player.EntityPlayer;
15
import net.minecraft.entity.projectile.EntityPotion;
16
import net.minecraft.init.Items;
17
import net.minecraft.nbt.NBTTagCompound;
18
import net.minecraft.nbt.NBTTagList;
19
import net.minecraft.potion.Potion;
20
import net.minecraft.potion.PotionEffect;
21
import net.minecraft.potion.PotionHelper;
22
import net.minecraft.stats.StatList;
23
import net.minecraft.util.EnumChatFormatting;
24
import net.minecraft.util.StatCollector;
25
import net.minecraft.world.World;
26
27
public class ItemPotion extends Item
28
{
29
    private Map<Integer, List<PotionEffect>> effectCache = Maps.<Integer, List<PotionEffect>>newHashMap();
30
    private static final Map<List<PotionEffect>, Integer> SUB_ITEMS_CACHE = Maps.<List<PotionEffect>, Integer>newLinkedHashMap();
31
32
    public ItemPotion()
33
    {
34
        this.setMaxStackSize(1);
35
        this.setHasSubtypes(true);
36
        this.setMaxDamage(0);
37
        this.setCreativeTab(CreativeTabs.tabBrewing);
38
    }
39
40
    public List<PotionEffect> getEffects(ItemStack stack)
41
    {
42
        if (stack.hasTagCompound() && stack.getTagCompound().hasKey("CustomPotionEffects", 9))
43
        {
44
            List<PotionEffect> list1 = Lists.<PotionEffect>newArrayList();
45
            NBTTagList nbttaglist = stack.getTagCompound().getTagList("CustomPotionEffects", 10);
46
47
            for (int i = 0; i < nbttaglist.tagCount(); ++i)
48
            {
49
                NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
50
                PotionEffect potioneffect = PotionEffect.readCustomPotionEffectFromNBT(nbttagcompound);
51
52
                if (potioneffect != null)
53
                {
54
                    list1.add(potioneffect);
55
                }
56
            }
57
58
            return list1;
59
        }
60
        else
61
        {
62
            List<PotionEffect> list = (List)this.effectCache.get(Integer.valueOf(stack.getMetadata()));
63
64
            if (list == null)
65
            {
66
                list = PotionHelper.getPotionEffects(stack.getMetadata(), false);
67
                this.effectCache.put(Integer.valueOf(stack.getMetadata()), list);
68
            }
69
70
            return list;
71
        }
72
    }
73
74
    public List<PotionEffect> getEffects(int meta)
75
    {
76
        List<PotionEffect> list = (List)this.effectCache.get(Integer.valueOf(meta));
77
78
        if (list == null)
79
        {
80
            list = PotionHelper.getPotionEffects(meta, false);
81
            this.effectCache.put(Integer.valueOf(meta), list);
82
        }
83
84
        return list;
85
    }
86
87
    /**
88
     * Called when the player finishes using this Item (E.g. finishes eating.). Not called when the player stops using
89
     * the Item before the action is complete.
90
     */
91
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityPlayer playerIn)
92
    {
93
        if (!playerIn.capabilities.isCreativeMode)
94
        {
95
            --stack.stackSize;
96
        }
97
98
        if (!worldIn.isRemote)
99
        {
100
            List<PotionEffect> list = this.getEffects(stack);
101
102
            if (list != null)
103
            {
104
                for (PotionEffect potioneffect : list)
105
                {
106
                    playerIn.addPotionEffect(new PotionEffect(potioneffect));
107
                }
108
            }
109
        }
110
111
        playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this)]);
112
113
        if (!playerIn.capabilities.isCreativeMode)
114
        {
115
            if (stack.stackSize <= 0)
116
            {
117
                return new ItemStack(Items.glass_bottle);
118
            }
119
120
            playerIn.inventory.addItemStackToInventory(new ItemStack(Items.glass_bottle));
121
        }
122
123
        return stack;
124
    }
125
126
    /**
127
     * How long it takes to use or consume an item
128
     */
129
    public int getMaxItemUseDuration(ItemStack stack)
130
    {
131
        return 32;
132
    }
133
134
    /**
135
     * returns the action that specifies what animation to play when the items is being used
136
     */
137
    public EnumAction getItemUseAction(ItemStack stack)
138
    {
139
        return EnumAction.DRINK;
140
    }
141
142
    /**
143
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
144
     */
145
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
146
    {
147
        if (isSplash(itemStackIn.getMetadata()))
148
        {
149
            if (!playerIn.capabilities.isCreativeMode)
150
            {
151
                --itemStackIn.stackSize;
152
            }
153
154
            worldIn.playSoundAtEntity(playerIn, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
155
156
            if (!worldIn.isRemote)
157
            {
158
                worldIn.spawnEntityInWorld(new EntityPotion(worldIn, playerIn, itemStackIn));
159
            }
160
161
            playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this)]);
162
            return itemStackIn;
163
        }
164
        else
165
        {
166
            playerIn.setItemInUse(itemStackIn, this.getMaxItemUseDuration(itemStackIn));
167
            return itemStackIn;
168
        }
169
    }
170
171
    /**
172
     * returns wether or not a potion is a throwable splash potion based on damage value
173
     */
174
    public static boolean isSplash(int meta)
175
    {
176
        return (meta & 16384) != 0;
177
    }
178
179
    public int getColorFromDamage(int meta)
180
    {
181
        return PotionHelper.getLiquidColor(meta, false);
182
    }
183
184
    public int getColorFromItemStack(ItemStack stack, int renderPass)
185
    {
186
        return renderPass > 0 ? 16777215 : this.getColorFromDamage(stack.getMetadata());
187
    }
188
189
    public boolean isEffectInstant(int meta)
190
    {
191
        List<PotionEffect> list = this.getEffects(meta);
192
193
        if (list != null && !list.isEmpty())
194
        {
195
            for (PotionEffect potioneffect : list)
196
            {
197
                if (Potion.potionTypes[potioneffect.getPotionID()].isInstant())
198
                {
199
                    return true;
200
                }
201
            }
202
203
            return false;
204
        }
205
        else
206
        {
207
            return false;
208
        }
209
    }
210
211
    public String getItemStackDisplayName(ItemStack stack)
212
    {
213
        if (stack.getMetadata() == 0)
214
        {
215
            return StatCollector.translateToLocal("item.emptyPotion.name").trim();
216
        }
217
        else
218
        {
219
            String s = "";
220
221
            if (isSplash(stack.getMetadata()))
222
            {
223
                s = StatCollector.translateToLocal("potion.prefix.grenade").trim() + " ";
224
            }
225
226
            List<PotionEffect> list = Items.potionitem.getEffects(stack);
227
228
            if (list != null && !list.isEmpty())
229
            {
230
                String s2 = ((PotionEffect)list.get(0)).getEffectName();
231
                s2 = s2 + ".postfix";
232
                return s + StatCollector.translateToLocal(s2).trim();
233
            }
234
            else
235
            {
236
                String s1 = PotionHelper.getPotionPrefix(stack.getMetadata());
237
                return StatCollector.translateToLocal(s1).trim() + " " + super.getItemStackDisplayName(stack);
238
            }
239
        }
240
    }
241
242
    /**
243
     * allows items to add custom lines of information to the mouseover description
244
     */
245
    public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
246
    {
247
        if (stack.getMetadata() != 0)
248
        {
249
            List<PotionEffect> list = Items.potionitem.getEffects(stack);
250
            Multimap<String, AttributeModifier> multimap = HashMultimap.<String, AttributeModifier>create();
251
252
            if (list != null && !list.isEmpty())
253
            {
254
                for (PotionEffect potioneffect : list)
255
                {
256
                    String s1 = StatCollector.translateToLocal(potioneffect.getEffectName()).trim();
257
                    Potion potion = Potion.potionTypes[potioneffect.getPotionID()];
258
                    Map<IAttribute, AttributeModifier> map = potion.getAttributeModifierMap();
259
260
                    if (map != null && map.size() > 0)
261
                    {
262
                        for (Entry<IAttribute, AttributeModifier> entry : map.entrySet())
263
                        {
264
                            AttributeModifier attributemodifier = (AttributeModifier)entry.getValue();
265
                            AttributeModifier attributemodifier1 = new AttributeModifier(attributemodifier.getName(), potion.getAttributeModifierAmount(potioneffect.getAmplifier(), attributemodifier), attributemodifier.getOperation());
266
                            multimap.put(((IAttribute)entry.getKey()).getAttributeUnlocalizedName(), attributemodifier1);
267
                        }
268
                    }
269
270
                    if (potioneffect.getAmplifier() > 0)
271
                    {
272
                        s1 = s1 + " " + StatCollector.translateToLocal("potion.potency." + potioneffect.getAmplifier()).trim();
273
                    }
274
275
                    if (potioneffect.getDuration() > 20)
276
                    {
277
                        s1 = s1 + " (" + Potion.getDurationString(potioneffect) + ")";
278
                    }
279
280
                    if (potion.isBadEffect())
281
                    {
282
                        tooltip.add(EnumChatFormatting.RED + s1);
283
                    }
284
                    else
285
                    {
286
                        tooltip.add(EnumChatFormatting.GRAY + s1);
287
                    }
288
                }
289
            }
290
            else
291
            {
292
                String s = StatCollector.translateToLocal("potion.empty").trim();
293
                tooltip.add(EnumChatFormatting.GRAY + s);
294
            }
295
296
            if (!multimap.isEmpty())
297
            {
298
                tooltip.add("");
299
                tooltip.add(EnumChatFormatting.DARK_PURPLE + StatCollector.translateToLocal("potion.effects.whenDrank"));
300
301
                for (Entry<String, AttributeModifier> entry1 : multimap.entries())
302
                {
303
                    AttributeModifier attributemodifier2 = (AttributeModifier)entry1.getValue();
304
                    double d0 = attributemodifier2.getAmount();
305
                    double d1;
306
307
                    if (attributemodifier2.getOperation() != 1 && attributemodifier2.getOperation() != 2)
308
                    {
309
                        d1 = attributemodifier2.getAmount();
310
                    }
311
                    else
312
                    {
313
                        d1 = attributemodifier2.getAmount() * 100.0D;
314
                    }
315
316
                    if (d0 > 0.0D)
317
                    {
318
                        tooltip.add(EnumChatFormatting.BLUE + StatCollector.translateToLocalFormatted("attribute.modifier.plus." + attributemodifier2.getOperation(), new Object[] {ItemStack.DECIMALFORMAT.format(d1), StatCollector.translateToLocal("attribute.name." + (String)entry1.getKey())}));
319
                    }
320
                    else if (d0 < 0.0D)
321
                    {
322
                        d1 = d1 * -1.0D;
323
                        tooltip.add(EnumChatFormatting.RED + StatCollector.translateToLocalFormatted("attribute.modifier.take." + attributemodifier2.getOperation(), new Object[] {ItemStack.DECIMALFORMAT.format(d1), StatCollector.translateToLocal("attribute.name." + (String)entry1.getKey())}));
324
                    }
325
                }
326
            }
327
        }
328
    }
329
330
    public boolean hasEffect(ItemStack stack)
331
    {
332
        List<PotionEffect> list = this.getEffects(stack);
333
        return list != null && !list.isEmpty();
334
    }
335
336
    /**
337
     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
338
     */
339
    public void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
340
    {
341
        super.getSubItems(itemIn, tab, subItems);
342
343
        if (SUB_ITEMS_CACHE.isEmpty())
344
        {
345
            for (int i = 0; i <= 15; ++i)
346
            {
347
                for (int j = 0; j <= 1; ++j)
348
                {
349
                    int lvt_6_1_;
350
351
                    if (j == 0)
352
                    {
353
                        lvt_6_1_ = i | 8192;
354
                    }
355
                    else
356
                    {
357
                        lvt_6_1_ = i | 16384;
358
                    }
359
360
                    for (int l = 0; l <= 2; ++l)
361
                    {
362
                        int i1 = lvt_6_1_;
363
364
                        if (l != 0)
365
                        {
366
                            if (l == 1)
367
                            {
368
                                i1 = lvt_6_1_ | 32;
369
                            }
370
                            else if (l == 2)
371
                            {
372
                                i1 = lvt_6_1_ | 64;
373
                            }
374
                        }
375
376
                        List<PotionEffect> list = PotionHelper.getPotionEffects(i1, false);
377
378
                        if (list != null && !list.isEmpty())
379
                        {
380
                            SUB_ITEMS_CACHE.put(list, Integer.valueOf(i1));
381
                        }
382
                    }
383
                }
384
            }
385
        }
386
387
        Iterator iterator = SUB_ITEMS_CACHE.values().iterator();
388
389
        while (iterator.hasNext())
390
        {
391
            int j1 = ((Integer)iterator.next()).intValue();
392
            subItems.add(new ItemStack(itemIn, 1, j1));
393
        }
394
    }
395
}
396

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