Hey, you there! I'm trying to make an event and that event goes okay. But I also want to remove an amount of dye from the inventory. This is my code to take items. Code (Text): public static void removeInventoryItems(PlayerInventory inv, Material type, int amount) { for (ItemStack is : inv.getContents()) { if ((is != null) && (is.getType() == type)) { int newamount = is.getAmount() - amount; if (newamount > 0) { is.setAmount(newamount); } else { inv.remove(is); amount = -newamount; if (amount == 0) { break; } } } } } Does someone know how to make it to take an amount of dye. Actually I should add short or something.
Check if the amount is 1, if it is, set item to null(remove it?) if it's not 1, setAmount(getAmount() - 1);
Thanks, first I thought it wouldn't work but I just did event.getPlayer().getItemInHand().setAmount(- 1); So the item will get the amount of -1. Fix: event.getPlayer().getItemInHand().setAmount(event.getPlayer().getItemInHand().getAmount() - 1); Thanks Teddy!
You want to remove a specific amount of an specific item of an player inventory? So your first step must be to count up all those items. Otherwise you would remove the amount for each occurence of the item at any slot (or is this what you're trying to accomplish?). My suggestion (untested): Code (Java): public void removeAmount(Inventory inv, ItemStack key, int sub) { int amount = 0; // first count up the total present amount of "similar" items and delete all those for (int slot : inv.all(key.getType()).keySet()) { if (key.isSimilar(inv.getItem(slot))) { amount = amount + inv.getItem(slot).getAmount(); inv.setItem(slot, new ItemStack(Material.AIR, 1)); } } // then subtract the cost amount = amount - sub; // if there's something left, add the rest again if (amount > 0) { // first add and give back all completely filled stacks key.setAmount(key.getMaxStackSize()); for (int i = amount; i > key.getMaxStackSize(); i = i - key.getMaxStackSize()) { inv.addItem(key.clone()); } // then the rest not-completely filled stack key.setAmount(amount % key.getMaxStackSize()); inv.addItem(key.clone()); } } EDIT: Fixed bug.
I'm not quite sure, what you mean... maybe java.lang.Short#intValue and java.lang.Integer#shortValue? Normally your compiler automatically cares about how to cast from int to short (and back) if necessary. So where's the problem?
I just mean to use the short for like (Material.INK_SACK, (short) 3); This will make the Material a Dye with the ID 3 that will result in blue dye I guess. Every dye has it own ID.
Using magic values like this is deprecated and should be avoided. Use org.bukkit.material.Dye. The method i posted abode already only takes items of the same color. That's why i'm using #isSimilar. If there is black and white dye in your inv and you call this method to remove x amount of black, it will only remove black, because white is not #isSimilar. Or do you want it to remove black and white, when it's called to remove black? Black / white are just examples of course... could be any color.
Code (Java): // ... // Your class // ... private void removeAmount(Inventory inv, ItemStack key, int sub) { /* See post above */ } // ... private void removeRedWool(Player player, int amount) { this.removeAmount(player.getInventory(), new Dye(DyeColor.RED).toItemStack(), amount); // may not include armor & hotbar slots } Does this answer your question?
This uses short value data, but does not use deprecated methods: Code (Java): public boolean removeItemType(Player player, Material type, short data, int amount) { List<Integer> slots = new ArrayList<Integer>(); int sum = 0; for (int i = 0; i < player.getInventory().getSize(); i++) { ItemStack item = player.getInventory().getItem(i); if (data == 0) { data = item.getDurability(); } if (item != null && item.getType().equals(type) && item.getDurability() == data) { slots.add(i); sum = (sum + item.getAmount()); if (sum >= amount) { break; } } if (i == player.getInventory().getSize() && sum < amount) { return false; } } if (sum > 0) { for (int i : slots) { ItemStack item = player.getInventory().getItem(i); if (item.getAmount() > amount) { item.setAmount((item.getAmount() - amount)); player.updateInventory(); return true; } else if (item.getAmount() == amount) { player.getInventory().setItem(i, null); player.updateInventory(); return true; } else { amount = (amount - item.getAmount()); player.getInventory().setItem(i, null); player.updateInventory(); } } } return false; }
Using durability to set meta values is deprecated, too. It's deprecated because of magic values. To learn more about magic values research the internet... there are plenty good explanations of it online. It's not declared as deprecated by the bukkit API, because within the Bukkit API #setDurability / #getDurability should be used to set / get the durability of tools, not the meta value of items. Unfortunately it's the same in this case.
You do realize the Dye class, and so forth are just wrappers? Also, again, the methods above use no deprecated methods marked by the Spigot API. It works and is not marked deprecated because on these items, the Durability IS it's color values with valid range... This also allows you to remove CUSTOM items from users with a specific short value for it's texture.
Yes. Because: You did not get the point of magic values. https://en.wikipedia.org/wiki/Magic_number_(programming)