PageRenderTime 272ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 304 lines | 240 code | 48 blank | 16 comment | 49 complexity | 31f58586491ddc3eaab8a0db732a8d3f 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 is.currency.Currency;
  15. import is.currency.syst.AccountContext;
  16. import java.util.List;
  17. import java.util.logging.Logger;
  18. import net.milkbowl.vault.economy.AbstractEconomy;
  19. import net.milkbowl.vault.economy.EconomyResponse;
  20. import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
  21. import org.bukkit.Bukkit;
  22. import org.bukkit.event.EventHandler;
  23. import org.bukkit.event.EventPriority;
  24. import org.bukkit.event.Listener;
  25. import org.bukkit.event.server.PluginDisableEvent;
  26. import org.bukkit.event.server.PluginEnableEvent;
  27. import org.bukkit.plugin.Plugin;
  28. public class Economy_CurrencyCore extends AbstractEconomy {
  29. private Currency currency;
  30. private static final Logger log = Logger.getLogger("Minecraft");
  31. private final Plugin plugin;
  32. private final String name = "CurrencyCore";
  33. public Economy_CurrencyCore(Plugin plugin) {
  34. this.plugin = plugin;
  35. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  36. // Load Plugin in case it was loaded before
  37. if(currency == null) {
  38. Plugin currencyPlugin = plugin.getServer().getPluginManager().getPlugin("CurrencyCore");
  39. if(currencyPlugin != null && currencyPlugin.getClass().getName().equals("is.currency.Currency")) {
  40. this.currency = (Currency) currencyPlugin;
  41. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  42. }
  43. }
  44. }
  45. public class EconomyServerListener implements Listener {
  46. private Economy_CurrencyCore economy = null;
  47. public EconomyServerListener(Economy_CurrencyCore economy) {
  48. this.economy = economy;
  49. }
  50. @EventHandler(priority = EventPriority.MONITOR)
  51. public void onPluginEnable(PluginEnableEvent event) {
  52. if(this.economy.currency == null) {
  53. Plugin currencyPlugin = event.getPlugin();
  54. if(currencyPlugin.getDescription().getName().equals("CurrencyCore") && currencyPlugin.getClass().getName().equals("is.currency.Currency")) {
  55. this.economy.currency = (Currency) currencyPlugin;
  56. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), this.economy.getName()));
  57. }
  58. }
  59. }
  60. @EventHandler(priority = EventPriority.MONITOR)
  61. public void onPluginDisable(PluginDisableEvent event) {
  62. if (this.economy.currency != null) {
  63. if (event.getPlugin().getDescription().getName().equals("CurrencyCore")) {
  64. this.economy.currency = null;
  65. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), this.economy.getName()));
  66. }
  67. }
  68. }
  69. }
  70. @Override
  71. public boolean isEnabled() {
  72. return currency != null;
  73. }
  74. @Override
  75. public String getName() {
  76. return name;
  77. }
  78. @Override
  79. public String format(double amount) {
  80. return this.currency.getFormatHelper().format(amount);
  81. }
  82. @Override
  83. public String currencyNamePlural() {
  84. return currency.getCurrencyConfig().getCurrencyMajor().get(1);
  85. }
  86. @Override
  87. public String currencyNameSingular() {
  88. return currency.getCurrencyConfig().getCurrencyMajor().get(0);
  89. }
  90. @Override
  91. public double getBalance(String playerName) {
  92. AccountContext account = this.currency.getAccountManager().getAccount(playerName);
  93. if (account == null) {
  94. return 0.0;
  95. }
  96. return account.getBalance();
  97. }
  98. @Override
  99. public boolean has(String playerName, double amount) {
  100. AccountContext account = this.currency.getAccountManager().getAccount(playerName);
  101. if (account == null) {
  102. return false;
  103. } else {
  104. return account.hasBalance(amount);
  105. }
  106. }
  107. @Override
  108. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  109. if (amount < 0) {
  110. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds");
  111. }
  112. AccountContext account = this.currency.getAccountManager().getAccount(playerName);
  113. if (account == null) {
  114. return new EconomyResponse(0.0, 0.0, ResponseType.FAILURE, "That account does not exist");
  115. } else if (!account.hasBalance(amount)) {
  116. return new EconomyResponse(0.0, account.getBalance(), ResponseType.FAILURE, "Insufficient funds");
  117. } else {
  118. account.subtractBalance(amount);
  119. return new EconomyResponse(amount, account.getBalance(), ResponseType.SUCCESS, "");
  120. }
  121. }
  122. @Override
  123. public EconomyResponse depositPlayer(String playerName, double amount) {
  124. if (amount < 0) {
  125. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds");
  126. }
  127. AccountContext account = this.currency.getAccountManager().getAccount(playerName);
  128. if (account == null) {
  129. return new EconomyResponse(0.0, 0.0, ResponseType.FAILURE, "That account does not exist");
  130. }
  131. account.addBalance(amount);
  132. return new EconomyResponse(amount, account.getBalance(), ResponseType.SUCCESS, "");
  133. }
  134. @Override
  135. public EconomyResponse createBank(String name, String player) {
  136. if (this.currency.getAccountManager().hasAccount(name)) {
  137. return new EconomyResponse(0, currency.getAccountManager().getAccount(name).getBalance(), ResponseType.FAILURE, "That account already exists.");
  138. }
  139. this.currency.getAccountManager().createAccount(name);
  140. return new EconomyResponse(0, 0, ResponseType.SUCCESS, "");
  141. }
  142. @Override
  143. public EconomyResponse deleteBank(String name) {
  144. if (this.currency.getAccountManager().hasAccount(name)) {
  145. this.currency.getAccountManager().deleteAccount(name);
  146. return new EconomyResponse(0, 0, ResponseType.SUCCESS, "");
  147. }
  148. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That account does not exist!");
  149. }
  150. @Override
  151. public EconomyResponse bankBalance(String name) {
  152. AccountContext account = this.currency.getAccountManager().getAccount(name);
  153. if (account == null) {
  154. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That account does not exists.");
  155. }
  156. return new EconomyResponse(0, account.getBalance(), ResponseType.SUCCESS, "");
  157. }
  158. @Override
  159. public EconomyResponse bankHas(String name, double amount) {
  160. AccountContext account = this.currency.getAccountManager().getAccount(name);
  161. if (account == null) {
  162. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That account does not exist!");
  163. } else if (!account.hasBalance(amount)) {
  164. return new EconomyResponse(0, account.getBalance(), ResponseType.FAILURE, "That account does not have enough!");
  165. } else {
  166. return new EconomyResponse(0, account.getBalance(), ResponseType.SUCCESS, "");
  167. }
  168. }
  169. @Override
  170. public EconomyResponse bankWithdraw(String name, double amount) {
  171. if (amount < 0) {
  172. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds");
  173. }
  174. AccountContext account = this.currency.getAccountManager().getAccount(name);
  175. if (account == null) {
  176. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That account does not exist!");
  177. } else if (!account.hasBalance(amount)) {
  178. return new EconomyResponse(0, account.getBalance(), ResponseType.FAILURE, "That account does not have enough!");
  179. } else {
  180. account.subtractBalance(amount);
  181. return new EconomyResponse(amount, account.getBalance(), ResponseType.SUCCESS, "");
  182. }
  183. }
  184. @Override
  185. public EconomyResponse bankDeposit(String name, double amount) {
  186. if (amount < 0) {
  187. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds");
  188. }
  189. AccountContext account = this.currency.getAccountManager().getAccount(name);
  190. if (account == null) {
  191. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That account does not exist!");
  192. } else {
  193. account.addBalance(amount);
  194. return new EconomyResponse(amount, account.getBalance(), ResponseType.SUCCESS, "");
  195. }
  196. }
  197. @Override
  198. public EconomyResponse isBankOwner(String name, String playerName) {
  199. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Currency does not support Bank members.");
  200. }
  201. @Override
  202. public EconomyResponse isBankMember(String name, String playerName) {
  203. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Currency does not support Bank members.");
  204. }
  205. @Override
  206. public List<String> getBanks() {
  207. return this.currency.getAccountManager().getAccountList();
  208. }
  209. @Override
  210. public boolean hasBankSupport() {
  211. return true;
  212. }
  213. @Override
  214. public boolean hasAccount(String playerName) {
  215. return this.currency.getAccountManager().getAccount(playerName) != null;
  216. }
  217. @Override
  218. public boolean createPlayerAccount(String playerName) {
  219. if (this.currency.getAccountManager().getAccount(playerName) != null) {
  220. return false;
  221. }
  222. this.currency.getAccountManager().createAccount(playerName);
  223. return true;
  224. }
  225. @Override
  226. public int fractionalDigits() {
  227. return -1;
  228. }
  229. @Override
  230. public boolean hasAccount(String playerName, String worldName) {
  231. return hasAccount(playerName);
  232. }
  233. @Override
  234. public double getBalance(String playerName, String world) {
  235. return getBalance(playerName);
  236. }
  237. @Override
  238. public boolean has(String playerName, String worldName, double amount) {
  239. return has(playerName, amount);
  240. }
  241. @Override
  242. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  243. return withdrawPlayer(playerName, amount);
  244. }
  245. @Override
  246. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  247. return depositPlayer(playerName, amount);
  248. }
  249. @Override
  250. public boolean createPlayerAccount(String playerName, String worldName) {
  251. return createPlayerAccount(playerName);
  252. }
  253. }