private boolean matchesWildcard(ItemStack expected, ItemStack actual) { return actual != null && expected.getType() == actual.getType() && ( isWildcard(expected.getDurability()) || expected.getDurability() == actual .getDurability()); }
public static Potion fromItemStack(ItemStack item) { Validate.notNull(item, "item cannot be null"); if (item.getType() != Material.POTION) throw new IllegalArgumentException("item is not a potion"); return fromDamage(item.getDurability()); }
/** * Removes multiple instances of an ingredient from the list. If there are * less instances then specified, all will be removed. If the data value * is -1, only ingredients with a -1 data value will be removed. * * @param count The number of copies to remove. * @param ingredient The ingredient to remove. * @param rawdata The data value. * @return The changed recipe. * @deprecated Magic value */ @Deprecated public ShapelessRecipe removeIngredient(int count, Material ingredient, int rawdata) { Iterator<ItemStack> iterator = ingredients.iterator(); while (count > 0 && iterator.hasNext()) { ItemStack stack = iterator.next(); if (stack.getType() == ingredient && stack.getDurability() == rawdata) { iterator.remove(); count--; } } return this; }
private boolean compareItems(ItemStack a, ItemStack b, boolean ignoreMeta) { if (ignoreMeta) { return a.getTypeId() == b.getTypeId() && a.getDurability() == b.getDurability(); } return a.isSimilar(b); }
@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; }
/** * Gets the MaterialData for this stack of items * * @return MaterialData for this item */ public MaterialData getData() { Material mat = getType(); if (data == null && mat != null && mat.getData() != null) { data = mat.getNewData((byte) this.getDurability()); } return data; }
@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; }
int mapId = original.getDurability();
/** * Get a list of all recipes for a given item. The stack size is ignored in comparisons. * If the durability is -1, it will match any data value. * * @param result The item whose recipes you want * @return The list of recipes */ public List<Recipe> getRecipesFor(ItemStack result) { // handling for old-style wildcards if (result.getDurability() == -1) { result = result.clone(); result.setDurability(Short.MAX_VALUE); } List<Recipe> recipes = new LinkedList<>(); for (Recipe recipe : this) { if (matchesWildcard(result, recipe.getResult())) { recipes.add(recipe); } } return recipes; }
/** * Write an item stack to the buffer. * * @param buf The buffer. * @param stack The stack to write, or null. */ public static void writeSlot(ByteBuf buf, ItemStack stack) { if (InventoryUtil.isEmpty(stack)) { buf.writeShort(-1); } else { buf.writeShort(stack.getTypeId()); buf.writeByte(stack.getAmount()); buf.writeShort(stack.getDurability()); if (stack.hasItemMeta()) { CompoundTag tag = GlowItemFactory.instance().writeNbt(stack.getItemMeta()); writeCompound(buf, tag); } else { writeCompound(buf, null); } } }
@Override public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) { super.placeBlock(player, state, face, holding, clickedLoc); // No Tree2 MaterialData MaterialData data = state.getData(); data.setData(setTree(face, (byte) holding.getDurability())); state.setData(data); }
/** * Creates a new item stack derived from the specified stack * * @param stack the stack to copy * @throws IllegalArgumentException if the specified stack is null or * returns an item meta not created by the item factory */ public ItemStack(final ItemStack stack) throws IllegalArgumentException { Validate.notNull(stack, "Cannot copy null stack"); this.type = stack.getTypeId(); this.amount = stack.getAmount(); this.durability = stack.getDurability(); this.data = stack.getData(); if (stack.hasItemMeta()) { setItemMeta0(stack.getItemMeta(), getType0()); } }
@Override public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) { super.placeBlock(player, state, face, holding, clickedLoc); if (holding.getDurability() > 1) { switch (face) { case NORTH: case SOUTH: state.setRawData((byte) 4); break; case WEST: case EAST: state.setRawData((byte) 3); break; case UP: case DOWN: state.setRawData((byte) 2); break; default: // do nothing // TODO: should this raise a warning? } } }
/** * 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); }
/** * Inflicts damage to an item. Unbreaking enchantment is applied if present. * * @param player the player holding the item, or null if held by a dispenser * @param holding the item * * @return the updated item stack */ public static ItemStack damageItem(GlowPlayer player, ItemStack holding) { if (player != null && player.getGameMode() == GameMode.CREATIVE) { return holding; } // Apply unbreaking enchantment. // TODO: Armor has a different formula for chance to avoid damage int durability = holding.getEnchantmentLevel(Enchantment.DURABILITY); if (durability > 0 && ThreadLocalRandom.current().nextDouble() < 1 / (durability + 1)) { return holding; } holding.setDurability((short) (holding.getDurability() + 1)); if (holding.getDurability() == holding.getType().getMaxDurability() + 1) { if (player != null) { EventFactory.getInstance() .callEvent(new PlayerItemBreakEvent(player, holding)); } return createEmptyStack(); } return holding; }
@Override public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding, GlowBlockState oldState) { GlowBanner banner = (GlowBanner) block.getState(); banner.setBaseColor(DyeColor.getByDyeData((byte) holding.getDurability())); BannerMeta meta = (BannerMeta) holding.getItemMeta(); meta.setPatterns(meta.getPatterns()); banner.update(); }
@Override public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) { super.placeBlock(player, state, face, holding, clickedLoc); MaterialData data = state.getData(); if (data instanceof Tree) { ((Tree) data).setDirection(face); ((Tree) data).setSpecies(TreeSpecies.getByData((byte) holding.getDurability())); } else { warnMaterialData(Tree.class, data); } state.setData(data); }
/** * Resets the enchantments. */ public void invalidate() { ItemStack item = inventory.getItem(); ItemStack resource = inventory.getSecondary(); if (item == null || !canEnchant(item) || player.getGameMode() != GameMode.CREATIVE && ( resource == null || resource.getType() != Material.INK_SACK || resource.getDurability() != 4)) { clearEnch(); } else { calculateNewEnchantsAndLevels(); } }
/** * Write an item stack to an NBT tag. * * <p>Null stacks produce an empty tag, and if slot is negative it is omitted from the result. * * @param stack The stack to write, or null. * @param slot The slot, or negative to omit. * @return The resulting tag. */ public static CompoundTag writeItem(ItemStack stack, int slot) { CompoundTag tag = new CompoundTag(); if (stack == null || stack.getType() == Material.AIR) { return tag; } tag.putString("id", ItemIds.getName(stack.getType())); tag.putShort("Damage", stack.getDurability()); tag.putByte("Count", stack.getAmount()); tag.putByte("Slot", slot); CompoundTag meta = GlowItemFactory.instance().writeNbt(stack.getItemMeta()); if (meta != null) { tag.putCompound("tag", meta); } return tag; }
@Override public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding, GlowBlockState oldState) { GlowSkull skull = (GlowSkull) block.getState(); skull.setSkullType(getType(holding.getDurability())); if (skull.getSkullType() == SkullType.PLAYER) { SkullMeta meta = (SkullMeta) holding.getItemMeta(); if (meta != null) { skull.setOwner(meta.getOwner()); } } MaterialData data = skull.getData(); if (!(data instanceof Skull)) { warnMaterialData(Skull.class, data); return; } Skull skullData = (Skull) data; if (canRotate(skullData)) { // Can be rotated skull.setRotation(player.getFacing().getOppositeFace()); } skull.update(); // Wither for (int i = 0; i < 3; i++) { if (WITHER_PATTERN.matches(block.getLocation().clone(), true, i, 0)) { block.getWorld() .spawnEntity(block.getLocation().clone().subtract(0, 2, 0), EntityType.WITHER); break; } } }