| 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.Multimap;
|
| 6 | import java.text.DecimalFormat;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Random;
|
| 9 | import java.util.Map.Entry;
|
| 10 | import net.minecraft.block.Block;
|
| 11 | import net.minecraft.enchantment.Enchantment;
|
| 12 | import net.minecraft.enchantment.EnchantmentDurability;
|
| 13 | import net.minecraft.enchantment.EnchantmentHelper;
|
| 14 | import net.minecraft.entity.Entity;
|
| 15 | import net.minecraft.entity.EntityLivingBase;
|
| 16 | import net.minecraft.entity.EnumCreatureAttribute;
|
| 17 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 18 | import net.minecraft.entity.ai.attributes.AttributeModifier;
|
| 19 | import net.minecraft.entity.item.EntityItemFrame;
|
| 20 | import net.minecraft.entity.player.EntityPlayer;
|
| 21 | import net.minecraft.event.HoverEvent;
|
| 22 | import net.minecraft.init.Items;
|
| 23 | import net.minecraft.nbt.NBTBase;
|
| 24 | import net.minecraft.nbt.NBTTagCompound;
|
| 25 | import net.minecraft.nbt.NBTTagList;
|
| 26 | import net.minecraft.stats.StatList;
|
| 27 | import net.minecraft.util.BlockPos;
|
| 28 | import net.minecraft.util.ChatComponentText;
|
| 29 | import net.minecraft.util.EnumChatFormatting;
|
| 30 | import net.minecraft.util.EnumFacing;
|
| 31 | import net.minecraft.util.IChatComponent;
|
| 32 | import net.minecraft.util.ResourceLocation;
|
| 33 | import net.minecraft.util.StatCollector;
|
| 34 | import net.minecraft.world.World;
|
| 35 |
|
| 36 | public final class ItemStack
|
| 37 | {
|
| 38 | public static final DecimalFormat DECIMALFORMAT = new DecimalFormat("#.###");
|
| 39 |
|
| 40 | /** Size of the stack. */
|
| 41 | public int stackSize;
|
| 42 |
|
| 43 | /**
|
| 44 | * Number of animation frames to go when receiving an item (by walking into it, for example).
|
| 45 | */
|
| 46 | public int animationsToGo;
|
| 47 | private Item item;
|
| 48 |
|
| 49 | /**
|
| 50 | * A NBTTagMap containing data about an ItemStack. Can only be used for non stackable items
|
| 51 | */
|
| 52 | private NBTTagCompound stackTagCompound;
|
| 53 | private int itemDamage;
|
| 54 |
|
| 55 | /** Item frame this stack is on, or null if not on an item frame. */
|
| 56 | private EntityItemFrame itemFrame;
|
| 57 | private Block canDestroyCacheBlock;
|
| 58 | private boolean canDestroyCacheResult;
|
| 59 | private Block canPlaceOnCacheBlock;
|
| 60 | private boolean canPlaceOnCacheResult;
|
| 61 |
|
| 62 | public ItemStack(Block blockIn)
|
| 63 | {
|
| 64 | this((Block)blockIn, 1);
|
| 65 | }
|
| 66 |
|
| 67 | public ItemStack(Block blockIn, int amount)
|
| 68 | {
|
| 69 | this((Block)blockIn, amount, 0);
|
| 70 | }
|
| 71 |
|
| 72 | public ItemStack(Block blockIn, int amount, int meta)
|
| 73 | {
|
| 74 | this(Item.getItemFromBlock(blockIn), amount, meta);
|
| 75 | }
|
| 76 |
|
| 77 | public ItemStack(Item itemIn)
|
| 78 | {
|
| 79 | this((Item)itemIn, 1);
|
| 80 | }
|
| 81 |
|
| 82 | public ItemStack(Item itemIn, int amount)
|
| 83 | {
|
| 84 | this((Item)itemIn, amount, 0);
|
| 85 | }
|
| 86 |
|
| 87 | public ItemStack(Item itemIn, int amount, int meta)
|
| 88 | {
|
| 89 | this.canDestroyCacheBlock = null;
|
| 90 | this.canDestroyCacheResult = false;
|
| 91 | this.canPlaceOnCacheBlock = null;
|
| 92 | this.canPlaceOnCacheResult = false;
|
| 93 | this.item = itemIn;
|
| 94 | this.stackSize = amount;
|
| 95 | this.itemDamage = meta;
|
| 96 |
|
| 97 | if (this.itemDamage < 0)
|
| 98 | {
|
| 99 | this.itemDamage = 0;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public static ItemStack loadItemStackFromNBT(NBTTagCompound nbt)
|
| 104 | {
|
| 105 | ItemStack itemstack = new ItemStack();
|
| 106 | itemstack.readFromNBT(nbt);
|
| 107 | return itemstack.getItem() != null ? itemstack : null;
|
| 108 | }
|
| 109 |
|
| 110 | private ItemStack()
|
| 111 | {
|
| 112 | this.canDestroyCacheBlock = null;
|
| 113 | this.canDestroyCacheResult = false;
|
| 114 | this.canPlaceOnCacheBlock = null;
|
| 115 | this.canPlaceOnCacheResult = false;
|
| 116 | }
|
| 117 |
|
| 118 | /**
|
| 119 | * Splits off a stack of the given amount of this stack and reduces this stack by the amount.
|
| 120 | */
|
| 121 | public ItemStack splitStack(int amount)
|
| 122 | {
|
| 123 | ItemStack itemstack = new ItemStack(this.item, amount, this.itemDamage);
|
| 124 |
|
| 125 | if (this.stackTagCompound != null)
|
| 126 | {
|
| 127 | itemstack.stackTagCompound = (NBTTagCompound)this.stackTagCompound.copy();
|
| 128 | }
|
| 129 |
|
| 130 | this.stackSize -= amount;
|
| 131 | return itemstack;
|
| 132 | }
|
| 133 |
|
| 134 | /**
|
| 135 | * Returns the object corresponding to the stack.
|
| 136 | */
|
| 137 | public Item getItem()
|
| 138 | {
|
| 139 | return this.item;
|
| 140 | }
|
| 141 |
|
| 142 | /**
|
| 143 | * Called when the player uses this ItemStack on a Block (right-click). Places blocks, etc. (Legacy name:
|
| 144 | * tryPlaceItemIntoWorld)
|
| 145 | */
|
| 146 | public boolean onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 147 | {
|
| 148 | boolean flag = this.getItem().onItemUse(this, playerIn, worldIn, pos, side, hitX, hitY, hitZ);
|
| 149 |
|
| 150 | if (flag)
|
| 151 | {
|
| 152 | playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this.item)]);
|
| 153 | }
|
| 154 |
|
| 155 | return flag;
|
| 156 | }
|
| 157 |
|
| 158 | public float getStrVsBlock(Block blockIn)
|
| 159 | {
|
| 160 | return this.getItem().getStrVsBlock(this, blockIn);
|
| 161 | }
|
| 162 |
|
| 163 | /**
|
| 164 | * Called whenever this item stack is equipped and right clicked. Returns the new item stack to put in the position
|
| 165 | * where this item is. Args: world, player
|
| 166 | */
|
| 167 | public ItemStack useItemRightClick(World worldIn, EntityPlayer playerIn)
|
| 168 | {
|
| 169 | return this.getItem().onItemRightClick(this, worldIn, playerIn);
|
| 170 | }
|
| 171 |
|
| 172 | /**
|
| 173 | * Called when the item in use count reach 0, e.g. item food eaten. Return the new ItemStack. Args : world, entity
|
| 174 | */
|
| 175 | public ItemStack onItemUseFinish(World worldIn, EntityPlayer playerIn)
|
| 176 | {
|
| 177 | return this.getItem().onItemUseFinish(this, worldIn, playerIn);
|
| 178 | }
|
| 179 |
|
| 180 | /**
|
| 181 | * Write the stack fields to a NBT object. Return the new NBT object.
|
| 182 | */
|
| 183 | public NBTTagCompound writeToNBT(NBTTagCompound nbt)
|
| 184 | {
|
| 185 | ResourceLocation resourcelocation = (ResourceLocation)Item.itemRegistry.getNameForObject(this.item);
|
| 186 | nbt.setString("id", resourcelocation == null ? "minecraft:air" : resourcelocation.toString());
|
| 187 | nbt.setByte("Count", (byte)this.stackSize);
|
| 188 | nbt.setShort("Damage", (short)this.itemDamage);
|
| 189 |
|
| 190 | if (this.stackTagCompound != null)
|
| 191 | {
|
| 192 | nbt.setTag("tag", this.stackTagCompound);
|
| 193 | }
|
| 194 |
|
| 195 | return nbt;
|
| 196 | }
|
| 197 |
|
| 198 | /**
|
| 199 | * Read the stack fields from a NBT object.
|
| 200 | */
|
| 201 | public void readFromNBT(NBTTagCompound nbt)
|
| 202 | {
|
| 203 | if (nbt.hasKey("id", 8))
|
| 204 | {
|
| 205 | this.item = Item.getByNameOrId(nbt.getString("id"));
|
| 206 | }
|
| 207 | else
|
| 208 | {
|
| 209 | this.item = Item.getItemById(nbt.getShort("id"));
|
| 210 | }
|
| 211 |
|
| 212 | this.stackSize = nbt.getByte("Count");
|
| 213 | this.itemDamage = nbt.getShort("Damage");
|
| 214 |
|
| 215 | if (this.itemDamage < 0)
|
| 216 | {
|
| 217 | this.itemDamage = 0;
|
| 218 | }
|
| 219 |
|
| 220 | if (nbt.hasKey("tag", 10))
|
| 221 | {
|
| 222 | this.stackTagCompound = nbt.getCompoundTag("tag");
|
| 223 |
|
| 224 | if (this.item != null)
|
| 225 | {
|
| 226 | this.item.updateItemStackNBT(this.stackTagCompound);
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | /**
|
| 232 | * Returns maximum size of the stack.
|
| 233 | */
|
| 234 | public int getMaxStackSize()
|
| 235 | {
|
| 236 | return this.getItem().getItemStackLimit();
|
| 237 | }
|
| 238 |
|
| 239 | /**
|
| 240 | * Returns true if the ItemStack can hold 2 or more units of the item.
|
| 241 | */
|
| 242 | public boolean isStackable()
|
| 243 | {
|
| 244 | return this.getMaxStackSize() > 1 && (!this.isItemStackDamageable() || !this.isItemDamaged());
|
| 245 | }
|
| 246 |
|
| 247 | /**
|
| 248 | * true if this itemStack is damageable
|
| 249 | */
|
| 250 | public boolean isItemStackDamageable()
|
| 251 | {
|
| 252 | return this.item == null ? false : (this.item.getMaxDamage() <= 0 ? false : !this.hasTagCompound() || !this.getTagCompound().getBoolean("Unbreakable"));
|
| 253 | }
|
| 254 |
|
| 255 | public boolean getHasSubtypes()
|
| 256 | {
|
| 257 | return this.item.getHasSubtypes();
|
| 258 | }
|
| 259 |
|
| 260 | /**
|
| 261 | * returns true when a damageable item is damaged
|
| 262 | */
|
| 263 | public boolean isItemDamaged()
|
| 264 | {
|
| 265 | return this.isItemStackDamageable() && this.itemDamage > 0;
|
| 266 | }
|
| 267 |
|
| 268 | public int getItemDamage()
|
| 269 | {
|
| 270 | return this.itemDamage;
|
| 271 | }
|
| 272 |
|
| 273 | public int getMetadata()
|
| 274 | {
|
| 275 | return this.itemDamage;
|
| 276 | }
|
| 277 |
|
| 278 | public void setItemDamage(int meta)
|
| 279 | {
|
| 280 | this.itemDamage = meta;
|
| 281 |
|
| 282 | if (this.itemDamage < 0)
|
| 283 | {
|
| 284 | this.itemDamage = 0;
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | /**
|
| 289 | * Returns the max damage an item in the stack can take.
|
| 290 | */
|
| 291 | public int getMaxDamage()
|
| 292 | {
|
| 293 | return this.item.getMaxDamage();
|
| 294 | }
|
| 295 |
|
| 296 | /**
|
| 297 | * Attempts to damage the ItemStack with par1 amount of damage, If the ItemStack has the Unbreaking enchantment
|
| 298 | * there is a chance for each point of damage to be negated. Returns true if it takes more damage than
|
| 299 | * getMaxDamage(). Returns false otherwise or if the ItemStack can't be damaged or if all points of damage are
|
| 300 | * negated.
|
| 301 | */
|
| 302 | public boolean attemptDamageItem(int amount, Random rand)
|
| 303 | {
|
| 304 | if (!this.isItemStackDamageable())
|
| 305 | {
|
| 306 | return false;
|
| 307 | }
|
| 308 | else
|
| 309 | {
|
| 310 | if (amount > 0)
|
| 311 | {
|
| 312 | int i = EnchantmentHelper.getEnchantmentLevel(Enchantment.unbreaking.effectId, this);
|
| 313 | int j = 0;
|
| 314 |
|
| 315 | for (int k = 0; i > 0 && k < amount; ++k)
|
| 316 | {
|
| 317 | if (EnchantmentDurability.negateDamage(this, i, rand))
|
| 318 | {
|
| 319 | ++j;
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | amount -= j;
|
| 324 |
|
| 325 | if (amount <= 0)
|
| 326 | {
|
| 327 | return false;
|
| 328 | }
|
| 329 | }
|
| 330 |
|
| 331 | this.itemDamage += amount;
|
| 332 | return this.itemDamage > this.getMaxDamage();
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | /**
|
| 337 | * Damages the item in the ItemStack
|
| 338 | */
|
| 339 | public void damageItem(int amount, EntityLivingBase entityIn)
|
| 340 | {
|
| 341 | if (!(entityIn instanceof EntityPlayer) || !((EntityPlayer)entityIn).capabilities.isCreativeMode)
|
| 342 | {
|
| 343 | if (this.isItemStackDamageable())
|
| 344 | {
|
| 345 | if (this.attemptDamageItem(amount, entityIn.getRNG()))
|
| 346 | {
|
| 347 | entityIn.renderBrokenItemStack(this);
|
| 348 | --this.stackSize;
|
| 349 |
|
| 350 | if (entityIn instanceof EntityPlayer)
|
| 351 | {
|
| 352 | EntityPlayer entityplayer = (EntityPlayer)entityIn;
|
| 353 | entityplayer.triggerAchievement(StatList.objectBreakStats[Item.getIdFromItem(this.item)]);
|
| 354 |
|
| 355 | if (this.stackSize == 0 && this.getItem() instanceof ItemBow)
|
| 356 | {
|
| 357 | entityplayer.destroyCurrentEquippedItem();
|
| 358 | }
|
| 359 | }
|
| 360 |
|
| 361 | if (this.stackSize < 0)
|
| 362 | {
|
| 363 | this.stackSize = 0;
|
| 364 | }
|
| 365 |
|
| 366 | this.itemDamage = 0;
|
| 367 | }
|
| 368 | }
|
| 369 | }
|
| 370 | }
|
| 371 |
|
| 372 | /**
|
| 373 | * Calls the corresponding fct in di
|
| 374 | */
|
| 375 | public void hitEntity(EntityLivingBase entityIn, EntityPlayer playerIn)
|
| 376 | {
|
| 377 | boolean flag = this.item.hitEntity(this, entityIn, playerIn);
|
| 378 |
|
| 379 | if (flag)
|
| 380 | {
|
| 381 | playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this.item)]);
|
| 382 | }
|
| 383 | }
|
| 384 |
|
| 385 | /**
|
| 386 | * Called when a Block is destroyed using this ItemStack
|
| 387 | */
|
| 388 | public void onBlockDestroyed(World worldIn, Block blockIn, BlockPos pos, EntityPlayer playerIn)
|
| 389 | {
|
| 390 | boolean flag = this.item.onBlockDestroyed(this, worldIn, blockIn, pos, playerIn);
|
| 391 |
|
| 392 | if (flag)
|
| 393 | {
|
| 394 | playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this.item)]);
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | /**
|
| 399 | * Check whether the given Block can be harvested using this ItemStack.
|
| 400 | */
|
| 401 | public boolean canHarvestBlock(Block blockIn)
|
| 402 | {
|
| 403 | return this.item.canHarvestBlock(blockIn);
|
| 404 | }
|
| 405 |
|
| 406 | public boolean interactWithEntity(EntityPlayer playerIn, EntityLivingBase entityIn)
|
| 407 | {
|
| 408 | return this.item.itemInteractionForEntity(this, playerIn, entityIn);
|
| 409 | }
|
| 410 |
|
| 411 | /**
|
| 412 | * Returns a new stack with the same properties.
|
| 413 | */
|
| 414 | public ItemStack copy()
|
| 415 | {
|
| 416 | ItemStack itemstack = new ItemStack(this.item, this.stackSize, this.itemDamage);
|
| 417 |
|
| 418 | if (this.stackTagCompound != null)
|
| 419 | {
|
| 420 | itemstack.stackTagCompound = (NBTTagCompound)this.stackTagCompound.copy();
|
| 421 | }
|
| 422 |
|
| 423 | return itemstack;
|
| 424 | }
|
| 425 |
|
| 426 | public static boolean areItemStackTagsEqual(ItemStack stackA, ItemStack stackB)
|
| 427 | {
|
| 428 | return stackA == null && stackB == null ? true : (stackA != null && stackB != null ? (stackA.stackTagCompound == null && stackB.stackTagCompound != null ? false : stackA.stackTagCompound == null || stackA.stackTagCompound.equals(stackB.stackTagCompound)) : false);
|
| 429 | }
|
| 430 |
|
| 431 | /**
|
| 432 | * compares ItemStack argument1 with ItemStack argument2; returns true if both ItemStacks are equal
|
| 433 | */
|
| 434 | public static boolean areItemStacksEqual(ItemStack stackA, ItemStack stackB)
|
| 435 | {
|
| 436 | return stackA == null && stackB == null ? true : (stackA != null && stackB != null ? stackA.isItemStackEqual(stackB) : false);
|
| 437 | }
|
| 438 |
|
| 439 | /**
|
| 440 | * compares ItemStack argument to the instance ItemStack; returns true if both ItemStacks are equal
|
| 441 | */
|
| 442 | private boolean isItemStackEqual(ItemStack other)
|
| 443 | {
|
| 444 | return this.stackSize != other.stackSize ? false : (this.item != other.item ? false : (this.itemDamage != other.itemDamage ? false : (this.stackTagCompound == null && other.stackTagCompound != null ? false : this.stackTagCompound == null || this.stackTagCompound.equals(other.stackTagCompound))));
|
| 445 | }
|
| 446 |
|
| 447 | /**
|
| 448 | * Compares Item and damage value of the two stacks
|
| 449 | */
|
| 450 | public static boolean areItemsEqual(ItemStack stackA, ItemStack stackB)
|
| 451 | {
|
| 452 | return stackA == null && stackB == null ? true : (stackA != null && stackB != null ? stackA.isItemEqual(stackB) : false);
|
| 453 | }
|
| 454 |
|
| 455 | /**
|
| 456 | * compares ItemStack argument to the instance ItemStack; returns true if the Items contained in both ItemStacks are
|
| 457 | * equal
|
| 458 | */
|
| 459 | public boolean isItemEqual(ItemStack other)
|
| 460 | {
|
| 461 | return other != null && this.item == other.item && this.itemDamage == other.itemDamage;
|
| 462 | }
|
| 463 |
|
| 464 | public String getUnlocalizedName()
|
| 465 | {
|
| 466 | return this.item.getUnlocalizedName(this);
|
| 467 | }
|
| 468 |
|
| 469 | /**
|
| 470 | * Creates a copy of a ItemStack, a null parameters will return a null.
|
| 471 | */
|
| 472 | public static ItemStack copyItemStack(ItemStack stack)
|
| 473 | {
|
| 474 | return stack == null ? null : stack.copy();
|
| 475 | }
|
| 476 |
|
| 477 | public String toString()
|
| 478 | {
|
| 479 | return this.stackSize + "x" + this.item.getUnlocalizedName() + "@" + this.itemDamage;
|
| 480 | }
|
| 481 |
|
| 482 | /**
|
| 483 | * Called each tick as long the ItemStack in on player inventory. Used to progress the pickup animation and update
|
| 484 | * maps.
|
| 485 | */
|
| 486 | public void updateAnimation(World worldIn, Entity entityIn, int inventorySlot, boolean isCurrentItem)
|
| 487 | {
|
| 488 | if (this.animationsToGo > 0)
|
| 489 | {
|
| 490 | --this.animationsToGo;
|
| 491 | }
|
| 492 |
|
| 493 | this.item.onUpdate(this, worldIn, entityIn, inventorySlot, isCurrentItem);
|
| 494 | }
|
| 495 |
|
| 496 | public void onCrafting(World worldIn, EntityPlayer playerIn, int amount)
|
| 497 | {
|
| 498 | playerIn.addStat(StatList.objectCraftStats[Item.getIdFromItem(this.item)], amount);
|
| 499 | this.item.onCreated(this, worldIn, playerIn);
|
| 500 | }
|
| 501 |
|
| 502 | public boolean getIsItemStackEqual(ItemStack p_179549_1_)
|
| 503 | {
|
| 504 | return this.isItemStackEqual(p_179549_1_);
|
| 505 | }
|
| 506 |
|
| 507 | public int getMaxItemUseDuration()
|
| 508 | {
|
| 509 | return this.getItem().getMaxItemUseDuration(this);
|
| 510 | }
|
| 511 |
|
| 512 | public EnumAction getItemUseAction()
|
| 513 | {
|
| 514 | return this.getItem().getItemUseAction(this);
|
| 515 | }
|
| 516 |
|
| 517 | /**
|
| 518 | * Called when the player releases the use item button. Args: world, entityplayer, itemInUseCount
|
| 519 | */
|
| 520 | public void onPlayerStoppedUsing(World worldIn, EntityPlayer playerIn, int timeLeft)
|
| 521 | {
|
| 522 | this.getItem().onPlayerStoppedUsing(this, worldIn, playerIn, timeLeft);
|
| 523 | }
|
| 524 |
|
| 525 | /**
|
| 526 | * Returns true if the ItemStack has an NBTTagCompound. Currently used to store enchantments.
|
| 527 | */
|
| 528 | public boolean hasTagCompound()
|
| 529 | {
|
| 530 | return this.stackTagCompound != null;
|
| 531 | }
|
| 532 |
|
| 533 | /**
|
| 534 | * Returns the NBTTagCompound of the ItemStack.
|
| 535 | */
|
| 536 | public NBTTagCompound getTagCompound()
|
| 537 | {
|
| 538 | return this.stackTagCompound;
|
| 539 | }
|
| 540 |
|
| 541 | /**
|
| 542 | * Get an NBTTagCompound from this stack's NBT data.
|
| 543 | */
|
| 544 | public NBTTagCompound getSubCompound(String key, boolean create)
|
| 545 | {
|
| 546 | if (this.stackTagCompound != null && this.stackTagCompound.hasKey(key, 10))
|
| 547 | {
|
| 548 | return this.stackTagCompound.getCompoundTag(key);
|
| 549 | }
|
| 550 | else if (create)
|
| 551 | {
|
| 552 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 553 | this.setTagInfo(key, nbttagcompound);
|
| 554 | return nbttagcompound;
|
| 555 | }
|
| 556 | else
|
| 557 | {
|
| 558 | return null;
|
| 559 | }
|
| 560 | }
|
| 561 |
|
| 562 | public NBTTagList getEnchantmentTagList()
|
| 563 | {
|
| 564 | return this.stackTagCompound == null ? null : this.stackTagCompound.getTagList("ench", 10);
|
| 565 | }
|
| 566 |
|
| 567 | /**
|
| 568 | * Assigns a NBTTagCompound to the ItemStack, minecraft validates that only non-stackable items can have it.
|
| 569 | */
|
| 570 | public void setTagCompound(NBTTagCompound nbt)
|
| 571 | {
|
| 572 | this.stackTagCompound = nbt;
|
| 573 | }
|
| 574 |
|
| 575 | /**
|
| 576 | * returns the display name of the itemstack
|
| 577 | */
|
| 578 | public String getDisplayName()
|
| 579 | {
|
| 580 | String s = this.getItem().getItemStackDisplayName(this);
|
| 581 |
|
| 582 | if (this.stackTagCompound != null && this.stackTagCompound.hasKey("display", 10))
|
| 583 | {
|
| 584 | NBTTagCompound nbttagcompound = this.stackTagCompound.getCompoundTag("display");
|
| 585 |
|
| 586 | if (nbttagcompound.hasKey("Name", 8))
|
| 587 | {
|
| 588 | s = nbttagcompound.getString("Name");
|
| 589 | }
|
| 590 | }
|
| 591 |
|
| 592 | return s;
|
| 593 | }
|
| 594 |
|
| 595 | public ItemStack setStackDisplayName(String displayName)
|
| 596 | {
|
| 597 | if (this.stackTagCompound == null)
|
| 598 | {
|
| 599 | this.stackTagCompound = new NBTTagCompound();
|
| 600 | }
|
| 601 |
|
| 602 | if (!this.stackTagCompound.hasKey("display", 10))
|
| 603 | {
|
| 604 | this.stackTagCompound.setTag("display", new NBTTagCompound());
|
| 605 | }
|
| 606 |
|
| 607 | this.stackTagCompound.getCompoundTag("display").setString("Name", displayName);
|
| 608 | return this;
|
| 609 | }
|
| 610 |
|
| 611 | /**
|
| 612 | * Clear any custom name set for this ItemStack
|
| 613 | */
|
| 614 | public void clearCustomName()
|
| 615 | {
|
| 616 | if (this.stackTagCompound != null)
|
| 617 | {
|
| 618 | if (this.stackTagCompound.hasKey("display", 10))
|
| 619 | {
|
| 620 | NBTTagCompound nbttagcompound = this.stackTagCompound.getCompoundTag("display");
|
| 621 | nbttagcompound.removeTag("Name");
|
| 622 |
|
| 623 | if (nbttagcompound.hasNoTags())
|
| 624 | {
|
| 625 | this.stackTagCompound.removeTag("display");
|
| 626 |
|
| 627 | if (this.stackTagCompound.hasNoTags())
|
| 628 | {
|
| 629 | this.setTagCompound((NBTTagCompound)null);
|
| 630 | }
|
| 631 | }
|
| 632 | }
|
| 633 | }
|
| 634 | }
|
| 635 |
|
| 636 | /**
|
| 637 | * Returns true if the itemstack has a display name
|
| 638 | */
|
| 639 | public boolean hasDisplayName()
|
| 640 | {
|
| 641 | return this.stackTagCompound == null ? false : (!this.stackTagCompound.hasKey("display", 10) ? false : this.stackTagCompound.getCompoundTag("display").hasKey("Name", 8));
|
| 642 | }
|
| 643 |
|
| 644 | public List<String> getTooltip(EntityPlayer playerIn, boolean advanced)
|
| 645 | {
|
| 646 | List<String> list = Lists.<String>newArrayList();
|
| 647 | String s = this.getDisplayName();
|
| 648 |
|
| 649 | if (this.hasDisplayName())
|
| 650 | {
|
| 651 | s = EnumChatFormatting.ITALIC + s;
|
| 652 | }
|
| 653 |
|
| 654 | s = s + EnumChatFormatting.RESET;
|
| 655 |
|
| 656 | if (advanced)
|
| 657 | {
|
| 658 | String s1 = "";
|
| 659 |
|
| 660 | if (s.length() > 0)
|
| 661 | {
|
| 662 | s = s + " (";
|
| 663 | s1 = ")";
|
| 664 | }
|
| 665 |
|
| 666 | int i = Item.getIdFromItem(this.item);
|
| 667 |
|
| 668 | if (this.getHasSubtypes())
|
| 669 | {
|
| 670 | s = s + String.format("#%04d/%d%s", new Object[] {Integer.valueOf(i), Integer.valueOf(this.itemDamage), s1});
|
| 671 | }
|
| 672 | else
|
| 673 | {
|
| 674 | s = s + String.format("#%04d%s", new Object[] {Integer.valueOf(i), s1});
|
| 675 | }
|
| 676 | }
|
| 677 | else if (!this.hasDisplayName() && this.item == Items.filled_map)
|
| 678 | {
|
| 679 | s = s + " #" + this.itemDamage;
|
| 680 | }
|
| 681 |
|
| 682 | list.add(s);
|
| 683 | int i1 = 0;
|
| 684 |
|
| 685 | if (this.hasTagCompound() && this.stackTagCompound.hasKey("HideFlags", 99))
|
| 686 | {
|
| 687 | i1 = this.stackTagCompound.getInteger("HideFlags");
|
| 688 | }
|
| 689 |
|
| 690 | if ((i1 & 32) == 0)
|
| 691 | {
|
| 692 | this.item.addInformation(this, playerIn, list, advanced);
|
| 693 | }
|
| 694 |
|
| 695 | if (this.hasTagCompound())
|
| 696 | {
|
| 697 | if ((i1 & 1) == 0)
|
| 698 | {
|
| 699 | NBTTagList nbttaglist = this.getEnchantmentTagList();
|
| 700 |
|
| 701 | if (nbttaglist != null)
|
| 702 | {
|
| 703 | for (int j = 0; j < nbttaglist.tagCount(); ++j)
|
| 704 | {
|
| 705 | int k = nbttaglist.getCompoundTagAt(j).getShort("id");
|
| 706 | int l = nbttaglist.getCompoundTagAt(j).getShort("lvl");
|
| 707 |
|
| 708 | if (Enchantment.getEnchantmentById(k) != null)
|
| 709 | {
|
| 710 | list.add(Enchantment.getEnchantmentById(k).getTranslatedName(l));
|
| 711 | }
|
| 712 | }
|
| 713 | }
|
| 714 | }
|
| 715 |
|
| 716 | if (this.stackTagCompound.hasKey("display", 10))
|
| 717 | {
|
| 718 | NBTTagCompound nbttagcompound = this.stackTagCompound.getCompoundTag("display");
|
| 719 |
|
| 720 | if (nbttagcompound.hasKey("color", 3))
|
| 721 | {
|
| 722 | if (advanced)
|
| 723 | {
|
| 724 | list.add("Color: #" + Integer.toHexString(nbttagcompound.getInteger("color")).toUpperCase());
|
| 725 | }
|
| 726 | else
|
| 727 | {
|
| 728 | list.add(EnumChatFormatting.ITALIC + StatCollector.translateToLocal("item.dyed"));
|
| 729 | }
|
| 730 | }
|
| 731 |
|
| 732 | if (nbttagcompound.getTagId("Lore") == 9)
|
| 733 | {
|
| 734 | NBTTagList nbttaglist1 = nbttagcompound.getTagList("Lore", 8);
|
| 735 |
|
| 736 | if (nbttaglist1.tagCount() > 0)
|
| 737 | {
|
| 738 | for (int j1 = 0; j1 < nbttaglist1.tagCount(); ++j1)
|
| 739 | {
|
| 740 | list.add(EnumChatFormatting.DARK_PURPLE + "" + EnumChatFormatting.ITALIC + nbttaglist1.getStringTagAt(j1));
|
| 741 | }
|
| 742 | }
|
| 743 | }
|
| 744 | }
|
| 745 | }
|
| 746 |
|
| 747 | Multimap<String, AttributeModifier> multimap = this.getAttributeModifiers();
|
| 748 |
|
| 749 | if (!multimap.isEmpty() && (i1 & 2) == 0)
|
| 750 | {
|
| 751 | list.add("");
|
| 752 |
|
| 753 | for (Entry<String, AttributeModifier> entry : multimap.entries())
|
| 754 | {
|
| 755 | AttributeModifier attributemodifier = (AttributeModifier)entry.getValue();
|
| 756 | double d0 = attributemodifier.getAmount();
|
| 757 |
|
| 758 | if (attributemodifier.getID() == Item.itemModifierUUID)
|
| 759 | {
|
| 760 | d0 += (double)EnchantmentHelper.getModifierForCreature(this, EnumCreatureAttribute.UNDEFINED);
|
| 761 | }
|
| 762 |
|
| 763 | double d1;
|
| 764 |
|
| 765 | if (attributemodifier.getOperation() != 1 && attributemodifier.getOperation() != 2)
|
| 766 | {
|
| 767 | d1 = d0;
|
| 768 | }
|
| 769 | else
|
| 770 | {
|
| 771 | d1 = d0 * 100.0D;
|
| 772 | }
|
| 773 |
|
| 774 | if (d0 > 0.0D)
|
| 775 | {
|
| 776 | list.add(EnumChatFormatting.BLUE + StatCollector.translateToLocalFormatted("attribute.modifier.plus." + attributemodifier.getOperation(), new Object[] {DECIMALFORMAT.format(d1), StatCollector.translateToLocal("attribute.name." + (String)entry.getKey())}));
|
| 777 | }
|
| 778 | else if (d0 < 0.0D)
|
| 779 | {
|
| 780 | d1 = d1 * -1.0D;
|
| 781 | list.add(EnumChatFormatting.RED + StatCollector.translateToLocalFormatted("attribute.modifier.take." + attributemodifier.getOperation(), new Object[] {DECIMALFORMAT.format(d1), StatCollector.translateToLocal("attribute.name." + (String)entry.getKey())}));
|
| 782 | }
|
| 783 | }
|
| 784 | }
|
| 785 |
|
| 786 | if (this.hasTagCompound() && this.getTagCompound().getBoolean("Unbreakable") && (i1 & 4) == 0)
|
| 787 | {
|
| 788 | list.add(EnumChatFormatting.BLUE + StatCollector.translateToLocal("item.unbreakable"));
|
| 789 | }
|
| 790 |
|
| 791 | if (this.hasTagCompound() && this.stackTagCompound.hasKey("CanDestroy", 9) && (i1 & 8) == 0)
|
| 792 | {
|
| 793 | NBTTagList nbttaglist2 = this.stackTagCompound.getTagList("CanDestroy", 8);
|
| 794 |
|
| 795 | if (nbttaglist2.tagCount() > 0)
|
| 796 | {
|
| 797 | list.add("");
|
| 798 | list.add(EnumChatFormatting.GRAY + StatCollector.translateToLocal("item.canBreak"));
|
| 799 |
|
| 800 | for (int k1 = 0; k1 < nbttaglist2.tagCount(); ++k1)
|
| 801 | {
|
| 802 | Block block = Block.getBlockFromName(nbttaglist2.getStringTagAt(k1));
|
| 803 |
|
| 804 | if (block != null)
|
| 805 | {
|
| 806 | list.add(EnumChatFormatting.DARK_GRAY + block.getLocalizedName());
|
| 807 | }
|
| 808 | else
|
| 809 | {
|
| 810 | list.add(EnumChatFormatting.DARK_GRAY + "missingno");
|
| 811 | }
|
| 812 | }
|
| 813 | }
|
| 814 | }
|
| 815 |
|
| 816 | if (this.hasTagCompound() && this.stackTagCompound.hasKey("CanPlaceOn", 9) && (i1 & 16) == 0)
|
| 817 | {
|
| 818 | NBTTagList nbttaglist3 = this.stackTagCompound.getTagList("CanPlaceOn", 8);
|
| 819 |
|
| 820 | if (nbttaglist3.tagCount() > 0)
|
| 821 | {
|
| 822 | list.add("");
|
| 823 | list.add(EnumChatFormatting.GRAY + StatCollector.translateToLocal("item.canPlace"));
|
| 824 |
|
| 825 | for (int l1 = 0; l1 < nbttaglist3.tagCount(); ++l1)
|
| 826 | {
|
| 827 | Block block1 = Block.getBlockFromName(nbttaglist3.getStringTagAt(l1));
|
| 828 |
|
| 829 | if (block1 != null)
|
| 830 | {
|
| 831 | list.add(EnumChatFormatting.DARK_GRAY + block1.getLocalizedName());
|
| 832 | }
|
| 833 | else
|
| 834 | {
|
| 835 | list.add(EnumChatFormatting.DARK_GRAY + "missingno");
|
| 836 | }
|
| 837 | }
|
| 838 | }
|
| 839 | }
|
| 840 |
|
| 841 | if (advanced)
|
| 842 | {
|
| 843 | if (this.isItemDamaged())
|
| 844 | {
|
| 845 | list.add("Durability: " + (this.getMaxDamage() - this.getItemDamage()) + " / " + this.getMaxDamage());
|
| 846 | }
|
| 847 |
|
| 848 | list.add(EnumChatFormatting.DARK_GRAY + ((ResourceLocation)Item.itemRegistry.getNameForObject(this.item)).toString());
|
| 849 |
|
| 850 | if (this.hasTagCompound())
|
| 851 | {
|
| 852 | list.add(EnumChatFormatting.DARK_GRAY + "NBT: " + this.getTagCompound().getKeySet().size() + " tag(s)");
|
| 853 | }
|
| 854 | }
|
| 855 |
|
| 856 | return list;
|
| 857 | }
|
| 858 |
|
| 859 | public boolean hasEffect()
|
| 860 | {
|
| 861 | return this.getItem().hasEffect(this);
|
| 862 | }
|
| 863 |
|
| 864 | public EnumRarity getRarity()
|
| 865 | {
|
| 866 | return this.getItem().getRarity(this);
|
| 867 | }
|
| 868 |
|
| 869 | /**
|
| 870 | * True if it is a tool and has no enchantments to begin with
|
| 871 | */
|
| 872 | public boolean isItemEnchantable()
|
| 873 | {
|
| 874 | return !this.getItem().isItemTool(this) ? false : !this.isItemEnchanted();
|
| 875 | }
|
| 876 |
|
| 877 | /**
|
| 878 | * Adds an enchantment with a desired level on the ItemStack.
|
| 879 | */
|
| 880 | public void addEnchantment(Enchantment ench, int level)
|
| 881 | {
|
| 882 | if (this.stackTagCompound == null)
|
| 883 | {
|
| 884 | this.setTagCompound(new NBTTagCompound());
|
| 885 | }
|
| 886 |
|
| 887 | if (!this.stackTagCompound.hasKey("ench", 9))
|
| 888 | {
|
| 889 | this.stackTagCompound.setTag("ench", new NBTTagList());
|
| 890 | }
|
| 891 |
|
| 892 | NBTTagList nbttaglist = this.stackTagCompound.getTagList("ench", 10);
|
| 893 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 894 | nbttagcompound.setShort("id", (short)ench.effectId);
|
| 895 | nbttagcompound.setShort("lvl", (short)((byte)level));
|
| 896 | nbttaglist.appendTag(nbttagcompound);
|
| 897 | }
|
| 898 |
|
| 899 | /**
|
| 900 | * True if the item has enchantment data
|
| 901 | */
|
| 902 | public boolean isItemEnchanted()
|
| 903 | {
|
| 904 | return this.stackTagCompound != null && this.stackTagCompound.hasKey("ench", 9);
|
| 905 | }
|
| 906 |
|
| 907 | public void setTagInfo(String key, NBTBase value)
|
| 908 | {
|
| 909 | if (this.stackTagCompound == null)
|
| 910 | {
|
| 911 | this.setTagCompound(new NBTTagCompound());
|
| 912 | }
|
| 913 |
|
| 914 | this.stackTagCompound.setTag(key, value);
|
| 915 | }
|
| 916 |
|
| 917 | public boolean canEditBlocks()
|
| 918 | {
|
| 919 | return this.getItem().canItemEditBlocks();
|
| 920 | }
|
| 921 |
|
| 922 | /**
|
| 923 | * Return whether this stack is on an item frame.
|
| 924 | */
|
| 925 | public boolean isOnItemFrame()
|
| 926 | {
|
| 927 | return this.itemFrame != null;
|
| 928 | }
|
| 929 |
|
| 930 | /**
|
| 931 | * Set the item frame this stack is on.
|
| 932 | */
|
| 933 | public void setItemFrame(EntityItemFrame frame)
|
| 934 | {
|
| 935 | this.itemFrame = frame;
|
| 936 | }
|
| 937 |
|
| 938 | /**
|
| 939 | * Return the item frame this stack is on. Returns null if not on an item frame.
|
| 940 | */
|
| 941 | public EntityItemFrame getItemFrame()
|
| 942 | {
|
| 943 | return this.itemFrame;
|
| 944 | }
|
| 945 |
|
| 946 | /**
|
| 947 | * Get this stack's repair cost, or 0 if no repair cost is defined.
|
| 948 | */
|
| 949 | public int getRepairCost()
|
| 950 | {
|
| 951 | return this.hasTagCompound() && this.stackTagCompound.hasKey("RepairCost", 3) ? this.stackTagCompound.getInteger("RepairCost") : 0;
|
| 952 | }
|
| 953 |
|
| 954 | /**
|
| 955 | * Set this stack's repair cost.
|
| 956 | */
|
| 957 | public void setRepairCost(int cost)
|
| 958 | {
|
| 959 | if (!this.hasTagCompound())
|
| 960 | {
|
| 961 | this.stackTagCompound = new NBTTagCompound();
|
| 962 | }
|
| 963 |
|
| 964 | this.stackTagCompound.setInteger("RepairCost", cost);
|
| 965 | }
|
| 966 |
|
| 967 | public Multimap<String, AttributeModifier> getAttributeModifiers()
|
| 968 | {
|
| 969 | Multimap<String, AttributeModifier> multimap;
|
| 970 |
|
| 971 | if (this.hasTagCompound() && this.stackTagCompound.hasKey("AttributeModifiers", 9))
|
| 972 | {
|
| 973 | multimap = HashMultimap.<String, AttributeModifier>create();
|
| 974 | NBTTagList nbttaglist = this.stackTagCompound.getTagList("AttributeModifiers", 10);
|
| 975 |
|
| 976 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 977 | {
|
| 978 | NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
| 979 | AttributeModifier attributemodifier = SharedMonsterAttributes.readAttributeModifierFromNBT(nbttagcompound);
|
| 980 |
|
| 981 | if (attributemodifier != null && attributemodifier.getID().getLeastSignificantBits() != 0L && attributemodifier.getID().getMostSignificantBits() != 0L)
|
| 982 | {
|
| 983 | multimap.put(nbttagcompound.getString("AttributeName"), attributemodifier);
|
| 984 | }
|
| 985 | }
|
| 986 | }
|
| 987 | else
|
| 988 | {
|
| 989 | multimap = this.getItem().getItemAttributeModifiers();
|
| 990 | }
|
| 991 |
|
| 992 | return multimap;
|
| 993 | }
|
| 994 |
|
| 995 | public void setItem(Item newItem)
|
| 996 | {
|
| 997 | this.item = newItem;
|
| 998 | }
|
| 999 |
|
| 1000 | /**
|
| 1001 | * Get a ChatComponent for this Item's display name that shows this Item on hover
|
| 1002 | */
|
| 1003 | public IChatComponent getChatComponent()
|
| 1004 | {
|
| 1005 | ChatComponentText chatcomponenttext = new ChatComponentText(this.getDisplayName());
|
| 1006 |
|
| 1007 | if (this.hasDisplayName())
|
| 1008 | {
|
| 1009 | chatcomponenttext.getChatStyle().setItalic(Boolean.valueOf(true));
|
| 1010 | }
|
| 1011 |
|
| 1012 | IChatComponent ichatcomponent = (new ChatComponentText("[")).appendSibling(chatcomponenttext).appendText("]");
|
| 1013 |
|
| 1014 | if (this.item != null)
|
| 1015 | {
|
| 1016 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 1017 | this.writeToNBT(nbttagcompound);
|
| 1018 | ichatcomponent.getChatStyle().setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new ChatComponentText(nbttagcompound.toString())));
|
| 1019 | ichatcomponent.getChatStyle().setColor(this.getRarity().rarityColor);
|
| 1020 | }
|
| 1021 |
|
| 1022 | return ichatcomponent;
|
| 1023 | }
|
| 1024 |
|
| 1025 | public boolean canDestroy(Block blockIn)
|
| 1026 | {
|
| 1027 | if (blockIn == this.canDestroyCacheBlock)
|
| 1028 | {
|
| 1029 | return this.canDestroyCacheResult;
|
| 1030 | }
|
| 1031 | else
|
| 1032 | {
|
| 1033 | this.canDestroyCacheBlock = blockIn;
|
| 1034 |
|
| 1035 | if (this.hasTagCompound() && this.stackTagCompound.hasKey("CanDestroy", 9))
|
| 1036 | {
|
| 1037 | NBTTagList nbttaglist = this.stackTagCompound.getTagList("CanDestroy", 8);
|
| 1038 |
|
| 1039 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 1040 | {
|
| 1041 | Block block = Block.getBlockFromName(nbttaglist.getStringTagAt(i));
|
| 1042 |
|
| 1043 | if (block == blockIn)
|
| 1044 | {
|
| 1045 | this.canDestroyCacheResult = true;
|
| 1046 | return true;
|
| 1047 | }
|
| 1048 | }
|
| 1049 | }
|
| 1050 |
|
| 1051 | this.canDestroyCacheResult = false;
|
| 1052 | return false;
|
| 1053 | }
|
| 1054 | }
|
| 1055 |
|
| 1056 | public boolean canPlaceOn(Block blockIn)
|
| 1057 | {
|
| 1058 | if (blockIn == this.canPlaceOnCacheBlock)
|
| 1059 | {
|
| 1060 | return this.canPlaceOnCacheResult;
|
| 1061 | }
|
| 1062 | else
|
| 1063 | {
|
| 1064 | this.canPlaceOnCacheBlock = blockIn;
|
| 1065 |
|
| 1066 | if (this.hasTagCompound() && this.stackTagCompound.hasKey("CanPlaceOn", 9))
|
| 1067 | {
|
| 1068 | NBTTagList nbttaglist = this.stackTagCompound.getTagList("CanPlaceOn", 8);
|
| 1069 |
|
| 1070 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 1071 | {
|
| 1072 | Block block = Block.getBlockFromName(nbttaglist.getStringTagAt(i));
|
| 1073 |
|
| 1074 | if (block == blockIn)
|
| 1075 | {
|
| 1076 | this.canPlaceOnCacheResult = true;
|
| 1077 | return true;
|
| 1078 | }
|
| 1079 | }
|
| 1080 | }
|
| 1081 |
|
| 1082 | this.canPlaceOnCacheResult = false;
|
| 1083 | return false;
|
| 1084 | }
|
| 1085 | }
|
| 1086 | }
|
| 1087 |
|