PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/milkbowl/vault/economy/plugins/Economy_MultiCurrency.java

https://gitlab.com/MineYourMind/Vault
Java | 294 lines | 224 code | 54 blank | 16 comment | 21 complexity | 833ec53703aa2e627de16fc5487b9ad9 MD5 | raw file
  1. /* This file is part of Vault.
  2. Vault is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Lesser General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. Vault is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with Vault. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package net.milkbowl.vault.economy.plugins;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.logging.Logger;
  17. import me.ashtheking.currency.Currency;
  18. import me.ashtheking.currency.CurrencyList;
  19. import net.milkbowl.vault.economy.AbstractEconomy;
  20. import net.milkbowl.vault.economy.EconomyResponse;
  21. import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
  22. import org.bukkit.Bukkit;
  23. import org.bukkit.event.EventHandler;
  24. import org.bukkit.event.EventPriority;
  25. import org.bukkit.event.Listener;
  26. import org.bukkit.event.server.PluginDisableEvent;
  27. import org.bukkit.event.server.PluginEnableEvent;
  28. import org.bukkit.plugin.Plugin;
  29. public class Economy_MultiCurrency extends AbstractEconomy {
  30. private static final Logger log = Logger.getLogger("Minecraft");
  31. private final String name = "MultiCurrency";
  32. private Plugin plugin = null;
  33. private Currency economy = null;
  34. public Economy_MultiCurrency(Plugin plugin) {
  35. this.plugin = plugin;
  36. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  37. // Load Plugin in case it was loaded before
  38. if (economy == null) {
  39. Plugin multiCurrency = plugin.getServer().getPluginManager().getPlugin("MultiCurrency");
  40. if (multiCurrency != null && multiCurrency.isEnabled()) {
  41. economy = (Currency) multiCurrency;
  42. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  43. }
  44. }
  45. }
  46. @Override
  47. public String getName() {
  48. return name;
  49. }
  50. @Override
  51. public boolean isEnabled() {
  52. if (economy == null) {
  53. return false;
  54. } else {
  55. return economy.isEnabled();
  56. }
  57. }
  58. @Override
  59. public double getBalance(String playerName) {
  60. final double balance;
  61. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  62. final double fBalance = balance;
  63. return fBalance;
  64. }
  65. @Override
  66. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  67. double balance;
  68. EconomyResponse.ResponseType type;
  69. String errorMessage = null;
  70. if (amount < 0) {
  71. errorMessage = "Cannot withdraw negative funds";
  72. type = EconomyResponse.ResponseType.FAILURE;
  73. amount = 0;
  74. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  75. return new EconomyResponse(amount, balance, type, errorMessage);
  76. }
  77. if (!CurrencyList.hasEnough(playerName, amount)) {
  78. errorMessage = "Insufficient funds";
  79. type = EconomyResponse.ResponseType.FAILURE;
  80. amount = 0;
  81. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  82. return new EconomyResponse(amount, balance, type, errorMessage);
  83. }
  84. if (CurrencyList.subtract(playerName, amount)) {
  85. type = EconomyResponse.ResponseType.SUCCESS;
  86. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  87. return new EconomyResponse(amount, balance, type, errorMessage);
  88. } else {
  89. errorMessage = "Error withdrawing funds";
  90. type = EconomyResponse.ResponseType.FAILURE;
  91. amount = 0;
  92. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  93. return new EconomyResponse(amount, balance, type, errorMessage);
  94. }
  95. }
  96. @Override
  97. public EconomyResponse depositPlayer(String playerName, double amount) {
  98. double balance;
  99. EconomyResponse.ResponseType type;
  100. String errorMessage = null;
  101. if (amount < 0) {
  102. errorMessage = "Cannot deposit negative funds";
  103. type = EconomyResponse.ResponseType.FAILURE;
  104. amount = 0;
  105. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  106. return new EconomyResponse(amount, balance, type, errorMessage);
  107. }
  108. if (CurrencyList.add(playerName, amount)) {
  109. type = EconomyResponse.ResponseType.SUCCESS;
  110. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  111. return new EconomyResponse(amount, balance, type, errorMessage);
  112. } else {
  113. errorMessage = "Error withdrawing funds";
  114. type = EconomyResponse.ResponseType.FAILURE;
  115. amount = 0;
  116. balance = CurrencyList.getValue((String) CurrencyList.maxCurrency(playerName)[0], playerName);
  117. return new EconomyResponse(amount, balance, type, errorMessage);
  118. }
  119. }
  120. public class EconomyServerListener implements Listener {
  121. Economy_MultiCurrency economy = null;
  122. public EconomyServerListener(Economy_MultiCurrency economy) {
  123. this.economy = economy;
  124. }
  125. @EventHandler(priority = EventPriority.MONITOR)
  126. public void onPluginEnable(PluginEnableEvent event) {
  127. if (economy.economy == null) {
  128. Plugin mcur = event.getPlugin();
  129. if (mcur.getDescription().getName().equals("MultiCurrency")) {
  130. economy.economy = (Currency) mcur;
  131. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  132. }
  133. }
  134. }
  135. @EventHandler(priority = EventPriority.MONITOR)
  136. public void onPluginDisable(PluginDisableEvent event) {
  137. if (economy.economy != null) {
  138. if (event.getPlugin().getDescription().getName().equals("MultiCurrency")) {
  139. economy.economy = null;
  140. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  141. }
  142. }
  143. }
  144. }
  145. @Override
  146. public String format(double amount) {
  147. return String.format("%.2f %s", amount, "currency");
  148. }
  149. @Override
  150. public String currencyNameSingular() {
  151. return "currency";
  152. }
  153. @Override
  154. public String currencyNamePlural() {
  155. return "currency";
  156. }
  157. @Override
  158. public boolean has(String playerName, double amount) {
  159. return getBalance(playerName) >= amount;
  160. }
  161. @Override
  162. public EconomyResponse createBank(String name, String player) {
  163. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts");
  164. }
  165. @Override
  166. public EconomyResponse deleteBank(String name) {
  167. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts!");
  168. }
  169. @Override
  170. public EconomyResponse bankHas(String name, double amount) {
  171. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts");
  172. }
  173. @Override
  174. public EconomyResponse bankWithdraw(String name, double amount) {
  175. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts");
  176. }
  177. @Override
  178. public EconomyResponse bankDeposit(String name, double amount) {
  179. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts");
  180. }
  181. @Override
  182. public EconomyResponse isBankOwner(String name, String playerName) {
  183. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts");
  184. }
  185. @Override
  186. public EconomyResponse isBankMember(String name, String playerName) {
  187. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts");
  188. }
  189. @Override
  190. public EconomyResponse bankBalance(String name) {
  191. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "MultiCurrency does not support bank accounts");
  192. }
  193. @Override
  194. public List<String> getBanks() {
  195. return new ArrayList<String>();
  196. }
  197. @Override
  198. public boolean hasBankSupport() {
  199. return false;
  200. }
  201. @Override
  202. public boolean hasAccount(String playerName) {
  203. return true;
  204. }
  205. @Override
  206. public boolean createPlayerAccount(String playerName) {
  207. return false;
  208. }
  209. @Override
  210. public int fractionalDigits() {
  211. return -1;
  212. }
  213. @Override
  214. public boolean hasAccount(String playerName, String worldName) {
  215. return hasAccount(playerName);
  216. }
  217. @Override
  218. public double getBalance(String playerName, String world) {
  219. return getBalance(playerName);
  220. }
  221. @Override
  222. public boolean has(String playerName, String worldName, double amount) {
  223. return has(playerName, amount);
  224. }
  225. @Override
  226. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  227. return withdrawPlayer(playerName, amount);
  228. }
  229. @Override
  230. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  231. return depositPlayer(playerName, amount);
  232. }
  233. @Override
  234. public boolean createPlayerAccount(String playerName, String worldName) {
  235. return createPlayerAccount(playerName);
  236. }
  237. }