Remove amount of DYE from inventory.

Discussion in 'Spigot Plugin Development' started by Zarlen, May 1, 2017.

  1. 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.
     
  2. Check if the amount is 1, if it is, set item to null(remove it?) if it's not 1, setAmount(getAmount() - 1);
     
  3. 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!
     
  4. @TheBlackTeddy is there also a way to add a short thing in my own method?
     
  5. Just have a short show in your method?
     
  6. Yea, and also apply it because I don't know how (just in the method)
     
  7. 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.
     
  8. I guess using setDurability you can change the datavalue
     
  9. It's actually better, the old one toke from all slots. But how do I use short in this method
     
  10. 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?
     
  11. 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.
     
  12. 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.
     
  13. Can you please give me an example to take red dye from the inventory?
     
  14. 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?
     
  15. WAS

    WAS

    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;

       }
     
  16. 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.
     
  17. WAS

    WAS

    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.
     
  18. Yes.

    Because:

    You did not get the point of magic values.
    https://en.wikipedia.org/wiki/Magic_number_(programming)
     
  19. WAS

    WAS