Removing items with certain meta from player inventory,

Discussion in 'Plugin Development' started by Monkeyboystein, Jun 19, 2013.

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

    Monkeyboystein

    Hey guys Again, im working on this money plugin and i have got the counting part of the deposit down pat. But i cant figure out why it wont remove the money, I dont get any errors in the console.

    Code:java
    1. public int countAndRemoveMoney(Inventory inventory) {
    2. int amount = 0;
    3. ItemStack[] contents = inventory.getContents();
    4. ItemStack money1 = new ItemStack(339, 1);
    5. ItemStack money2 = new ItemStack(339, 1);
    6. ItemStack money3 = new ItemStack(339, 1);
    7. ItemStack money4 = new ItemStack(339, 1);
    8. ItemStack money5 = new ItemStack(339, 1);
    9. ItemStack money6 = new ItemStack(339, 1);
    10. ItemStack money7 = new ItemStack(339, 1);
    11. ItemStack money8 = new ItemStack(339, 1);
    12. ItemStack money9 = new ItemStack(339, 1);
    13. ItemStack money10 = new ItemStack(339, 1);
    14. money1.setItemMeta(money_ones);
    15. money2.setItemMeta(money_twos);
    16. money3.setItemMeta(money_threes);
    17. money4.setItemMeta(money_fours);
    18. money5.setItemMeta(money_fives);
    19. money6.setItemMeta(money_sixs);
    20. money7.setItemMeta(money_sevens);
    21. money8.setItemMeta(money_eights);
    22. money9.setItemMeta(money_nines);
    23. money10.setItemMeta(money_tens);
    24.  
    25.  
    26. // Do the same for all of your types of money, changing it to be * (dollar value)
    27. // }
    28.  
    29. if(inventory.contains(money1)) {
    30. amount += money1.getAmount() *1;
    31.  
    32. money1.setAmount(0);
    33. }
    34. if(inventory.contains(money2)) {
    35. amount += money2.getAmount() *2;
    36. money2.setAmount(0);
    37. }
    38. if(inventory.contains(money3)) {
    39. amount += money3.getAmount() *3;
    40. money3.setAmount(0);
    41. }
    42. if(inventory.contains(money4)) {
    43. amount += money4.getAmount() *4;
    44. money4.setAmount(0);
    45. }
    46. if(inventory.contains(money5)) {
    47. amount += money5.getAmount() *5;
    48. money5.setAmount(0);
    49. }
    50. if(inventory.contains(Material.PAPER)) {
    51. amount += money6.getAmount() *6;
    52. money6.setAmount(0);
    53. }
    54. if(inventory.contains(money7)) {
    55. amount += money7.getAmount() *7;
    56. money7.setAmount(0);
    57. }
    58. if(inventory.contains(money8)) {
    59. amount += money8.getAmount() *8;
    60. money8.setAmount(0);
    61. }
    62. if(inventory.contains(money9)) {
    63. amount += money9.getAmount() *9;
    64. money9.setAmount(0);
    65. }
    66. if(inventory.contains(money10)) {
    67. amount += money10.getAmount() *10;
    68.  
    69. money10.setAmount(0);
    70. }
    71.  
    72.  
    73. return amount;
    74. }



    If you need more of the code please comment below and tag me
     
  2. Offline

    BENCLABSTER

    Give the money a display name
    Code:java
    1. if(player.getInventory().contains(ItemStackHere).getItemMeta().getDisplayName("Display name here")) {
    2. player.getInventory().remove(ItemStackHere);
    3. }
     
  3. Offline

    Ivan

    You'll have to work with colors if you want to do that in a safe manner...
     
  4. Offline

    BENCLABSTER

    True, but having a money plugin wouldn't you need to do so anyways? You wouldn't be able to differentiate between currencies if you didn't have them.
     
  5. Offline

    Ivan

    He might be setting the lore.
     
  6. Offline

    Monkeyboystein

    i did indeed set the display name with $AMOUNT
    the lore for all the money is the same
     
  7. Offline

    Ivan

    Don't forget to apply a chat color then.
     
  8. Offline

    BENCLABSTER

    Yeah he is correct. You would have to add chat color for example if it was green.
    Code:java
    1. if(player.getInventory().contains(ItemStackHere).getItemMeta().getDisplayName(ChatColor.GREEN + "Display name here")) {
    2. player.getInventory().remove(ItemStackHere);
    3. }


    The name is case sensitive aswell.
     
  9. Offline

    Monkeyboystein

    Yeah i think ill do that
     
  10. Offline

    BENCLABSTER

    oh, I forgot to add this. you need to specify the amount to remove, most likely one so just do this :p
    1. Code:java
      1. player.getInventory().remove(ItemStackHere, 1);
     
  11. Offline

    Monkeyboystein

    But
    Code:
    ItemStack money = new ItemStack(339, 1);
    if(player.getInventory().contains(money)) {
        if(money.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "$2" )) {
            amount += money.getAmount() * 2;
        }
       
    }
    I wont be able to get the different money and add them all up, or am i over thinking this
     
  12. Offline

    BENCLABSTER

    If all the display names are different, the money will be counted as separate itemstacks. so if you had an emerald named "$2" and another named "$5" they would count as different itemstacks. The money could still be sorted :p
     
  13. Offline

    Monkeyboystein

    Im sorry im not getting any of this but i have these at the top would there be a simpler way to use these to get the items?
    Code:java
    1. static {
    2. ItemStack paper = new ItemStack(Material.PAPER);
    3. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    4.  
    5. // Initialize $1 bills
    6. money_ones = paper.getItemMeta();
    7. money_ones.setDisplayName(ChatColor.GREEN + "$1");
    8. money_ones.setLore(moneyLore);
    9. // etc, do this for all the bill types
    10. }
    11. public static final ItemMeta money_twos;
    12. // etc for all the money types
    13.  
    14. static {
    15. ItemStack paper = new ItemStack(Material.PAPER);
    16. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    17.  
    18. // Initialize $1 bills
    19. money_twos = paper.getItemMeta();
    20. money_twos.setDisplayName(ChatColor.GREEN + "$2");
    21. money_twos.setLore(moneyLore);
    22. // etc, do this for all the bill types
    23. }
    24. public static final ItemMeta money_threes;
    25. // etc for all the money types
    26.  
    27. static {
    28. ItemStack paper = new ItemStack(Material.PAPER);
    29. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    30.  
    31. // Initialize $1 bills
    32. money_threes = paper.getItemMeta();
    33. money_threes.setDisplayName(ChatColor.GREEN + "$3");
    34. money_threes.setLore(moneyLore);
    35. // etc, do this for all the bill types
    36. }
    37. public static final ItemMeta money_fours;
    38. // etc for all the money types
    39.  
    40. static {
    41. ItemStack paper = new ItemStack(Material.PAPER);
    42. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    43.  
    44. // Initialize $1 bills
    45. money_fours = paper.getItemMeta();
    46. money_fours.setDisplayName(ChatColor.GREEN + "$4");
    47. money_fours.setLore(moneyLore);
    48. // etc, do this for all the bill types
    49. }
    50. public static final ItemMeta money_fives;
    51. // etc for all the money types
    52.  
    53. static {
    54. ItemStack paper = new ItemStack(Material.PAPER);
    55. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    56.  
    57. // Initialize $1 bills
    58. money_fives = paper.getItemMeta();
    59. money_fives.setDisplayName(ChatColor.GREEN + "$5");
    60. money_fives.setLore(moneyLore);
    61. // etc, do this for all the bill types
    62. }
    63. public static final ItemMeta money_sixs;
    64. // etc for all the money types
    65.  
    66. static {
    67. ItemStack paper = new ItemStack(Material.PAPER);
    68. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    69.  
    70. // Initialize $1 bills
    71. money_sixs = paper.getItemMeta();
    72. money_sixs.setDisplayName(ChatColor.GREEN + "$6");
    73. money_sixs.setLore(moneyLore);
    74. // etc, do this for all the bill types
    75. }
    76. public static final ItemMeta money_sevens;
    77. // etc for all the money types
    78.  
    79. static {
    80. ItemStack paper = new ItemStack(Material.PAPER);
    81. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    82.  
    83. // Initialize $1 bills
    84. money_sevens = paper.getItemMeta();
    85. money_sevens.setDisplayName(ChatColor.GREEN + "$7");
    86. money_sevens.setLore(moneyLore);
    87. // etc, do this for all the bill types
    88. }
    89. public static final ItemMeta money_eights;
    90. // etc for all the money types
    91.  
    92. static {
    93. ItemStack paper = new ItemStack(Material.PAPER);
    94. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    95.  
    96. // Initialize $1 bills
    97. money_eights = paper.getItemMeta();
    98. money_eights.setDisplayName(ChatColor.GREEN + "$8");
    99. money_eights.setLore(moneyLore);
    100. // etc, do this for all the bill types
    101. }
    102. public static final ItemMeta money_nines;
    103. // etc for all the money types
    104.  
    105. static {
    106. ItemStack paper = new ItemStack(Material.PAPER);
    107. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    108.  
    109. // Initialize $1 bills
    110. money_nines = paper.getItemMeta();
    111. money_nines.setDisplayName(ChatColor.GREEN + "$9");
    112. money_nines.setLore(moneyLore);
    113. // etc, do this for all the bill types
    114. }
    115. public static final ItemMeta money_tens;
    116. // etc for all the money types
    117.  
    118. static {
    119. ItemStack paper = new ItemStack(Material.PAPER);
    120. List<String> moneyLore = Arrays.asList(ChatColor.GRAY + "Issued by MonkeyCraft's bank department.");
    121.  
    122. // Initialize $1 bills
    123. money_tens = paper.getItemMeta();
    124. money_tens.setDisplayName(ChatColor.GREEN + "$1");
    125. money_tens.setLore(moneyLore);
    126. // etc, do this for all the bill types
    127. }
     
  14. Offline

    BENCLABSTER

    the best way to do this is to make a new itemstack, then get the item meta, set your things and set the itemmeta to what it was. ex.
    Code:java
    1. ItemStack money1 = new ItemStack(Material.PAPER);
    2. ItemMeta im = money1.getItemMeta();
    3. im.setDisplayName(ChatColor.GREEN + "$1");
    4. im.setLore(ChatColor.GRAY + "Lore");
    5. money1.setItemMeta(im);


    That will make a new itemstack, set the lore and display name all in 5 lines. Saves a lot of time. Just repeat that for other bills and rename the item meta and the itemstack
     
Thread Status:
Not open for further replies.

Share This Page