All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minecraft.server.ItemStack Maven / Gradle / Ivy

package net.minecraft.server;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.bukkit.Location;
import org.bukkit.TreeType;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.block.CraftBlockState;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.Player;
import org.bukkit.event.world.StructureGrowEvent;
import org.jetbrains.annotations.NotNull;
import walkmc.annotation.FromWalk;
import walkmc.block.Click;
import walkmc.item.IItem;
import walkmc.item.ItemRegistry;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Random;

public final class ItemStack {
	
	public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.###");
	
	/**
	 * Size of the stack.
	 */
	public int count;
	
	/**
	 * Number of animation frames to go when receiving an item (by walking into it, for example).
	 */
	public int animationsToGo;
	public Item item;
	
	/**
	 * A NBTTagCompound containing data about an ItemStack.
	 */
	public NBTTagCompound tag;
	public int damage;
	
	/**
	 * Item frame this stack is on, or null if not on an item frame.
	 */
	public EntityItemFrame itemFrame;
	
	public Block canDestroyCacheBlock;
	public boolean canDestroyCacheResult;
	public Block canPlaceOnCacheBlock;
	public boolean canPlaceOnCacheResult;
	
	//
	//    From WalkMC section:
	//
	
	public IItem customItem;
	public CraftItemStack cachedCraftItem;
	
	//
	//    End of all sections.
	//
	
	public ItemStack(Block block) {
		this(block, 1);
	}
	
	public ItemStack(Block block, int i) {
		this(block, i, 0);
	}
	
	public ItemStack(Block block, int i, int j) {
		this(Item.getItemOf(block), i, j);
	}
	
	public ItemStack(Item item) {
		this(item, 1);
	}
	
	public ItemStack(Item item, int i) {
		this(item, i, 0);
	}
	
	public ItemStack(Item item, int i, int j) {
		this.canDestroyCacheBlock = null;
		this.canDestroyCacheResult = false;
		this.canPlaceOnCacheBlock = null;
		this.canPlaceOnCacheResult = false;
		this.item = item;
		this.count = i;
		this.setItemDamage(j);
		this.cachedCraftItem = toCraftItem(); // walkmc
	}
	
	private ItemStack() {
		this.canDestroyCacheBlock = null;
		this.canDestroyCacheResult = false;
		this.canPlaceOnCacheBlock = null;
		this.canPlaceOnCacheResult = false;
		this.cachedCraftItem = toCraftItem(); // walkmc
	}
	
	public static ItemStack createStack(NBTTagCompound nbttagcompound) {
		ItemStack itemstack = new ItemStack();
		
		itemstack.readFromNBT(nbttagcompound);
		return itemstack.getItem() != null ? itemstack : null;
	}
	
	public static boolean equals(ItemStack itemstack, ItemStack itemstack1) {
		return itemstack == null && itemstack1 == null || (itemstack != null && itemstack1 != null && ((itemstack.tag != null || itemstack1.tag == null) && (itemstack.tag == null || itemstack.tag.equals(itemstack1.tag))));
	}
	
	// Spigot Start
	public static boolean fastMatches(ItemStack itemstack, ItemStack itemstack1) {
		if (itemstack == null && itemstack1 == null) {
			return true;
		}
		if (itemstack != null && itemstack1 != null) {
			return itemstack.count == itemstack1.count && itemstack.item == itemstack1.item && itemstack.damage == itemstack1.damage;
		}
		return false;
	}
	
	public static boolean matches(ItemStack itemstack, ItemStack itemstack1) {
		return itemstack == null && itemstack1 == null || (itemstack != null && itemstack1 != null && itemstack.isItemStackEqual(itemstack1));
	}
	
	public static boolean c(ItemStack itemstack, ItemStack itemstack1) {
		return itemstack == null && itemstack1 == null || (itemstack != null && itemstack.doMaterialsMatch(itemstack1));
	}
	
	public static ItemStack b(ItemStack itemstack) {
		return itemstack == null ? null : itemstack.cloneItemStack();
	}
	
	public ItemStack cloneAndSubtract(int i) {
		ItemStack itemstack = new ItemStack(this.item, i, this.damage);
		
		// walkmc start
		if (customItem != null) {
			itemstack.customItem = customItem;
		}
		// walkmc end
		
		if (this.tag != null) {
			itemstack.tag = (NBTTagCompound) this.tag.clone();
		}
		
		this.count -= i;
		return itemstack;
	}
	
	public Item getItem() {
		return this.item;
	}
	
	public void setItem(Item item) {
		this.item = item;
		this.setItemDamage(this.getData()); // CraftBukkit - Set data again to ensure it is filtered properly
	}
	
	public boolean placeItem(EntityHuman player, World world, BlockPosition pos, EnumDirection enumdirection, float f, float f1, float f2) {
		// CraftBukkit start - handle all block place event logic here
		int data = this.getData();
		int count = this.count;
		
		if (!(this.getItem() instanceof ItemBucket)) { // if not bucket
			world.captureBlockStates = true;
			// special case bonemeal
			if (this.getItem() instanceof ItemDye && this.getData() == 15) {
				Block block = world.getType(pos).getBlock();
				if (block == Blocks.SAPLING || block instanceof BlockMushroom) {
					world.captureTreeGeneration = true;
				}
			}
		}
		boolean flag = this.getItem().interactWith(this, player, world, pos, enumdirection, f, f1, f2);
		int newData = this.getData();
		int newCount = this.count;
		this.count = count;
		this.setItemDamage(data);
		world.captureBlockStates = false;
		if (flag && world.captureTreeGeneration && world.capturedBlockStates.size() > 0) {
			world.captureTreeGeneration = false;
			Location location = new Location(world.getWorld(), pos.getX(), pos.getY(), pos.getZ());
			TreeType treeType = BlockSapling.treeType;
			BlockSapling.treeType = null;
			List blocks = (List) world.capturedBlockStates.clone();
			world.capturedBlockStates.clear();
			StructureGrowEvent event = null;
			if (treeType != null) {
				boolean isBonemeal = getItem() == Items.DYE && data == 15;
				event = new StructureGrowEvent(location, treeType, isBonemeal, (Player) player.getBukkitEntity(), blocks);
				org.bukkit.Bukkit.getPluginManager().callEvent(event);
			}
			if (event == null || !event.isCancelled()) {
				// Change the stack to its new contents if it hasn't been tampered with.
				if (this.count == count && this.getData() == data) {
					this.setItemDamage(newData);
					this.count = newCount;
				}
				for (BlockState blockstate : blocks) {
					blockstate.update(true);
				}
			}
			
			return true;
		}
		world.captureTreeGeneration = false;
		
		if (flag) {
			org.bukkit.event.block.BlockPlaceEvent placeEvent = null;
			List blocks = (List) world.capturedBlockStates.clone();
			world.capturedBlockStates.clear();
			if (blocks.size() > 1) {
				placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockMultiPlaceEvent(world, player, blocks, pos.getX(), pos.getY(), pos.getZ());
			} else if (blocks.size() == 1) {
				placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(world, player, blocks.get(0), pos.getX(), pos.getY(), pos.getZ());
			}
			
			// walkmc start
			if (!onPlaceBlock((Player) player.getEntityBukkit(), world.world.getBlockAt(pos))) {
				if (placeEvent != null)
					placeEvent.setCancelled(true);
			}
			// walkmc end
			
			if (placeEvent != null && (placeEvent.isCancelled() || !placeEvent.canBuild())) {
				flag = false; // cancel placement
				// revert back all captured blocks
				
				for (BlockState blockstate : blocks) {
					blockstate.update(true, false);
				}
			} else {
				// Change the stack to its new contents if it hasn't been tampered with.
				if (this.count == count && this.getData() == data) {
					this.setItemDamage(newData);
					this.count = newCount;
				}
				for (BlockState blockstate : blocks) {
					int x = blockstate.getX();
					int y = blockstate.getY();
					int z = blockstate.getZ();
					int updateFlag = ((CraftBlockState) blockstate).getFlag();
					org.bukkit.Material mat = blockstate.getType();
					Block oldBlock = CraftMagicNumbers.getBlock(mat);
					BlockPosition newblockposition = new BlockPosition(x, y, z);
					IBlockData block = world.getType(newblockposition);
					
					if (!(block instanceof BlockContainer)) { // Containers get placed automatically
						block.getBlock().onPlace(world, newblockposition, block);
					}
					
					world.notifyAndUpdatePhysics(newblockposition, null, oldBlock, block.getBlock(), updateFlag); // send null chunk as chunk.k() returns false by this point
				}
				
				for (Map.Entry e : world.capturedTileEntities.entrySet()) {
					world.setTileEntity(e.getKey(), e.getValue());
				}
				
				// Special case juke boxes as they update their tile entity. Copied from ItemRecord.
				if (this.getItem() instanceof ItemRecord) {
					((BlockJukeBox) Blocks.JUKEBOX).a(world, pos, world.getType(pos), this);
					world.a(null, 1005, pos, Item.getId(this.getItem()));
					--this.count;
					player.b(StatisticList.X);
				}
				
				if (this.getItem() == Items.SKULL) { // Special case skulls to allow wither spawns to be cancelled
					BlockPosition bp = pos;
					if (!world.getType(pos).getBlock().a(world, pos)) {
						if (!world.getType(pos).getBlock().getMaterial().isBuildable()) {
							bp = null;
						} else {
							bp = bp.shift(enumdirection);
						}
					}
					if (bp != null) {
						TileEntity te = world.getTileEntity(bp);
						if (te instanceof TileEntitySkull) {
							Blocks.SKULL.a(world, bp, (TileEntitySkull) te);
						}
					}
				}
				
				player.b(StatisticList.USE_ITEM_COUNT[Item.getId(this.item)]);
			}
		}
		world.capturedTileEntities.clear();
		world.capturedBlockStates.clear();
		// CraftBukkit end
		
		return flag;
	}
	
	// from -> a()
	public float getDestroySpeed(Block block) {
		return this.getItem().getDestroySpeed(this, block);
	}
	
	/**
	 * Called whenever this item stack is equipped and right clicked. Returns the new item stack to put in the position
	 * where this item is. Args: world, player
	 */
	// from -> a()
	public ItemStack useItemRightClick(World world, EntityHuman player) {
		return this.getItem().a(this, world, player);
	}
	
	/**
	 * Called when the item in use count reach 0, e.g. item food eaten. Return the new ItemStack. Args : world, entity
	 */
	// from -> b()
	public ItemStack onItemUseFinish(World world, EntityHuman entityhuman) {
		return this.getItem().b(this, world, entityhuman);
	}
	
	/**
	 * Write the stack fields to a NBT object. Return the new NBT object.
	 */
	// from -> save()
	public NBTTagCompound writeToNBT(NBTTagCompound root) {
		MinecraftKey minecraftkey = Item.REGISTRY.c(this.item);
		
		root.setString("id", minecraftkey == null ? "minecraft:air" : minecraftkey.toString());
		root.setByte("Count", (byte) this.count);
		root.setShort("Damage", (short) this.damage);
		
		if (tag != null) {
			if (customItem != null) {
				customItem.saveData(cachedCraftItem, tag);
			}
			
			root.set("tag", this.tag.clone());
		}
		
		if (customItem != null) {
			customItem.onSave(cachedCraftItem);
		}
		
		return root;
	}
	
	/**
	 * Read the stack fields from a NBT object.
	 */
	// from -> c()
	public void readFromNBT(NBTTagCompound tag) {
		if (tag.hasKeyOfType("id", 8)) {
			this.item = Item.d(tag.getString("id"));
		} else {
			this.item = Item.getById(tag.getShort("id"));
		}
		
		this.count = tag.getByte("Count");
		this.setItemDamage(tag.getShort("Damage"));
		
		if (tag.hasKeyOfType("tag", 10)) {
			this.tag = (NBTTagCompound) tag.getCompound("tag").clone();
			if (this.item != null) {
				this.item.a(this.tag);
			}
			
			// walkmc start
			if (this.tag.hasKey("CustomItemId")) {
				IItem customItem = ItemRegistry.find(this.tag.getString("CustomItemId"));
				if (customItem != null) {
					this.customItem = customItem;
					customItem.loadData(cachedCraftItem, this.tag);
				}
			}
			// walkmc end
		}
		
		if (customItem != null)
			this.customItem.onLoad(cachedCraftItem);
		
	}
	
	/**
	 * Returns maximum size of the stack.
	 */
	public int getMaxStackSize() {
		if (customItem != null) {
			return customItem.getMaxStack(cachedCraftItem);
		}
		return this.getItem().getMaxStackSize();
	}
	
	/**
	 * Returns true if the ItemStack can hold 2 or more units of the item.
	 */
	public boolean isStackable() {
		return this.getMaxStackSize() > 1 && (!this.isItemStackDamageable() || !this.isItemDamaged());
	}
	
	/**
	 * true if this itemStack is damageable
	 */
	// from -> e()
	public boolean isItemStackDamageable() {
		if (this.item.getMaxDurability() <= 0) {
			return false;
		}
		return (!hasTag()) || (!getTag().getBoolean("Unbreakable"));
	}
	
	// from -> usesData()
	public boolean getHasSubtypes() {
		return this.item.k();
	}
	
	/**
	 * returns true when a damageable item is damaged
	 */
	// from -> g()
	public boolean isItemDamaged() {
		return this.isItemStackDamageable() && this.damage > 0;
	}
	
	// from -> h()
	public int getItemDamage() {
		return this.damage;
	}
	
	// from -> setData()
	public void setItemDamage(int i) {
		// CraftBukkit start - Filter out data for items that shouldn't have it
		// The crafting system uses this value for a special purpose so we have to allow it
		if (i == 32767) {
			this.damage = i;
			return;
		}
		
		// Is this a block?
		if (CraftMagicNumbers.getBlock(CraftMagicNumbers.getId(this.getItem())) != Blocks.AIR) {
			// If vanilla doesn't use data on it don't allow any
			if (!(this.getHasSubtypes() || this.getItem().usesDurability())) {
				i = 0;
			}
		}
		
		// Filter invalid plant data
		if (CraftMagicNumbers.getBlock(CraftMagicNumbers.getId(this.getItem())) == Blocks.DOUBLE_PLANT && (i > 5 || i < 0)) {
			i = 0;
		}
		// CraftBukkit end
		this.damage = i;
		if (this.damage < -1) { // CraftBukkit
			this.damage = 0;
		}
		
	}
	
	// from -> getData()
	public int getData() {
		return this.damage;
	}
	
	/**
	 * Returns the max damage an item in the stack can take.
	 */
	// from -> j()
	public int getMaxDamage() {
		return this.item.getMaxDurability();
	}
	
	/**
	 * Attempts to damage the ItemStack with par1 amount of damage, If the ItemStack has the Unbreaking enchantment
	 * there is a chance for each point of damage to be negated. Returns true if it takes more damage than
	 * getMaxDamage(). Returns false otherwise or if the ItemStack can't be damaged or if all points of damage are
	 * negated.
	 */
	// from -> isDamaged()
	public boolean attemptDamageItem(int amound, Random random) {
		return attemptDamageItem(amound, random, null);
	}
	
	/**
	 * Attempts to damage the ItemStack with par1 amount of damage, If the ItemStack has the Unbreaking enchantment
	 * there is a chance for each point of damage to be negated. Returns true if it takes more damage than
	 * getMaxDamage(). Returns false otherwise or if the ItemStack can't be damaged or if all points of damage are
	 * negated.
	 */
	// from -> isDamaged()
	public boolean attemptDamageItem(int amount, Random random, EntityLiving entity) {
		// Spigot end
		if (!this.isItemStackDamageable() || !isDamageable()) {
			return false;
		} else {
			if (amount > 0) {
				int j = EnchantmentManager.getEnchantmentLevel(Enchantment.DURABILITY.id, this);
				int k = 0;
				
				for (int l = 0; j > 0 && l < amount; ++l) {
					if (EnchantmentDurability.a(this, j, random)) {
						++k;
					}
				}
				
				amount -= k;
				if (!onDamage(amount))
					return false;
				
				// Spigot start
				if (entity instanceof EntityPlayer) {
					org.bukkit.craftbukkit.inventory.CraftItemStack item = org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(this);
					org.bukkit.event.player.PlayerItemDamageEvent event = new org.bukkit.event.player.PlayerItemDamageEvent((org.bukkit.entity.Player) entity.getBukkitEntity(), item, amount);
					org.bukkit.Bukkit.getServer().getPluginManager().callEvent(event);
					if (!onDamageWith(((EntityPlayer) entity).getBukkitEntity(), amount)) return false;
					if (event.isCancelled()) return false;
					amount = event.getDamage();
				}
				// Spigot end
				if (amount <= 0) {
					return false;
				}
			}
			
			this.damage += amount;
			return this.damage > this.getMaxDamage();
		}
	}
	
	/**
	 * Damages the item in the ItemStack
	 */
	// from -> damage()
	public void damageItem(int amount, EntityLiving entity) {
		if (!(entity instanceof EntityHuman) || !((EntityHuman) entity).abilities.canInstantlyBuild) {
			if (this.isItemStackDamageable()) {
				if (this.attemptDamageItem(amount, entity.getRandom(), entity)) { // Spigot
					entity.renderBrokenItemStack(this);
					--this.count;
					if (entity instanceof EntityHuman) {
						EntityHuman entityhuman = (EntityHuman) entity;
						
						entityhuman.b(StatisticList.BREAK_ITEM_COUNT[Item.getId(this.item)]);
						if (this.count == 0 && this.getItem() instanceof ItemBow) {
							entityhuman.ca();
						}
					}
					
					if (this.count < 0) {
						this.count = 0;
					}
					
					// CraftBukkit start - Check for item breaking
					if (this.count == 0 && entity instanceof EntityHuman) {
						org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent((EntityHuman) entity, this);
						onItemBreak((Player) entity.getBukkitEntity());
					}
					// CraftBukkit end
					
					this.damage = 0;
				}
			}
		}
	}
	
	/**
	 * Calls the corresponding fct in di
	 */
	// from -> a()
	public void hitEntity(EntityLiving entity, EntityHuman player) {
		boolean flag = this.item.a(this, entity, player);
		
		if (flag) {
			player.b(StatisticList.USE_ITEM_COUNT[Item.getId(this.item)]);
		}
		
	}
	
	/**
	 * Called when a Block is destroyed using this ItemStack
	 */
	// from -> a()
	public void onBlockDestroyed(World world, Block block, BlockPosition pos, EntityHuman player) {
		if (customItem != null) {
			customItem.onBreak((Player) player.getBukkitEntity(), cachedCraftItem, world.world.getBlockAt(pos));
		}
		
		boolean flag = this.item.a(this, world, block, pos, player);
		
		if (flag) {
			player.b(StatisticList.USE_ITEM_COUNT[Item.getId(this.item)]);
		}
	}
	
	/**
	 * Check whether the given Block can be harvested using this ItemStack.
	 */
	// from -> b()
	public boolean canHarvestBlock(Block block) {
		return this.item.canDestroySpecialBlock(block);
	}
	
	// from -> a()
	public boolean interactWithEntity(EntityHuman player, EntityLiving entity) {
		return this.item.a(this, player, entity);
	}
	
	/**
	 * Returns a new stack with the same properties.
	 */
	public ItemStack cloneItemStack() {
		ItemStack itemstack = new ItemStack(this.item, this.count, this.damage);
		
		// walkmc
		if (customItem != null) {
			itemstack.customItem = customItem;
		}
		
		if (this.tag != null) {
			itemstack.tag = (NBTTagCompound) this.tag.clone();
		}
		
		return itemstack;
	}
	
	/**
	 * compares ItemStack argument to the instance ItemStack; returns true if both ItemStacks are equal
	 */
	// from -> d()
	private boolean isItemStackEqual(ItemStack itemstack) {
		// 		return this.count == itemstack.count && (this.item == itemstack.item && (this.damage == itemstack.damage && ((this.tag != null || itemstack.tag == null) && (this.tag == null || this.tag.equals(itemstack.tag)))));
		return this.count == itemstack.count && (this.item == itemstack.item && (this.damage == itemstack.damage && ((this.tag != null || itemstack.tag == null) && (this.tag == null || this.tag.equals(itemstack.tag)))));
	}
	
	public boolean doMaterialsMatch(ItemStack itemstack) {
		return itemstack != null && this.item == itemstack.item && this.damage == itemstack.damage;
	}
	
	// from -> a()
	public String getUnlocalizedName() {
		return this.item.e_(this);
	}
	
	public String toString() {
		return this.count + "x" + this.item.getName() + "@" + this.damage;
	}
	
	/**
	 * Called each tick as long the ItemStack in on player inventory.
	 * Used to progress the pickup animation and update maps.
	 */
	// from -> a()
	public void updateAnimation(World world, Entity entity, int i, boolean flag) {
		if (this.animationsToGo > 0) {
			--this.animationsToGo;
		}
		
		this.item.a(this, world, entity, i, flag);
	}
	
	// from -> a()
	public void onCrafting(World world, EntityHuman player, int amount) {
		player.a(StatisticList.CRAFT_BLOCK_COUNT[Item.getId(this.item)], amount);
		this.item.d(this, world, player);
	}
	
	// from -> l()
	public int getMaxItemUseDuration() {
		return this.getItem().d(this);
	}
	
	// from -> m()
	public EnumAnimation getItemAnimation() {
		return this.getItem().e(this);
	}
	
	/**
	 * Called when the player releases the use item button. Args: world, entityplayer, itemInUseCount
	 */
	// from -> b()
	public void onPlayerStoppedUsing(World world, EntityHuman player, int amount) {
		if (customItem != null) {
			customItem.onRelease((Player) player.getEntityBukkit(), cachedCraftItem, amount);
		}
		
		this.getItem().a(this, world, player, amount);
	}
	
	/**
	 * Returns true if the ItemStack has an NBTTagCompound. Currently used to store enchantments.
	 */
	public boolean hasTag() {
		return this.tag != null;
	}
	
	/**
	 * Returns the NBTTagCompound of the ItemStack.
	 */
	public NBTTagCompound getTag() {
		return this.tag;
	}
	
	/**
	 * Assigns a NBTTagCompound to the ItemStack.
	 */
	public void setTag(NBTTagCompound nbttagcompound) {
		this.tag = nbttagcompound;
	}
	
	public void addAll(NBTTagCompound value) {
		if (tag == null)
			tag = new NBTTagCompound();
		
		tag.addAllIfAbsent(value);
	}
	
	/**
	 * Get an NBTTagCompound from this stack's NBT data.
	 */
	// from -> a()
	public NBTTagCompound getSubCompound(String s, boolean flag) {
		if (this.tag != null && this.tag.hasKeyOfType(s, 10)) {
			return this.tag.getCompound(s);
		} else if (flag) {
			NBTTagCompound nbttagcompound = new NBTTagCompound();
			
			this.setTagInfo(s, nbttagcompound);
			return nbttagcompound;
		} else {
			return null;
		}
	}
	
	public NBTTagList getEnchantments() {
		return this.tag == null ? null : this.tag.getList("ench", 10);
	}
	
	public String getName() {
		String s = this.getItem().a(this);
		
		if (this.tag != null && this.tag.hasKeyOfType("display", 10)) {
			NBTTagCompound nbttagcompound = this.tag.getCompound("display");
			
			if (nbttagcompound.hasKeyOfType("Name", 8)) {
				s = nbttagcompound.getString("Name");
			}
		}
		
		return s;
	}
	
	public ItemStack setName(String s) {
		if (this.tag == null) {
			this.tag = new NBTTagCompound();
		}
		
		if (!this.tag.hasKeyOfType("display", 10)) {
			this.tag.set("display", new NBTTagCompound());
		}
		
		this.tag.getCompound("display").setString("Name", s);
		return this;
	}
	
	/**
	 * Clear any custom name set for this ItemStack
	 */
	// from -> r()
	public void clearCustomName() {
		if (this.tag != null) {
			if (this.tag.hasKeyOfType("display", 10)) {
				NBTTagCompound nbttagcompound = this.tag.getCompound("display");
				
				nbttagcompound.remove("Name");
				if (nbttagcompound.isEmpty()) {
					this.tag.remove("display");
					if (this.tag.isEmpty()) {
						this.setTag(null);
					}
				}
				
			}
		}
	}
	
	/**
	 * Returns true if the itemstack has a display name
	 */
	public boolean hasName() {
		return this.tag != null && (this.tag.hasKeyOfType("display", 10) && this.tag.getCompound("display").hasKeyOfType("Name", 8));
	}
	
	// from -> u()
	public EnumItemRarity getRarity() {
		return this.getItem().g(this);
	}
	
	/**
	 * True if it is a tool and has no enchantments to begin with
	 */
	// from -> v()
	public boolean isItemEnchantable() {
		return this.getItem().f_(this) && !this.hasEnchantments();
	}
	
	/**
	 * Adds an enchantment with a desired level on the ItemStack.
	 */
	public void addEnchantment(Enchantment enchantment, int level) {
		if (this.tag == null) {
			this.setTag(new NBTTagCompound());
		}
		
		if (!this.tag.hasKeyOfType("ench", 9)) {
			this.tag.set("ench", new NBTTagList());
		}
		
		NBTTagList nbttaglist = this.tag.getList("ench", 10);
		NBTTagCompound nbttagcompound = new NBTTagCompound();
		
		nbttagcompound.setShort("id", (short) enchantment.id);
		nbttagcompound.setInt("lvl", level);
		
		nbttaglist.add(nbttagcompound);
	}
	
	/**
	 * True if the item has enchantment data
	 */
	public boolean hasEnchantments() {
		return this.tag != null && this.tag.hasKeyOfType("ench", 9);
	}
	
	// from -> a()
	public void setTagInfo(String s, NBTBase nbtbase) {
		if (this.tag == null) {
			this.setTag(new NBTTagCompound());
		}
		
		this.tag.set(s, nbtbase);
	}
	
	/**
	 * Returns if this item stack can edit blocks.
	 */
	// from -> x()
	public boolean canEditBlocks() {
		return this.getItem().s();
	}
	
	/**
	 * Return whether this stack is on an item frame.
	 */
	// from -> y()
	public boolean isOnItemFrame() {
		return this.itemFrame != null;
	}
	
	/**
	 * Set the item frame this stack is on.
	 */
	// from -> a()
	public void setItemFrame(EntityItemFrame entityitemframe) {
		this.itemFrame = entityitemframe;
	}
	
	/**
	 * Return the item frame this stack is on. Returns null if not on an item frame.
	 */
	// from -> z()
	public EntityItemFrame getItemFrame() {
		return this.itemFrame;
	}
	
	/**
	 * Get this stack's repair cost, or 0 if no repair cost is defined.
	 */
	public int getRepairCost() {
		return this.hasTag() && this.tag.hasKeyOfType("RepairCost", 3) ? this.tag.getInt("RepairCost") : 0;
	}
	
	/**
	 * Set this stack's repair cost.
	 */
	public void setRepairCost(int i) {
		if (!this.hasTag()) {
			this.tag = new NBTTagCompound();
		}
		
		this.tag.setInt("RepairCost", i);
	}
	
	/**
	 * Gets all attribute modifiers of this item stack.
	 */
	// from -> B()
	public Multimap getAttributes() {
		Multimap object;
		
		if (this.hasTag() && this.tag.hasKeyOfType("AttributeModifiers", 9)) {
			object = HashMultimap.create();
			NBTTagList nbttaglist = this.tag.getList("AttributeModifiers", 10);
			
			for (int i = 0; i < nbttaglist.size(); ++i) {
				NBTTagCompound nbttagcompound = nbttaglist.getCompound(i);
				AttributeModifier attributemodifier = GenericAttributes.a(nbttagcompound);
				
				if (attributemodifier != null && attributemodifier.a().getLeastSignificantBits() != 0L && attributemodifier.a().getMostSignificantBits() != 0L) {
					object.put(nbttagcompound.getString("AttributeName"), attributemodifier);
				}
			}
		} else {
			object = this.getItem().i();
		}
		
		return object;
	}
	
	/**
	 * Get a ChatComponent for this Item's display name that shows this Item on hover
	 */
	// from -> C()
	public IChatBaseComponent getChatComponent() {
		ChatComponentText chatcomponenttext = new ChatComponentText(this.getName());
		
		if (this.hasName()) {
			chatcomponenttext.getChatModifier().setItalic(Boolean.TRUE);
		}
		
		IChatBaseComponent ichatbasecomponent = (new ChatComponentText("[")).addSibling(chatcomponenttext).a("]");
		
		if (this.item != null) {
			NBTTagCompound nbttagcompound = new NBTTagCompound();
			
			this.writeToNBT(nbttagcompound);
			ichatbasecomponent.getChatModifier().setChatHoverable(new ChatHoverable(ChatHoverable.EnumHoverAction.SHOW_ITEM, new ChatComponentText(nbttagcompound.toString())));
			ichatbasecomponent.getChatModifier().setColor(this.getRarity().e);
		}
		
		return ichatbasecomponent;
	}
	
	// from -> c()
	public boolean canDestroy(Block block) {
		if (block == this.canDestroyCacheBlock) {
			return this.canDestroyCacheResult;
		} else {
			this.canDestroyCacheBlock = block;
			if (this.hasTag() && this.tag.hasKeyOfType("CanDestroy", 9)) {
				NBTTagList nbttaglist = this.tag.getList("CanDestroy", 8);
				
				for (int i = 0; i < nbttaglist.size(); ++i) {
					Block block1 = Block.getByName(nbttaglist.getString(i));
					
					if (block1 == block) {
						this.canDestroyCacheResult = true;
						return true;
					}
				}
			}
			
			this.canDestroyCacheResult = false;
			return false;
		}
	}
	
	// from -> d()
	public boolean canPlaceOn(Block block) {
		if (block == this.canPlaceOnCacheBlock) {
			return this.canPlaceOnCacheResult;
		} else {
			this.canPlaceOnCacheBlock = block;
			if (this.hasTag() && this.tag.hasKeyOfType("CanPlaceOn", 9)) {
				NBTTagList nbttaglist = this.tag.getList("CanPlaceOn", 8);
				
				for (int i = 0; i < nbttaglist.size(); ++i) {
					Block block1 = Block.getByName(nbttaglist.getString(i));
					
					if (block1 == block) {
						this.canPlaceOnCacheResult = true;
						return true;
					}
				}
			}
			
			this.canPlaceOnCacheResult = false;
			return false;
		}
	}
	
	@FromWalk
	public boolean canHarvestBlock(Player player, org.bukkit.block.Block block) {
		if (customItem == null)
			return false;
		
		return customItem.canHarvest(player, cachedCraftItem, block);
	}
	
	@FromWalk
	public void onItemBreak(Player player) {
		if (customItem != null) {
			customItem.onItemBreak(player, cachedCraftItem);
		}
	}
	
	@FromWalk
	public boolean onDamage(int amount) {
		if (customItem == null)
			return true;
		
		return customItem.onDamage(cachedCraftItem, amount);
	}
	
	@FromWalk
	public boolean onDamageWith(Player player, int amount) {
		if (customItem == null)
			return true;
		
		return customItem.onDamageWith(player, cachedCraftItem, amount);
	}
	
	@FromWalk
	public boolean isDamageable() {
		if (customItem == null)
			return isItemStackDamageable();
		
		return customItem.isDamageable(cachedCraftItem);
	}
	
	@FromWalk
	public boolean canDestroyBlock(Player player, org.bukkit.block.Block block) {
		if (customItem == null)
			return true;
		
		return customItem.canDestroy(player, cachedCraftItem, block);
	}
	
	@FromWalk
	public boolean onAirInteract(Player player, Click click) {
		if (customItem == null)
			return true;
		
		return customItem.onInteract(player, cachedCraftItem, click);
	}
	
	@FromWalk
	public boolean onBlockInteract(Player player, org.bukkit.block.Block block, Click click) {
		if (customItem == null)
			return true;
		
		return customItem.onBlockInteract(player, cachedCraftItem, block, click);
	}
	
	@FromWalk
	public boolean onEntityInteract(Player player, org.bukkit.entity.Entity entity, Click click) {
		if (customItem == null)
			return true;
		
		return customItem.onEntityInteract(player, entity, cachedCraftItem, click);
	}
	
	@FromWalk
	public float getBreakSpeed(Player player, org.bukkit.block.Block block) {
		if (customItem == null)
			return 0.0f;
		
		return customItem.getBreakSpeed(player, cachedCraftItem, block);
	}
	
	@FromWalk
	public boolean canPlaceBlock(Player player, org.bukkit.block.Block block) {
		if (customItem == null)
			return true;
		
		return customItem.canPlace(player, cachedCraftItem, block);
	}
	
	@FromWalk
	public boolean onPlaceBlock(Player player, org.bukkit.block.Block block) {
		if (customItem == null)
			return true;
		
		return customItem.onPlace(player, cachedCraftItem, block);
	}
	
	@FromWalk
	public CraftItemStack toCraftItem() {
		return CraftItemStack.asCraftMirror(this);
	}
	
	@FromWalk
	public String getCustomItemId() {
		if (customItem != null)
			return customItem.getId();
		
		return hasCustomItem() ? tag.getString("CustomItemId") : null;
	}
	
	@FromWalk
	public IItem getCustomItem() {
		if (customItem != null)
			return customItem;
		
		String id = getCustomItemId();
		if (id == null)
			return null;
		
		return customItem = ItemRegistry.find(id);
	}
	
	@FromWalk
	public void setCustomItem(@NotNull IItem item) {
		if (tag == null)
			tag = new NBTTagCompound();
		
		tag.setString("CustomItemId", item.getId());
		customItem = item;
	}
	
	@FromWalk
	public boolean hasCustomItem() {
		if (customItem != null)
			return true;
		
		return tag != null && tag.hasKey("CustomItemId");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy