/Essentials/src/com/earth2me/essentials/api/Economy.java

https://github.com/BodyshotzVG/Essentials · Java · 293 lines · 165 code · 23 blank · 105 comment · 20 complexity · 3c6839d268242e27e4d913ba95e076c7 MD5 · raw file

  1. package com.earth2me.essentials.api;
  2. import com.earth2me.essentials.EssentialsConf;
  3. import com.earth2me.essentials.IEssentials;
  4. import com.earth2me.essentials.User;
  5. import com.earth2me.essentials.Util;
  6. import java.io.File;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import org.bukkit.entity.Player;
  10. /**
  11. * Instead of using this api directly, we recommend to use the register plugin:
  12. * http://bit.ly/RegisterMethod
  13. */
  14. public final class Economy
  15. {
  16. private Economy()
  17. {
  18. }
  19. private static final Logger logger = Logger.getLogger("Minecraft");
  20. private static IEssentials ess;
  21. /**
  22. * @param aEss the ess to set
  23. */
  24. public static void setEss(IEssentials aEss)
  25. {
  26. ess = aEss;
  27. }
  28. private static void createNPCFile(String name)
  29. {
  30. File folder = new File(ess.getDataFolder(), "userdata");
  31. if (!folder.exists())
  32. {
  33. folder.mkdirs();
  34. }
  35. EssentialsConf npcConfig = new EssentialsConf(new File(folder, Util.sanitizeFileName(name) + ".yml"));
  36. npcConfig.load();
  37. npcConfig.setProperty("npc", true);
  38. npcConfig.setProperty("money", ess.getSettings().getStartingBalance());
  39. npcConfig.save();
  40. }
  41. private static void deleteNPC(String name)
  42. {
  43. File folder = new File(ess.getDataFolder(), "userdata");
  44. if (!folder.exists())
  45. {
  46. folder.mkdirs();
  47. }
  48. File config = new File(folder, Util.sanitizeFileName(name) + ".yml");
  49. EssentialsConf npcConfig = new EssentialsConf(config);
  50. npcConfig.load();
  51. if (npcConfig.hasProperty("npc") && npcConfig.getBoolean("npc", false))
  52. {
  53. if (!config.delete())
  54. {
  55. logger.log(Level.WARNING, Util.format("deleteFileError", config));
  56. }
  57. ess.getUserMap().removeUser(name);
  58. }
  59. }
  60. private static User getUserByName(String name)
  61. {
  62. User user;
  63. Player player = ess.getServer().getPlayer(name);
  64. if (player != null)
  65. {
  66. user = ess.getUser(player);
  67. }
  68. else
  69. {
  70. user = ess.getOfflineUser(name);
  71. }
  72. return user;
  73. }
  74. /**
  75. * Returns the balance of a user
  76. * @param name Name of the user
  77. * @return balance
  78. * @throws UserDoesNotExistException
  79. */
  80. public static double getMoney(String name) throws UserDoesNotExistException
  81. {
  82. User user = getUserByName(name);
  83. if (user == null)
  84. {
  85. throw new UserDoesNotExistException(name);
  86. }
  87. return user.getMoney();
  88. }
  89. /**
  90. * Sets the balance of a user
  91. * @param name Name of the user
  92. * @param balance The balance you want to set
  93. * @throws UserDoesNotExistException If a user by that name does not exists
  94. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  95. */
  96. public static void setMoney(String name, double balance) throws UserDoesNotExistException, NoLoanPermittedException
  97. {
  98. User user = getUserByName(name);
  99. if (user == null)
  100. {
  101. throw new UserDoesNotExistException(name);
  102. }
  103. if (balance < 0.0 && !user.isAuthorized("essentials.eco.loan"))
  104. {
  105. throw new NoLoanPermittedException();
  106. }
  107. user.setMoney(balance);
  108. }
  109. /**
  110. * Adds money to the balance of a user
  111. * @param name Name of the user
  112. * @param amount The money you want to add
  113. * @throws UserDoesNotExistException If a user by that name does not exists
  114. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  115. */
  116. public static void add(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
  117. {
  118. double result = getMoney(name) + amount;
  119. setMoney(name, result);
  120. }
  121. /**
  122. * Substracts money from the balance of a user
  123. * @param name Name of the user
  124. * @param amount The money you want to substract
  125. * @throws UserDoesNotExistException If a user by that name does not exists
  126. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  127. */
  128. public static void subtract(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
  129. {
  130. double result = getMoney(name) - amount;
  131. setMoney(name, result);
  132. }
  133. /**
  134. * Divides the balance of a user by a value
  135. * @param name Name of the user
  136. * @param value The balance is divided by this value
  137. * @throws UserDoesNotExistException If a user by that name does not exists
  138. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  139. */
  140. public static void divide(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException
  141. {
  142. double result = getMoney(name) / value;
  143. setMoney(name, result);
  144. }
  145. /**
  146. * Multiplies the balance of a user by a value
  147. * @param name Name of the user
  148. * @param value The balance is multiplied by this value
  149. * @throws UserDoesNotExistException If a user by that name does not exists
  150. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  151. */
  152. public static void multiply(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException
  153. {
  154. double result = getMoney(name) * value;
  155. setMoney(name, result);
  156. }
  157. /**
  158. * Resets the balance of a user to the starting balance
  159. * @param name Name of the user
  160. * @throws UserDoesNotExistException If a user by that name does not exists
  161. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  162. */
  163. public static void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException
  164. {
  165. setMoney(name, ess.getSettings().getStartingBalance());
  166. }
  167. /**
  168. * @param name Name of the user
  169. * @param amount The amount of money the user should have
  170. * @return true, if the user has more or an equal amount of money
  171. * @throws UserDoesNotExistException If a user by that name does not exists
  172. */
  173. public static boolean hasEnough(String name, double amount) throws UserDoesNotExistException
  174. {
  175. return amount <= getMoney(name);
  176. }
  177. /**
  178. * @param name Name of the user
  179. * @param amount The amount of money the user should have
  180. * @return true, if the user has more money
  181. * @throws UserDoesNotExistException If a user by that name does not exists
  182. */
  183. public static boolean hasMore(String name, double amount) throws UserDoesNotExistException
  184. {
  185. return amount < getMoney(name);
  186. }
  187. /**
  188. * @param name Name of the user
  189. * @param amount The amount of money the user should not have
  190. * @return true, if the user has less money
  191. * @throws UserDoesNotExistException If a user by that name does not exists
  192. */
  193. public static boolean hasLess(String name, double amount) throws UserDoesNotExistException
  194. {
  195. return amount > getMoney(name);
  196. }
  197. /**
  198. * Test if the user has a negative balance
  199. * @param name Name of the user
  200. * @return true, if the user has a negative balance
  201. * @throws UserDoesNotExistException If a user by that name does not exists
  202. */
  203. public static boolean isNegative(String name) throws UserDoesNotExistException
  204. {
  205. return getMoney(name) < 0.0;
  206. }
  207. /**
  208. * Formats the amount of money like all other Essentials functions.
  209. * Example: $100000 or $12345.67
  210. * @param amount The amount of money
  211. * @return Formatted money
  212. */
  213. public static String format(double amount)
  214. {
  215. return Util.formatCurrency(amount, ess);
  216. }
  217. /**
  218. * Test if a player exists to avoid the UserDoesNotExistException
  219. * @param name Name of the user
  220. * @return true, if the user exists
  221. */
  222. public static boolean playerExists(String name)
  223. {
  224. return getUserByName(name) != null;
  225. }
  226. /**
  227. * Test if a player is a npc
  228. * @param name Name of the player
  229. * @return true, if it's a npc
  230. * @throws UserDoesNotExistException
  231. */
  232. public static boolean isNPC(String name) throws UserDoesNotExistException
  233. {
  234. User user = getUserByName(name);
  235. if (user == null)
  236. {
  237. throw new UserDoesNotExistException(name);
  238. }
  239. return user.isNPC();
  240. }
  241. /**
  242. * Creates dummy files for a npc, if there is no player yet with that name.
  243. * @param name Name of the player
  244. * @return true, if a new npc was created
  245. */
  246. public static boolean createNPC(String name)
  247. {
  248. User user = getUserByName(name);
  249. if (user == null)
  250. {
  251. createNPCFile(name);
  252. return true;
  253. }
  254. return false;
  255. }
  256. /**
  257. * Deletes a user, if it is marked as npc.
  258. * @param name Name of the player
  259. * @throws UserDoesNotExistException
  260. */
  261. public static void removeNPC(String name) throws UserDoesNotExistException
  262. {
  263. User user = getUserByName(name);
  264. if (user == null)
  265. {
  266. throw new UserDoesNotExistException(name);
  267. }
  268. deleteNPC(name);
  269. }
  270. }