/** * Get the amount of layers in the crafting matrix. This assumes all Minecraft recipes have an * item stack of 1 for all items in the recipe. * * @param items The items in the crafting matrix. * @return The number of stacks for a recipe. */ public static int getLayers(ItemStack... items) { int layers = 0; for (ItemStack item : items) { if (!InventoryUtil.isEmpty(item) && (item.getAmount() < layers || layers == 0)) { layers = item.getAmount(); } } return layers; }
@Override public boolean contains(int materialId, int amount) { HashMap<Integer, ? extends ItemStack> found = all(materialId); int total = 0; for (ItemStack stack : found.values()) { total += stack.getAmount(); } return total >= amount; }
/** * Checks whether the given ItemStack is empty. * * @param stack the ItemStack to check * @return whether the given ItemStack is empty */ public static boolean isEmpty(ItemStack stack) { return stack == null || stack.getType() == Material.AIR || stack.getAmount() == 0; }
@Override @Utility public String toString() { StringBuilder toString = new StringBuilder("ItemStack{").append(getType().name()).append(" x ").append(getAmount()); if (hasItemMeta()) { toString.append(", ").append(getItemMeta()); } return toString.append('}').toString(); }
@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; }
@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; }
protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) { BlockFace facing = BlockDispenser.getFacing(block); Vector dispensePosition = BlockDispenser.getDispensePosition(block); ItemStack items = new ItemStack(stack.getType(), 1); stack.setAmount(stack.getAmount() - 1); doDispense(block, items, 6, facing, dispensePosition); return stack.getAmount() > 0 ? stack : null; }
/** * Create a WorldEdit BaseItemStack from a Bukkit ItemStack * * @param itemStack The Bukkit ItemStack * @return The WorldEdit BaseItemStack */ public static BaseItemStack adapt(ItemStack itemStack) { checkNotNull(itemStack); return new BaseItemStack(ItemTypes.get(itemStack.getType().getKey().toString()), itemStack.getAmount()); }
private ItemStack combine(ItemStack slotItem, ItemStack cursor, int amount) { if (InventoryUtil.isEmpty(slotItem)) { ItemStack stack = cursor.clone(); stack.setAmount(amount); return stack; } else if (slotItem.isSimilar(cursor)) { slotItem.setAmount(slotItem.getAmount() + amount); return slotItem; } else { throw new IllegalArgumentException( "Trying to combine dissimilar " + slotItem + " and " + cursor); } }
private static boolean canEnchant(ItemStack item) { Material type = item.getType(); switch (type) { case ENCHANTED_BOOK: return false; case BOOK: return item.getAmount() == 1; case FISHING_ROD: case BOW: return item.getEnchantments().isEmpty(); default: return (isEnchantableTool(type) || isCloth(type) || ToolType.SWORD.matches(type)) && item.getEnchantments().isEmpty(); } }
@Override protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) { GlowWorld world = block.getWorld(); GlowBlock target = block.getRelative(BlockDispenser.getFacing(block)); GlowTntPrimed tnt = (GlowTntPrimed) world .spawnEntity(target.getLocation().add(0.5, 0, 0.5), EntityType.PRIMED_TNT); world.playSound(tnt.getLocation(), Sound.ENTITY_TNT_PRIMED, 1, 1); stack.setAmount(stack.getAmount() - 1); return stack.getAmount() > 0 ? stack : null; } }
private void spawnFirework(GlowPlayer player, ItemStack item, Location location, UUID spawner, LivingEntity boostedEntity) { if (item.getType() != Material.FIREWORK || !(item.getItemMeta() instanceof FireworkMeta)) { return; } new GlowFirework(location, spawner, boostedEntity, item); if (player.getGameMode() != GameMode.CREATIVE) { item.setAmount(item.getAmount() - 1); } }
private void throwEnderPearl(GlowPlayer player, ItemStack holding) { if (player.getEnderPearlCooldown() == 0) { if (!player.getGameMode().equals(GameMode.CREATIVE)) { holding.setAmount(holding.getAmount() - 1); } throwEnderPearl(player); } }
/** * 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 protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) { GlowBlock target = block.getRelative(BlockDispenser.getFacing(block)); successful = true; if (target.getType() == Material.AIR) { target.setType(Material.FIRE); // TODO: Find the slot, so we can damage the flint and steel and write it back to the // inventory // InventoryUtil.damageItem(null, stack); } else if (target.getType() == Material.TNT) { BlockTnt.igniteBlock(target, false); } else { successful = false; } return stack.getAmount() > 0 ? stack : null; }
private void giveItem(CommandSender sender, Player player, ItemStack stack, ResourceBundle resourceBundle) { player.getInventory().addItem(stack); new LocalizedStringImpl("give.done", resourceBundle) .send(sender, ItemIds.getName(stack.getType()), stack.getAmount(), player.getName()); }
@Override protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) { GlowWorld world = block.getWorld(); Vector position = BlockDispenser.getDispensePosition(block); BlockFace face = BlockDispenser.getFacing(block); Projectile entity = projectileCreator.apply( new Location(world, position.getX(), position.getY(), position.getZ()), stack); entity.setShooter(new GlowDispenser(block)); entity.setVelocity( new Vector(face.getModX(), face.getModY() + 0.1f, face.getModZ()).multiply(6)); stack.setAmount(stack.getAmount() - 1); if (stack.getAmount() < 1) { return null; } return stack; }
@Override public GlowItem drop(ItemStack stack) { GlowItem dropping = super.drop(stack); if (dropping != null) { PlayerDropItemEvent event = new PlayerDropItemEvent(this, dropping); EventFactory.getInstance().callEvent(event); if (event.isCancelled()) { dropping.remove(); dropping = null; } else { incrementStatistic(Statistic.DROP, stack.getAmount()); } } return dropping; }
@Override public void rightClickBlock(GlowPlayer player, GlowBlock target, BlockFace face, ItemStack holding, Vector clickedLoc, EquipmentSlot hand) { if (target.getType() == soilType && target.getRelative(BlockFace.UP).getType() == Material.AIR && face == BlockFace.UP) { GlowBlockState state = target.getRelative(BlockFace.UP).getState(); state.setType(cropsType); state.setRawData((byte) 0); state.update(true); // deduct from stack if not in creative mode if (player.getGameMode() != GameMode.CREATIVE) { holding.setAmount(holding.getAmount() - 1); } } } }