Check if an item with damage is in players inventory

Discussion in 'Plugin Development' started by seang96, Jul 20, 2013.

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

    seang96

    I have seen a few methods around the forums but I have gotten none of them to fully work.
    Line 68 of the code below is the check, whatever method i use the if returns false and goes to its else statement. I would like to add support for colored wool etc in my plugin. Thanks.
    Code:java
    1. if (args[0].equalsIgnoreCase("Sell")) {
    2. if (args.length == 1) {
    3. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "Please type /shop sell <Item|ID> <Amount> <Price> [confirm]", getDescription().getName()));
    4. return false;
    5. }
    6. if (args.length > 5 || args.length < 4) {
    7. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "Syntax error.", getDescription().getName()));
    8. p.sendMessage(String.format(ChatColor.GOLD + "Please type /shop sell <Item|ID> <Amount> <Price> [confirm]", getDescription().getName()));
    9. return false;
    10. }
    11. if (args.length > 4 && args[4].equalsIgnoreCase("confirm")) {
    12. confirm = true;
    13. } else {
    14. confirm = false;
    15. if (args.length >= 5) {
    16. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "Error: Please enter a valid item name. If you used a space try replacing with _ (underscore).", getDescription().getName()));
    17. return false;
    18. }
    19. }
    20. if (config.getBoolean("AutoConfirm")) {
    21. if (!users.getBoolean(((Player) sender).getDisplayName()
    22. + ".Confirm")) {
    23. confirm = true;
    24. }
    25. } else {
    26. if (users.getBoolean(((Player) sender).getDisplayName()
    27. + ".Confirm")) {
    28. confirm = true;
    29. }
    30. }
    31. // long IDcount = data.getInt("Total");
    32. int amount = Integer.parseInt(args[2]);
    33. int price = Integer.parseInt(args[3]);
    34. int totalprice = price * amount;
    35. double t = config.getInt("Taxes");
    36. double t1 = t / 100;
    37. double tax = price * amount * t1;
    38. tax = (double) ((int) ((tax + 0.005) * 100) % Integer.MAX_VALUE) / 100;
    39. String[] itemAndData = args[1].split(":");
    40. int item = 0;
    41. try {
    42. item = Integer.parseInt(itemAndData[1]);
    43. } catch (NumberFormatException ex) {
    44. // Item was not an int, do nothing
    45. }
    46. Material mat = item == 0 ? Material.matchMaterial(itemAndData[0]) : Material.getMaterial(item);
    47. int damage = 0;
    48. if (mat == null) {
    49. p.sendMessage(String.format(ChatColor.RED + "Invalid item '" + itemAndData[0] + "'!"));
    50. return true;
    51. }
    52. if (itemAndData.length == 2) {
    53. try {
    54. damage = Integer.parseInt(itemAndData[1]);
    55. } catch (NumberFormatException ex) {
    56. // Data was not an int, complain here
    57. p.sendMessage(String.format(ChatColor.RED + "Data must be a valid number!"));
    58. return true;
    59. }
    60. }
    61. int id = mat.getId();
    62. ItemStack is = new ItemStack(mat, amount, (short) damage);
    63. if (confirm) {
    64. if (config.getBoolean("UseMaxTransactions") && data.getInt("Total") == config.getInt("MaxTransactions")) {
    65. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "The global limit of selling transactions have been reached. Wait for the current transactions to be canceled or sold.", getDescription().getName()));
    66. }
    67. else {
    68. if(checkForAmount(new ArrayList<ItemStack>(p.getInventory().all(mat).values()), amount, (short) damage)){
    69. p.getInventory().removeItem(is);
    70. EconomyResponse r = econ.withdrawPlayer(p.getName(),
    71. tax);
    72. if (r.transactionSuccess()) {
    73. // IDcount++;
    74. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "You have added " + ChatColor.DARK_AQUA + args[2] + " " + args[1] + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + args[3] + ChatColor.GOLD + " to the market. You have paid " + ChatColor.DARK_AQUA + String.valueOf(tax) + ChatColor.GOLD + " for taxes.", getDescription() .getName()));
    75. for(int i = 1; data.getInt("Total") == 0 || i <= (data.getInt("Total") + 1); i++) {
    76. if (config.getBoolean("Debug")) {
    77. log.info(String.format(" t = " + data.getInt("Total") + " i = " + i, getDescription()
    78. .getName()));
    79. }
    80. if (data.getString("Transactions." + i) == null) {
    81. loadYamls();
    82. data.set("Transactions." + i + ".Player",
    83. ((Player) sender).getDisplayName());
    84. if (itemAndData.length == 2) {
    85. data.set("Transactions." + i + ".Item", id + ":" + damage);
    86. }
    87. else {
    88. data.set("Transactions." + i + ".Item", id);
    89. }
    90. data.set("Transactions." + i + ".TotalAmount", amount);
    91. data.set("Transactions." + i + ".Amount", amount);
    92. data.set("Transactions." + i + ".Price", price);
    93. data.set("Transactions." + i + ".Status", "Selling");
    94. data.set("Transactions." + i + ".Check", "T");
    95. data.set("Total", i);
    96. saveYamls();
    97. return false;
    98. }
    99. if (data.getString("Transactions." + i + ".Status").equals("Sold") || data.getString("Transactions." + i + ".Status").equals("Cancelled")) {
    100. loadYamls();
    101. data.set("Transactions." + i + ".Player",
    102. ((Player) sender).getDisplayName());
    103. data.set("Transactions." + i + ".Item", id);
    104. data.set("Transactions." + i + ".TotalAmount", amount);
    105. data.set("Transactions." + i + ".Amount", amount);
    106. data.set("Transactions." + i + ".Price", price);
    107. data.set("Transactions." + i + ".Status", "Selling");
    108. data.set("Transactions." + i + ".Check", "T");
    109. saveYamls();
    110. return false;
    111. }
    112. }
    113. } else {
    114. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "An error occured: %s", getDescription() .getName(), r.errorMessage));
    115. }
    116. }
    117. else {
    118. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "You do not have " + ChatColor.DARK_AQUA + args[2] + " " + args[1] + ChatColor.GOLD + " please try selling at a lower amount.", getDescription() .getName()));
    119. }
    120. }
    121. }
    122. else {
    123. if (config.getBoolean("Debug")) {
    124. log.info(String.format(" t = " + t1, getDescription()
    125. .getName()));
    126. }
    127. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "You will sell " + ChatColor.DARK_AQUA + args[2] + " " + args[1] + ChatColor.GOLD + ". The total price will be " + ChatColor.DARK_AQUA + String.valueOf(totalprice) + ChatColor.GOLD + ". You wil spend " + ChatColor.DARK_AQUA + String.valueOf(tax) + ChatColor.GOLD + " for taxes.", getDescription().getName()));
    128. }
    129. } else if (args[0].equalsIgnoreCase("List")) {
    130. if (args.length == 1) {
    131. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "Please type /shop list <amount|common|expensive|recent> [Item|ID]", getDescription().getName()));
    132. return false;
    133. }
    134. if (args.length > 3 || args.length < 2) {
    135. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "Syntax error.", getDescription().getName()));
    136. p.sendMessage(String.format(ChatColor.GOLD + "Please type /shop list <amount|common|expensive|recent> [Item|ID]", getDescription().getName()));
    137. return false;
    138. }
    139. if (args[1].equalsIgnoreCase("amount")) {
    140. if (args.length > 2 && Material.matchMaterial(args[2]) != null) {
    141. Material mat = Material.matchMaterial(args[2]);
    142. int amount = 0;
    143. for (int i = 1; i <= data.getInt("Total"); i++) {
    144. if (data.getInt("Transactions." + i + ".Item") == mat.getId()
    145. && data.getString("Transactions." + i + ".Status").equals(
    146. "Selling")) {
    147. amount += data.getInt("Transactions." + i + ".Amount");
    148. if (config.getBoolean("Debug")) {
    149. log.info(String.format(" A = " + amount,
    150. getDescription().getName()));
    151. }
    152. }
    153. }
    154. if (amount == 1) {
    155. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "There is only " + ChatColor.DARK_AQUA + amount + " " + args[2] + ChatColor.GOLD + " left.", getDescription().getName()));
    156. } else if (amount == 0) {
    157. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "There is no " + ChatColor.DARK_AQUA + args[2] + ChatColor.GOLD + " on the market.", getDescription().getName()));
    158. } else {
    159. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "There are " + ChatColor.DARK_AQUA + amount + " " + args[2] + ChatColor.GOLD + " left.", getDescription().getName()));
    160. }
    161. } else {
    162. p.sendMessage(String.format("[" + ChatColor.LIGHT_PURPLE + "%s" + ChatColor.RESET + "] " + ChatColor.GOLD + "Please specify a item. /shop list amount <Item|ID>", getDescription().getName()));
    163. }
    164. }
     
  2. Offline

    xTrollxDudex

    seang96
    I would cast wool onto the item and use get color to check for a certain DyeColor (enum)
     
  3. Offline

    seang96

    That would still limit other items. The plugins a shop I need it to support dyes etc.
     
  4. Offline

    xTrollxDudex

  5. Offline

    seang96

    Do you have any other ideas to support every item?
     
  6. Offline

    xTrollxDudex

    seang96
    Nope. I'm not good with ideas. Just developing.
     
Thread Status:
Not open for further replies.

Share This Page