Problems removing items from player's inventory [Solved]

Discussion in 'Plugin Development' started by Ahniolator, Oct 3, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Ahniolator

    Edit: Fixed it. See code for why

    Code:java
    1. public static void removeInventoryItems(Inventory inv, ItemStack item) {
    2. removeInventoryItems(inv, item.getType(), item.getAmount());
    3. }
    4.  
    5. public static void removeInventoryItems(Inventory inv, Material type, int amount) {
    6. for (ItemStack is : inv.getContents()) {
    7. if (is != null && is.getType() == type) {
    8. int newamount = is.getAmount() - amount;
    9. if (is.getDurability() > (short) 0) {
    10. continue;
    11. }
    12. if (newamount > 0) {
    13. is.setAmount(newamount);
    14. isBroken = false;
    15. break;
    16. } else {
    17. inv.remove(is); //removes ALL item stacks that match the type and amount of this one this was the problem, as if it ended on a stack with one of the "item", it removed ALL stacks matching the type and amount of "item"
    18. amount = -newamount;
    19. if (amount <= 0) {
    20. isBroken = false;
    21. break;
    22. }
    23. }
    24. }
    25. }
    26. }


    This is the way the code should look:

    Code:java
    1. public static void removeInventoryItems(Inventory inv, ItemStack item) {
    2. removeInventoryItems(inv, item.getType(), item.getAmount());
    3. }
    4.  
    5. public static void removeInventoryItems(Inventory inv, Material type, int amount) {
    6. int testamt = amount, x = 0;
    7. for (ItemStack is : inv.getContents()) {
    8. if (is != null && is.getType() == type) {
    9. int newamount = is.getAmount() - testamt;
    10. if (is.getDurability() > (short) 0) {
    11. ++x;
    12. continue;
    13. }
    14. if (newamount > 0) {
    15. is.setAmount(newamount);
    16. isBroken = false;
    17. break;
    18. } else {
    19. testamt = testamt - is.getAmount();
    20. inv.clear(x);
    21. if (testamt <= 0) {
    22. isBroken = false;
    23. break;
    24. }
    25. }
    26. }
    27. ++x;
    28. }
    29. }
     
Thread Status:
Not open for further replies.

Share This Page