Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. public static void setMoney(String name, BigDecimal balance) throws UserDoesNotExistException, NoLoanPermittedException
  2. {
  3. User user = getUserByName(name);
  4. if (user == null)
  5. {
  6. throw new UserDoesNotExistException(name);
  7. }
  8. if (balance.compareTo(ess.getSettings().getMinMoney()) < 0)
  9. {
  10. throw new NoLoanPermittedException();
  11. }
  12. if (balance.signum() < 0 && !user.isAuthorized("essentials.eco.loan"))
  13. {
  14. throw new NoLoanPermittedException();
  15. }
  16. try
  17. {
  18. user.setMoney(balance);
  19. }
  20. catch (MaxMoneyException ex)
  21. {
  22. //TODO: Update API to show max balance errors
  23. }
  24. Trade.log("API", "Set", "API", name, new Trade(balance, ess), null, null, null, ess);
  25. }
  26.  
  27. /**
  28. * Adds money to the balance of a user
  29. *
  30. * @param name Name of the user
  31. * @param amount The money you want to add
  32. * @throws UserDoesNotExistException If a user by that name does not exists
  33. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  34. */
  35. @Deprecated
  36. public static void add(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
  37. {
  38. try
  39. {
  40. add(name, BigDecimal.valueOf(amount));
  41. }
  42. catch (ArithmeticException e)
  43. {
  44. logger.log(Level.WARNING, "Failed to add " + amount + " to balance of " + name + ": " + e.getMessage(), e);
  45. }
  46. }
  47.  
  48. public static void add(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException
  49. {
  50. BigDecimal result = getMoneyExact(name).add(amount, MATH_CONTEXT);
  51. setMoney(name, result);
  52. Trade.log("API", "Add", "API", name, new Trade(amount, ess), null, null, null, ess);
  53. }
  54.  
  55. /**
  56. * Substracts money from the balance of a user
  57. *
  58. * @param name Name of the user
  59. * @param amount The money you want to substract
  60. * @throws UserDoesNotExistException If a user by that name does not exists
  61. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  62. */
  63. @Deprecated
  64. public static void subtract(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
  65. {
  66. try
  67. {
  68. substract(name, BigDecimal.valueOf(amount));
  69. }
  70. catch (ArithmeticException e)
  71. {
  72. logger.log(Level.WARNING, "Failed to substract " + amount + " of balance of " + name + ": " + e.getMessage(), e);
  73. }
  74. }
  75.  
  76. public static void substract(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException
  77. {
  78. BigDecimal result = getMoneyExact(name).subtract(amount, MATH_CONTEXT);
  79. setMoney(name, result);
  80. Trade.log("API", "Subtract", "API", name, new Trade(amount, ess), null, null, null, ess);
  81. }
  82.  
  83. /**
  84. * Divides the balance of a user by a value
  85. *
  86. * @param name Name of the user
  87. * @param value The balance is divided by this value
  88. * @throws UserDoesNotExistException If a user by that name does not exists
  89. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  90. */
  91. @Deprecated
  92. public static void divide(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
  93. {
  94. try
  95. {
  96. divide(name, BigDecimal.valueOf(amount));
  97. }
  98. catch (ArithmeticException e)
  99. {
  100. logger.log(Level.WARNING, "Failed to divide balance of " + name + " by " + amount + ": " + e.getMessage(), e);
  101. }
  102. }
  103.  
  104. public static void divide(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException
  105. {
  106. BigDecimal result = getMoneyExact(name).divide(amount, MATH_CONTEXT);
  107. setMoney(name, result);
  108. Trade.log("API", "Divide", "API", name, new Trade(amount, ess), null, null, null, ess);
  109. }
  110.  
  111. /**
  112. * Multiplies the balance of a user by a value
  113. *
  114. * @param name Name of the user
  115. * @param value The balance is multiplied by this value
  116. * @throws UserDoesNotExistException If a user by that name does not exists
  117. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  118. */
  119. @Deprecated
  120. public static void multiply(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
  121. {
  122. try
  123. {
  124. multiply(name, BigDecimal.valueOf(amount));
  125. }
  126. catch (ArithmeticException e)
  127. {
  128. logger.log(Level.WARNING, "Failed to multiply balance of " + name + " by " + amount + ": " + e.getMessage(), e);
  129. }
  130. }
  131.  
  132. public static void multiply(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException
  133. {
  134. BigDecimal result = getMoneyExact(name).multiply(amount, MATH_CONTEXT);
  135. setMoney(name, result);
  136. Trade.log("API", "Multiply", "API", name, new Trade(amount, ess), null, null, null, ess);
  137. }
  138.  
  139. /**
  140. * Resets the balance of a user to the starting balance
  141. *
  142. * @param name Name of the user
  143. * @throws UserDoesNotExistException If a user by that name does not exists
  144. * @throws NoLoanPermittedException If the user is not allowed to have a negative balance
  145. */
  146. public static void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException
  147. {
  148. if (ess == null)
  149. {
  150. throw new RuntimeException(noCallBeforeLoad);
  151. }
  152. setMoney(name, ess.getSettings().getStartingBalance());
  153. Trade.log("API", "Reset", "API", name, new Trade(BigDecimal.ZERO, ess), null, null, null, ess);
  154. }
  155.  
  156. /**
  157. * @param name Name of the user
  158. * @param amount The amount of money the user should have
  159. * @return true, if the user has more or an equal amount of money
  160. * @throws UserDoesNotExistException If a user by that name does not exists
  161. */
  162. @Deprecated
  163. public static boolean hasEnough(String name, double amount) throws UserDoesNotExistException
  164. {
  165. try
  166. {
  167. return hasEnough(name, BigDecimal.valueOf(amount));
  168. }
  169. catch (ArithmeticException e)
  170. {
  171. logger.log(Level.WARNING, "Failed to compare balance of " + name + " with " + amount + ": " + e.getMessage(), e);
  172. return false;
  173. }
  174. }
  175.  
  176. public static boolean hasEnough(String name, BigDecimal amount) throws UserDoesNotExistException, ArithmeticException
  177. {
  178. return amount.compareTo(getMoneyExact(name)) <= 0;
  179. }
  180.  
  181. /**
  182. * @param name Name of the user
  183. * @param amount The amount of money the user should have
  184. * @return true, if the user has more money
  185. * @throws UserDoesNotExistException If a user by that name does not exists
  186. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement