PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

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