private ItemStack amountOrEmpty(ItemStack original, int amount) { original.setAmount(amount); return amount <= 0 ? InventoryUtil.createEmptyStack() : original; }
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); } }
@Override public Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) { // TODO: Implement Silk Touch ThreadLocalRandom random = ThreadLocalRandom.current(); int count = minCount; if (maxCount > minCount) { count += random.nextInt(maxCount - minCount); } ItemStack stack = new ItemStack(dropType, count, (short) data); if (tool == null) { return Collections.unmodifiableList(Arrays.asList(stack)); } Collection<ItemStack> drops = super.getDrops(block, tool); if (drops.size() == 0) { return drops; } if (tool.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)) { stack.setAmount(count * getMultiplicator(random, tool.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS))); } return Collections.unmodifiableList(Arrays.asList(stack)); }
@Override public void setItem(ItemStack is) { is = InventoryUtil.itemOrEmpty(is).clone(); is.setAmount(1); metadata.set(MetadataIndex.ITEM_FRAME_ITEM, is); }
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); } }
/** * Remove a specific amount of layers from the crafting matrix and recipe result. * * @param items The items to remove the ingredients from. * @param inv The inventory to remove the items from. * @param amount The amount of items you want to remove. */ public void removeItems(final ItemStack[] items, final GlowCraftingInventory inv, final int amount) { if (amount < 0) { throw new IllegalArgumentException("Can not remove negative amount of layers."); } for (int i = 0; i < items.length; i++) { if (!InventoryUtil.isEmpty(items[i])) { int itemAmount = items[i].getAmount(); if (itemAmount > amount) { items[i].setAmount(itemAmount - amount); inv.updateResultSlot(); } else { inv.setItem(i + 1, InventoryUtil.createEmptyStack()); } } } }
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; }
@Override protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) { GlowDispenser dispenser = (GlowDispenser) block.getState(); GlowBlock target = block.getRelative(BlockDispenser.getFacing(block)); BlockLiquid liquid = collectableLiquidAtBlock(target); if (liquid == null) { return super.dispenseStack(block, stack); } Material bucket = liquid.getBucketType(); target.setType(Material.AIR); stack.setAmount(stack.getAmount() - 1); if (stack.getAmount() == 0) { stack.setAmount(1); stack.setType(bucket); } else { ItemStack toPlace = new ItemStack(bucket); ItemStack remaining = dispenser.placeInDispenser(toPlace); if (remaining != null) { INSTANCE.dispense(block, remaining); } } return stack; }
/** * Consumes 1 copy of the given item, which must be held by the given player, unless that player * is in Creative mode. * * @param player the player holding the item * @param item the item to consume */ public static void consumeHeldItem(HumanEntity player, ItemStack item) { if (player.getGameMode() == GameMode.CREATIVE) { return; } if (item.getAmount() > 1) { item.setAmount(item.getAmount() - 1); } else { player.getInventory().clear(player.getInventory().getHeldItemSlot()); } } }
@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 rightClickBook(GlowPlayer player, ItemStack holding) { if (holding.getItemMeta() instanceof KnowledgeBookMeta) { KnowledgeBookMeta recipes = (KnowledgeBookMeta) holding.getItemMeta(); if (recipes.hasRecipes()) { for (NamespacedKey recipe : recipes.getRecipes()) { player.learnRecipe(((GlowServer) ServerProvider.getServer()) .getCraftingManager().getRecipeByKey(recipe), true); } } if (player.getGameMode() != GameMode.CREATIVE) { holding.setAmount(0); } } } }
/** * Consumes an item or the full stack in the given slot. * @param slot The slot to consume. * @param wholeStack True if we should remove the complete stack. * @return The number of item really consumed. */ public int consumeItem(int slot, boolean wholeStack) { ItemStack item = InventoryUtil.itemOrEmpty(getItem(slot)); if (InventoryUtil.isEmpty(item)) { return 0; } if (wholeStack || item.getAmount() == 1) { setItem(slot, InventoryUtil.createEmptyStack()); } else { item.setAmount(item.getAmount() - 1); setItem(slot, item); } return wholeStack ? item.getAmount() : 1; }
private void fillBottle(GlowPlayer player, GlowBlock block) { // fired when a player fills an empty bottle from the cauldron if (block.getData() > LEVEL_EMPTY) { if (!setCauldronLevel(block, block.getData() - 1, player, CauldronLevelChangeEvent.ChangeReason.BOTTLE_FILL)) { return; } if (player.getGameMode() != GameMode.CREATIVE) { Map<Integer, ItemStack> drops = player.getInventory() .addItem(new ItemStack(Material.POTION)); if (!drops.isEmpty()) { player.getWorld() .dropItemNaturally(player.getLocation(), new ItemStack(Material.POTION)); } player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1); } } }
@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); } } } }
@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); } } } }
@Override protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) { ItemFilledBucket bucket = (ItemFilledBucket) ItemTable.instance().getItem(stack.getType()); BlockLiquid liquid = (BlockLiquid) bucket.getLiquid(); BlockFace facing = BlockDispenser.getFacing(block); GlowBlock target = block.getRelative(facing); if (canPlace(target, facing, stack)) { target.setType(liquid.getMaterial()); stack.setType(Material.BUCKET); stack.setAmount(1); return stack; } else { return INSTANCE.dispense(block, stack); } }
@Override public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) { ItemStack item = player.getItemInHand(); if (item != null && item.getType() == Material.EYE_OF_ENDER) { if ((block.getData() & 0x4) != 0) { return true; } if (player.getGameMode() != GameMode.CREATIVE) { item.setAmount(item.getAmount() - 1); } block.setData((byte) (block.getData() | 0x4)); if (block.getWorld().getEnvironment() != Environment.THE_END) { searchForCompletedPortal(player, block); } return true; } return false; }
@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 boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) { Jukebox jukebox = (Jukebox) block.getState(); if (jukebox.isPlaying()) { jukebox.eject(); jukebox.update(); return true; } ItemStack handItem = player.getItemInHand(); if (handItem != null && handItem.getType().isRecord()) { jukebox.setPlaying(handItem.getType()); jukebox.update(); if (player.getGameMode() != GameMode.CREATIVE) { handItem.setAmount(handItem.getAmount() - 1); player.setItemInHand(handItem); } return true; } return false; }