public static int modifyContainer(BlockState b, ItemStack item) { if (b instanceof InventoryHolder) { final Inventory inv = ((InventoryHolder) b).getInventory(); if (item.getAmount() < 0) { item.setAmount(-item.getAmount()); final ItemStack tmp = inv.removeItem(item).get(0); return tmp != null ? tmp.getAmount() : 0; } else if (item.getAmount() > 0) { final ItemStack tmp = inv.addItem(item).get(0); return tmp != null ? tmp.getAmount() : 0; } } return 0; }
@EventHandler public void gun(PlayerInteractEvent event) { if (!event.getPlayer().getWorld().getName().equals(getName())) return; if (event.getPlayer().getGameMode() == GameMode.SURVIVAL) { Player player = event.getPlayer(); Location loc = player.getLocation(); Action action = event.getAction(); ItemStack i = player.getItemInHand(); Inventory inv = player.getInventory(); Material tool = i.getType(); final World world = loc.getWorld(); if (tool == Material.BLAZE_ROD) { if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { if (inv.contains(Material.FLINT)) { player.launchProjectile(Arrow.class); world.playSound(loc, Sound.COW_WALK, 10, 10); ItemStack AMMO = new ItemStack(Material.FLINT, 1); inv.removeItem(AMMO); ItemMeta ammo = AMMO.getItemMeta(); ammo.setDisplayName(ChatColor.BLUE + "Ammunition"); AMMO.setItemMeta(ammo); inv.removeItem(AMMO); // Make it remove normal flints, too. player.updateInventory(); } else { world.playSound(loc, Sound.CLICK, 10, 10); } } } } }
@SuppressWarnings("deprecation") private boolean removeItems(Player player, int amount) { Inventory inv = player.getInventory(); ItemStack item = new ItemStack(DragonTravel.getInstance().getConfigHandler().getPaymentItemType(), amount); ItemMeta im = item.getItemMeta(); if (!DragonTravel.getInstance() .getConfigHandler() .getPaymentItemName() .equalsIgnoreCase("<default>")) im.setDisplayName( ChatColor.translateAlternateColorCodes( '&', DragonTravel.getInstance().getConfigHandler().getPaymentItemName())); item.setItemMeta(im); if (inv.containsAtLeast(item, amount)) { Map<Integer, ItemStack> leftover = inv.removeItem(item); if (!leftover.isEmpty()) { Bukkit.getLogger() .warning( "Removing items from " + player.getName() + "'s inventory gave a leftover; allowing payment anyways."); } player.updateInventory(); String message = DragonTravel.getInstance() .getMessagesHandler() .getMessage("Messages.Payment.Resources.Successful.WithdrawMessage"); message = message.replace("{amount}", "%d"); message = String.format(message, amount); player.sendMessage(message); return true; } else { player.sendMessage( DragonTravel.getInstance() .getMessagesHandler() .getMessage("Messages.Payment.Resources.Error.NotEnoughResources")); return false; } }