@Utility public Map<String, Object> serialize() { Map<String, Object> result = new LinkedHashMap<String, Object>(); result.put("type", getType().name()); if (getDurability() != 0) { result.put("damage", getDurability()); } if (getAmount() != 1) { result.put("amount", getAmount()); } ItemMeta meta = getItemMeta(); if (!Bukkit.getItemFactory().equals(meta, null)) { result.put("meta", meta); } return result; }
/** * Get the maximum stacksize for the material hold in this ItemStack. * (Returns -1 if it has no idea) * * @return The maximum you can stack this material to. */ @Utility public int getMaxStackSize() { Material material = getType(); if (material != null) { return material.getMaxStackSize(); } return -1; }
@Override @Utility public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof ItemStack)) { return false; } ItemStack stack = (ItemStack) obj; return getAmount() == stack.getAmount() && isSimilar(stack); }
/** * This method is the same as equals, but does not consider stack size * (amount). * * @param stack the item stack to compare to * @return true if the two stacks are equal, ignoring the amount */ @Utility public boolean isSimilar(ItemStack stack) { if (stack == null) { return false; } if (stack == this) { return true; } return getTypeId() == stack.getTypeId() && getDurability() == stack.getDurability() && hasItemMeta() == stack.hasItemMeta() && (hasItemMeta() ? Bukkit.getItemFactory().equals(getItemMeta(), stack.getItemMeta()) : true); }
@Override @Utility public final int hashCode() { int hash = 1; hash = hash * 31 + getTypeId(); hash = hash * 31 + getAmount(); hash = hash * 31 + (getDurability() & 0xffff); hash = hash * 31 + (hasItemMeta() ? (meta == null ? getItemMeta().hashCode() : meta.hashCode()) : 0); return hash; }
/** * Adds the specified {@link Enchantment} to this item stack. * <p> * If this item stack already contained the given enchantment (at any * level), it will be replaced. * * @param ench Enchantment to add * @param level Level of the enchantment * @throws IllegalArgumentException if enchantment null, or enchantment is * not applicable */ @Utility public void addEnchantment(Enchantment ench, int level) { Validate.notNull(ench, "Enchantment cannot be null"); if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) { throw new IllegalArgumentException("Enchantment level is either too low or too high (given " + level + ", bounds are " + ench.getStartLevel() + " to " + ench.getMaxLevel() + ")"); } else if (!ench.canEnchantItem(this)) { throw new IllegalArgumentException("Specified enchantment cannot be applied to this itemstack"); } addUnsafeEnchantment(ench, level); }
/** * Get the maximum stacksize for the material hold in this ItemStack * Returns -1 if it has no idea. * * @return The maximum you can stack this material to. */ @Utility public int getMaxStackSize() { Material material = getType(); if (material != null) { return material.getMaxStackSize(); } return -1; }
/** * This method is the same as equals, but does not consider stack size (amount). * * @param stack the item stack to compare to * @return true if the two stacks are equal, ignoring the amount */ @Utility public boolean isSimilar(ItemStack stack) { if (stack == null) { return false; } if (stack == this) { return true; } return getTypeId() == stack.getTypeId() && getDurability() == stack.getDurability() && hasItemMeta() == stack.hasItemMeta() && (hasItemMeta() ? Bukkit.getItemFactory().equals(getItemMeta(), stack.getItemMeta()) : true); }
/** * Adds the specified {@link Enchantment} to this item stack. * <p> * If this item stack already contained the given enchantment (at any level), it will be replaced. * * @param ench Enchantment to add * @param level Level of the enchantment * @throws IllegalArgumentException if enchantment null, or enchantment is not applicable */ @Utility public void addEnchantment(Enchantment ench, int level) { Validate.notNull(ench, "Enchantment cannot be null"); if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) { throw new IllegalArgumentException("Enchantment level is either too low or too high (given " + level + ", bounds are " + ench.getStartLevel() + " to " + ench.getMaxLevel() + ")"); } else if (!ench.canEnchantItem(this)) { throw new IllegalArgumentException("Specified enchantment cannot be applied to this itemstack"); } addUnsafeEnchantment(ench, level); }