Item loop won't remove last item

Discussion in 'Plugin Development' started by Colby l, Feb 22, 2014.

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

    Colby l

    With the loop below, the item in a player's inventory will drop down to a single, "ghost" item, where it will appear and disappear from the inventory randomly, but can easily be duplicated.

    For instance, if the item stack size is 2, it will go down to 2 items, then one item, then "ghost."

    I've tried the loop without p.updateinventoty(), as well as p.getInventory().setContents(p.getInventory().getContents());

    Loop:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void withdrawItem(int tier, Player p) {
    3. // Config variables
    4. int itemChargeAmount = config.getItemChargeAmount(tier);
    5. // gets item name enum
    6. //String itemChargeEnum = config.getItemChargeEnum(tier);
    7. // gets inventory server
    8. PlayerInventory inventory = p.getInventory();
    9. // gets itemstack
    10. // ItemStack itemStack = new
    11. // ItemStack(Material.getMaterial(itemChargeEnum), itemChargeAmount);
    12.  
    13. // checks if item charge is enabled
    14. if (itemChargeAmount == 0) {
    15. // returns true if no items are needed to fly
    16.  
    17. } else if (config.getItemMetaTag(tier).contains("-1")) {
    18. // checks if player has item and amount
    19. for (ItemStack is : inventory) {
    20.  
    21. if (is == null || is.equals(Material.AIR)) {
    22. continue;
    23. }
    24.  
    25. if (is.getItemMeta().getLore() != null) {
    26.  
    27. continue;
    28. }
    29.  
    30. if (itemChargeAmount > is.getAmount()) {
    31.  
    32. Flight.disableFlight(p, tier, 4);
    33. continue;
    34. }
    35. if (is.getAmount() == itemChargeAmount) {
    36. is.setAmount(0);
    37. // updates player's inventory
    38. p.getInventory().setContents(p.getInventory().getContents());
    39. continue;
    40. }
    41. // remove's players item
    42. is.setAmount(is.getAmount() - itemChargeAmount);
    43. // updates player's inventory
    44. p.getInventory().setContents(p.getInventory().getContents());
    45. break;
    46. }
    47.  
    48. } else {
    49. // iterates through player's inventory
    50. // int itemAmount = 0;
    51. for (ItemStack is : inventory) {
    52.  
    53. if (is == null || is.equals(Material.AIR)) {
    54. continue;
    55. }
    56. if (!is.hasItemMeta()) {
    57. continue;
    58. }
    59. ItemMeta itemmeta = is.getItemMeta();
    60. List<String> lore = itemmeta.getLore();
    61. if (!lore.contains(config.getItemMetaTag(tier))) {
    62. continue;
    63. }
    64. if (is.getAmount() == itemChargeAmount) {
    65.  
    66. is.setAmount(0);
    67. // updates player's inventory
    68. // p.getInventory().setContents(p.getInventory().getContents());
    69. p.updateInventory();
    70. continue;
    71. }
    72. if (itemChargeAmount > is.getAmount()) {
    73. continue;
    74. }
    75. // remove's players item
    76. is.setAmount(is.getAmount() - itemChargeAmount);
    77. // updates player's inventory
    78. // p.getInventory().setContents(p.getInventory().getContents());
    79. p.updateInventory();
    80. break;
    81. }
    82. }
    83. }


    thanks for the help!
     
  2. Offline

    L33m4n123

    if you want to set the item to "0" ammount, you need to set it to air instead
     
Thread Status:
Not open for further replies.

Share This Page