/Essentials/src/com/earth2me/essentials/Trade.java

https://github.com/jeremyroza/Essentials · Java · 258 lines · 241 code · 17 blank · 0 comment · 65 complexity · 037bfd48c6a7327f1b1ca502bb562457 MD5 · raw file

  1. package com.earth2me.essentials;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.text.DateFormat;
  6. import java.util.Date;
  7. import java.util.Map;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import org.bukkit.Location;
  11. import org.bukkit.inventory.ItemStack;
  12. public class Trade
  13. {
  14. private final transient String command;
  15. private final transient Double money;
  16. private final transient ItemStack itemStack;
  17. private final transient IEssentials ess;
  18. public Trade(final String command, final IEssentials ess)
  19. {
  20. this(command, null, null, ess);
  21. }
  22. public Trade(final double money, final IEssentials ess)
  23. {
  24. this(null, money, null, ess);
  25. }
  26. public Trade(final ItemStack items, final IEssentials ess)
  27. {
  28. this(null, null, items, ess);
  29. }
  30. private Trade(final String command, final Double money, final ItemStack item, final IEssentials ess)
  31. {
  32. this.command = command;
  33. this.money = money;
  34. this.itemStack = item;
  35. this.ess = ess;
  36. }
  37. public void isAffordableFor(final IUser user) throws ChargeException
  38. {
  39. final double mon = user.getMoney();
  40. if (getMoney() != null
  41. && mon < getMoney()
  42. && getMoney() > 0
  43. && !user.isAuthorized("essentials.eco.loan"))
  44. {
  45. throw new ChargeException(Util.i18n("notEnoughMoney"));
  46. }
  47. if (getItemStack() != null
  48. && !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
  49. {
  50. throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
  51. }
  52. if (command != null && !command.isEmpty()
  53. && !user.isAuthorized("essentials.nocommandcost.all")
  54. && !user.isAuthorized("essentials.nocommandcost." + command)
  55. && mon < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
  56. && 0 < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
  57. && !user.isAuthorized("essentials.eco.loan"))
  58. {
  59. throw new ChargeException(Util.i18n("notEnoughMoney"));
  60. }
  61. }
  62. public void pay(final IUser user)
  63. {
  64. pay(user, true);
  65. }
  66. public boolean pay(final IUser user, final boolean dropItems)
  67. {
  68. boolean success = true;
  69. if (getMoney() != null && getMoney() > 0)
  70. {
  71. user.giveMoney(getMoney());
  72. }
  73. if (getItemStack() != null)
  74. {
  75. if (dropItems)
  76. {
  77. final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
  78. for (ItemStack itemStack : leftOver.values())
  79. {
  80. InventoryWorkaround.dropItem(user.getLocation(), itemStack);
  81. }
  82. }
  83. else
  84. {
  85. success = InventoryWorkaround.addAllItems(user.getInventory(), true, getItemStack());
  86. }
  87. user.updateInventory();
  88. }
  89. return success;
  90. }
  91. public void charge(final IUser user) throws ChargeException
  92. {
  93. if (getMoney() != null)
  94. {
  95. final double mon = user.getMoney();
  96. if (mon < getMoney() && getMoney() > 0 && !user.isAuthorized("essentials.eco.loan"))
  97. {
  98. throw new ChargeException(Util.i18n("notEnoughMoney"));
  99. }
  100. user.takeMoney(getMoney());
  101. }
  102. if (getItemStack() != null)
  103. {
  104. if (!InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
  105. {
  106. throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
  107. }
  108. InventoryWorkaround.removeItem(user.getInventory(), true, getItemStack());
  109. user.updateInventory();
  110. }
  111. if (command != null && !command.isEmpty()
  112. && !user.isAuthorized("essentials.nocommandcost.all")
  113. && !user.isAuthorized("essentials.nocommandcost." + command))
  114. {
  115. final double mon = user.getMoney();
  116. final double cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command);
  117. if (mon < cost && cost > 0 && !user.isAuthorized("essentials.eco.loan"))
  118. {
  119. throw new ChargeException(Util.i18n("notEnoughMoney"));
  120. }
  121. user.takeMoney(cost);
  122. }
  123. }
  124. public Double getMoney()
  125. {
  126. return money;
  127. }
  128. public ItemStack getItemStack()
  129. {
  130. return itemStack;
  131. }
  132. private static FileWriter fw = null;
  133. public static void log(String type, String subtype, String event, String sender, Trade charge, String receiver, Trade pay, Location loc, IEssentials ess)
  134. {
  135. if (!ess.getSettings().isEcoLogEnabled())
  136. {
  137. return;
  138. }
  139. if (fw == null)
  140. {
  141. try
  142. {
  143. fw = new FileWriter(new File(ess.getDataFolder(), "trade.log"), true);
  144. }
  145. catch (IOException ex)
  146. {
  147. Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
  148. }
  149. }
  150. StringBuilder sb = new StringBuilder();
  151. sb.append(type).append(",").append(subtype).append(",").append(event).append(",\"");
  152. sb.append(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()));
  153. sb.append("\",\"");
  154. if (sender != null)
  155. {
  156. sb.append(sender);
  157. }
  158. sb.append("\",");
  159. if (charge == null)
  160. {
  161. sb.append("\"\",\"\",\"\"");
  162. }
  163. else
  164. {
  165. if (charge.getItemStack() != null)
  166. {
  167. sb.append(charge.getItemStack().getAmount()).append(",");
  168. sb.append(charge.getItemStack().getType().toString()).append(",");
  169. sb.append(charge.getItemStack().getDurability());
  170. }
  171. if (charge.getMoney() != null)
  172. {
  173. sb.append(charge.getMoney()).append(",");
  174. sb.append("money").append(",");
  175. sb.append(ess.getSettings().getCurrencySymbol());
  176. }
  177. }
  178. sb.append(",\"");
  179. if (receiver != null)
  180. {
  181. sb.append(receiver);
  182. }
  183. sb.append("\",");
  184. if (pay == null)
  185. {
  186. sb.append("\"\",\"\",\"\"");
  187. }
  188. else
  189. {
  190. if (pay.getItemStack() != null)
  191. {
  192. sb.append(pay.getItemStack().getAmount()).append(",");
  193. sb.append(pay.getItemStack().getType().toString()).append(",");
  194. sb.append(pay.getItemStack().getDurability());
  195. }
  196. if (pay.getMoney() != null)
  197. {
  198. sb.append(pay.getMoney()).append(",");
  199. sb.append("money").append(",");
  200. sb.append(ess.getSettings().getCurrencySymbol());
  201. }
  202. }
  203. if (loc == null)
  204. {
  205. sb.append(",\"\",\"\",\"\",\"\"");
  206. }
  207. else
  208. {
  209. sb.append(",\"");
  210. sb.append(loc.getWorld().getName()).append("\",");
  211. sb.append(loc.getBlockX()).append(",");
  212. sb.append(loc.getBlockY()).append(",");
  213. sb.append(loc.getBlockZ()).append(",");
  214. }
  215. sb.append("\n");
  216. try
  217. {
  218. fw.write(sb.toString());
  219. fw.flush();
  220. }
  221. catch (IOException ex)
  222. {
  223. Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
  224. }
  225. }
  226. public static void closeLog()
  227. {
  228. if (fw != null)
  229. {
  230. try
  231. {
  232. fw.close();
  233. }
  234. catch (IOException ex)
  235. {
  236. Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
  237. }
  238. fw = null;
  239. }
  240. }
  241. }