/iExchange/src/SellItems.java

https://github.com/darkdiplomat/iExchange · Java · 242 lines · 238 code · 4 blank · 0 comment · 85 complexity · 2f937eaccf50e6514dc0d766debb6bd6 MD5 · raw file

  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. public class SellItems{
  8. static Logger log = Logger.getLogger("Minecraft");
  9. public static void Sell(Player player, String[] split){
  10. String ItemID;
  11. String SellItemPricesFile = "plugins/config/iExchange/SellItemPrices.txt";
  12. if (split.length == 2){
  13. player.sendMessage("§e[iExchange] §bUsage is /iex sell [Item] [Amount]");
  14. return;
  15. }else{
  16. String Numberof = null;
  17. Inventory inventory = player.getInventory();
  18. if (split.length > 3){
  19. Numberof = split[3];
  20. }
  21. int amount = 0;
  22. if (Numberof != null){
  23. try{
  24. amount = Integer.parseInt(Numberof);
  25. }catch (NumberFormatException localNumberFormatException) {
  26. player.sendMessage("§e[iExchage] §cERROR 409: CONFLICT");
  27. player.sendMessage("§c(You didn't enter a proper number)");
  28. return;
  29. }
  30. }else{
  31. amount = 1;
  32. }
  33. if (amount <= 0){
  34. player.sendMessage("§e[iExchage] §cERROR 409: CONFLICT");
  35. player.sendMessage("§c(You entered a number less than 1)");
  36. return;
  37. }
  38. ItemID = split[2].toUpperCase();
  39. ArrayList<String> ITEMPRICES = new ArrayList<String>();
  40. try {
  41. BufferedReader in = new BufferedReader(new FileReader(SellItemPricesFile));
  42. String str;
  43. while ((str = in.readLine()) != null){
  44. String Upped = str.toUpperCase();
  45. String[] CheckName = Upped.split(":");
  46. if (CheckName[0].equals(ItemID)){
  47. ITEMPRICES.add(str.toString());
  48. }
  49. }
  50. in.close();
  51. } catch (IOException e) {
  52. player.sendMessage("§e[iExchange] §cERROR 404: FILE NOT FOUND!");
  53. player.sendMessage("§c(SellItemPrices file error! Have an ADMIN verify the file)");
  54. log.log(Level.SEVERE, "[iExchange] Failed to LOAD: \"SellItemPrices.txt\", unable to continue.");
  55. ITEMPRICES.clear();
  56. return;
  57. }
  58. if (!(ITEMPRICES.isEmpty())){
  59. String ItemIDS = ITEMPRICES.toString();
  60. String[] Item = ItemIDS.split(":");
  61. String PricePrice;
  62. int ID = 0;
  63. int price = 0;
  64. int damage = 0;
  65. try{
  66. ID = Integer.parseInt(Item[1]);
  67. }catch (NumberFormatException n1) {
  68. player.sendMessage("§e[iExchange] §cERROR 404: ITEM NOT FOUND!");
  69. player.sendMessage("§c(SellItemPrices file error! Have an ADMIN verify the file)");
  70. log.log(Level.INFO, "[iExchange] Failed to LOAD: " + Item[1] + " from \"SellItemPrices.txt\" (is the ID set correctly?)");
  71. ITEMPRICES.clear();
  72. return;
  73. }
  74. Item itemd = new Item();
  75. if ((ID == 6) || (ID == 17) || (ID == 18) || (ID == 35) || (ID == 44) || (ID == 98) || (ID == 263) || (ID == 351) || (ID == 358)){
  76. damage = Integer.parseInt(Item[2]);
  77. if (Item[3].contains(",")){
  78. String[] unfuck = Item[3].split(",");
  79. PricePrice = unfuck[0];
  80. itemd = new Item(ID, amount, -1, damage);
  81. }else{
  82. PricePrice = Item[3].replace("]", "");
  83. itemd = new Item(ID, amount, -1, damage);
  84. }
  85. }else{
  86. if (Item[2].contains(",")){
  87. String[] unfuck = Item[2].split(",");
  88. PricePrice = unfuck[0];
  89. }else{
  90. PricePrice = Item[2].replace("]", "");
  91. }
  92. itemd = new Item(ID, amount);
  93. }
  94. try{
  95. price = Integer.parseInt(PricePrice);
  96. }catch (NumberFormatException n2) {
  97. player.sendMessage("§e[iExchange] §cERROR 404: PRICE NOT FOUND!");
  98. player.sendMessage("§c(Props file error! Have an ADMIN verify the file)");
  99. log.log(Level.INFO, "[iExchange] Failed to LOAD:" + PricePrice + " \"SellItemPrices.txt\", (is the price set correctly?)");
  100. ITEMPRICES.clear();
  101. return;
  102. }
  103. String ItemItem = Item[0].replace("[", "");
  104. String ItemGave = ItemItem.toUpperCase();
  105. if (inventory.hasItem(itemd.getItemId())){
  106. if ((ID == 6) || (ID == 17) || (ID == 18) || (ID == 35) || (ID == 44) || (ID == 98) || (ID == 263) || (ID == 351) || (ID == 358)){
  107. int i = -1;
  108. int scan = -1;
  109. int invamount = 0;
  110. while (scan <= 40){
  111. scan++;
  112. if (inventory.getItemFromSlot(scan) != null){
  113. Item itemcheck = inventory.getItemFromSlot(scan);
  114. int id1 = itemd.getItemId();
  115. int id2 = itemcheck.getItemId();
  116. int d1 = itemd.getDamage();
  117. int d2 = itemcheck.getDamage();
  118. if ((id1 == id2) && (d1 == d2)){
  119. invamount += itemcheck.getAmount();
  120. }
  121. }
  122. }
  123. if (invamount < amount){
  124. player.sendMessage("§e[iExchange] §cERROR 404: AMOUNT NOT FOUND!");
  125. player.sendMessage("§cYou don't have that much!");
  126. ITEMPRICES.clear();
  127. return;
  128. }
  129. while (i <= 40){
  130. i++;
  131. if (inventory.getItemFromSlot(i) != null){
  132. Item item2 = inventory.getItemFromSlot(i);
  133. int id1 = itemd.getItemId();
  134. int id2 = item2.getItemId();
  135. int d1 = itemd.getDamage();
  136. int d2 = item2.getDamage();
  137. if ((id1 == id2) && (d1 == d2)){
  138. if (amount > 0){
  139. int oldamount = item2.getAmount();
  140. int newamount = (oldamount - amount);
  141. if (newamount <=0){
  142. inventory.removeItem(i);
  143. inventory.update();
  144. amount -= oldamount;
  145. }else{
  146. inventory.removeItem(i);
  147. inventory.setSlot(itemd.getItemId(), newamount, d1, i);
  148. inventory.update();
  149. amount -= oldamount;
  150. }
  151. }else{
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. if (Numberof != null){
  158. amount = Integer.parseInt(Numberof);
  159. }else{
  160. amount = 1;
  161. }
  162. etc.getLoader().callCustomHook("iBalance", new Object[] { "deposit", player.getName(), (price*amount)});
  163. player.sendMessage("§e[iExchange] §2" + amount + " " + ItemGave + " §bsold for §2$" + price*amount);
  164. ITEMPRICES.clear();
  165. return;
  166. }else if (ID != 0){
  167. int scan = -1;
  168. int invamount = 0;
  169. while (scan <= 36){
  170. scan++;
  171. if (inventory.getItemFromSlot(scan) != null){
  172. Item itemcheck = inventory.getItemFromSlot(scan);
  173. int id1 = itemd.getItemId();
  174. int id2 = itemcheck.getItemId();
  175. if (id1 == id2){
  176. invamount += itemcheck.getAmount();
  177. }
  178. }
  179. }
  180. if (invamount < amount){
  181. player.sendMessage("§e[iExchange] §cERROR 404: AMOUNT NOT FOUND!");
  182. player.sendMessage("§cYou don't have that much!");
  183. ITEMPRICES.clear();
  184. return;
  185. }
  186. int i = -1;
  187. while (i <= 40){
  188. i++;
  189. if (inventory.getItemFromSlot(i) != null){
  190. Item item2 = inventory.getItemFromSlot(i);
  191. int id1 = itemd.getItemId();
  192. int id2 = item2.getItemId();
  193. if ((id1 == id2)){
  194. if (amount > 0){
  195. int oldamount = item2.getAmount();
  196. int newamount = (oldamount - amount);
  197. if (newamount <=0){
  198. inventory.removeItem(i);
  199. inventory.update();
  200. amount -= oldamount;
  201. }else{
  202. inventory.removeItem(i);
  203. inventory.setSlot(itemd.getItemId(), newamount, i);
  204. inventory.update();
  205. amount -= oldamount;
  206. }
  207. }else{
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. if (Numberof != null){
  214. amount = Integer.parseInt(Numberof);
  215. }else{
  216. amount = 1;
  217. }
  218. etc.getLoader().callCustomHook("iBalance", new Object[] { "deposit", player.getName(), (price*amount)});
  219. player.sendMessage("§e[iExchange] §2" + amount + " " + ItemGave + " §bsold for §2$" + price*amount);
  220. ITEMPRICES.clear();
  221. return;
  222. }
  223. }else{
  224. player.sendMessage("§e[iExchange] §cERROR 404: ITEM NOT FOUND!");
  225. player.sendMessage("§cYou don't have that item!");
  226. ITEMPRICES.clear();
  227. return;
  228. }
  229. }else{
  230. player.sendMessage("§e[iExchange] §cError 400: ITEM NOT ON LIST!");
  231. player.sendMessage("§c(check your spelling or ask an ADMIN if item is on the list)");
  232. ITEMPRICES.clear();
  233. return;
  234. }
  235. }
  236. return;
  237. }
  238. }