PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 253 lines | 197 code | 40 blank | 16 comment | 23 complexity | 9c6a522c9e6fab44f11cbfbb6c9dd7dc 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.ethan.eWallet.ECO;
  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_eWallet extends AbstractEconomy {
  29. private static final Logger log = Logger.getLogger("Minecraft");
  30. private final String name = "eWallet";
  31. private Plugin plugin = null;
  32. private ECO econ = null;
  33. public Economy_eWallet(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 (econ == null) {
  38. Plugin econ = plugin.getServer().getPluginManager().getPlugin("eWallet");
  39. if (econ != null && econ.isEnabled()) {
  40. this.econ = (ECO) econ;
  41. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  42. }
  43. }
  44. }
  45. public class EconomyServerListener implements Listener {
  46. Economy_eWallet economy = null;
  47. public EconomyServerListener(Economy_eWallet economy) {
  48. this.economy = economy;
  49. }
  50. @EventHandler(priority = EventPriority.MONITOR)
  51. public void onPluginEnable(PluginEnableEvent event) {
  52. if (economy.econ == null) {
  53. Plugin eco = event.getPlugin();
  54. if (eco.getDescription().getName().equals("eWallet")) {
  55. economy.econ = (ECO) eco;
  56. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  57. }
  58. }
  59. }
  60. @EventHandler(priority = EventPriority.MONITOR)
  61. public void onPluginDisable(PluginDisableEvent event) {
  62. if (economy.econ != null) {
  63. if (event.getPlugin().getDescription().getName().equals("eWallet")) {
  64. economy.econ = null;
  65. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  66. }
  67. }
  68. }
  69. }
  70. @Override
  71. public boolean isEnabled() {
  72. return this.econ != null;
  73. }
  74. @Override
  75. public String getName() {
  76. return name;
  77. }
  78. @Override
  79. public String format(double amount) {
  80. amount = Math.ceil(amount);
  81. if (amount == 1) {
  82. return String.format("%d %s", (int)amount, econ.singularCurrency);
  83. } else {
  84. return String.format("%d %s", (int)amount, econ.pluralCurrency);
  85. }
  86. }
  87. @Override
  88. public String currencyNameSingular() {
  89. return econ.singularCurrency;
  90. }
  91. @Override
  92. public String currencyNamePlural() {
  93. return econ.pluralCurrency;
  94. }
  95. @Override
  96. public double getBalance(String playerName) {
  97. Integer i = econ.getMoney(playerName);
  98. return i == null ? 0 : i;
  99. }
  100. @Override
  101. public boolean has(String playerName, double amount) {
  102. return getBalance(playerName) >= Math.ceil(amount);
  103. }
  104. @Override
  105. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  106. double balance = getBalance(playerName);
  107. amount = Math.ceil(amount);
  108. if (amount < 0) {
  109. return new EconomyResponse(0, balance, ResponseType.FAILURE, "Cannot withdraw negative funds");
  110. } else if (balance >= amount) {
  111. double finalBalance = balance - amount;
  112. econ.takeMoney(playerName, (int) amount);
  113. return new EconomyResponse(amount, finalBalance, ResponseType.SUCCESS, null);
  114. } else {
  115. return new EconomyResponse(0, balance, ResponseType.FAILURE, "Insufficient funds");
  116. }
  117. }
  118. @Override
  119. public EconomyResponse depositPlayer(String playerName, double amount) {
  120. double balance = getBalance(playerName);
  121. amount = Math.ceil(amount);
  122. if (amount < 0) {
  123. return new EconomyResponse(0, balance, ResponseType.FAILURE, "Cannot deposit negative funds");
  124. } else {
  125. balance += amount;
  126. econ.giveMoney(playerName, (int) amount);
  127. return new EconomyResponse(amount, balance, ResponseType.SUCCESS, null);
  128. }
  129. }
  130. @Override
  131. public EconomyResponse createBank(String name, String player) {
  132. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  133. }
  134. @Override
  135. public EconomyResponse deleteBank(String name) {
  136. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  137. }
  138. @Override
  139. public EconomyResponse bankHas(String name, double amount) {
  140. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  141. }
  142. @Override
  143. public EconomyResponse bankWithdraw(String name, double amount) {
  144. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  145. }
  146. @Override
  147. public EconomyResponse bankDeposit(String name, double amount) {
  148. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  149. }
  150. @Override
  151. public EconomyResponse isBankOwner(String name, String playerName) {
  152. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  153. }
  154. @Override
  155. public EconomyResponse isBankMember(String name, String playerName) {
  156. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  157. }
  158. @Override
  159. public EconomyResponse bankBalance(String name) {
  160. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "eWallet does not support bank accounts!");
  161. }
  162. @Override
  163. public List<String> getBanks() {
  164. return new ArrayList<String>();
  165. }
  166. @Override
  167. public boolean hasBankSupport() {
  168. return false;
  169. }
  170. @Override
  171. public boolean hasAccount(String playerName) {
  172. return econ.hasAccount(playerName);
  173. }
  174. @Override
  175. public boolean createPlayerAccount(String playerName) {
  176. if (hasAccount(playerName)) {
  177. return false;
  178. }
  179. econ.createAccount(playerName, 0);
  180. return true;
  181. }
  182. @Override
  183. public int fractionalDigits() {
  184. return 0;
  185. }
  186. @Override
  187. public boolean hasAccount(String playerName, String worldName) {
  188. return hasAccount(playerName);
  189. }
  190. @Override
  191. public double getBalance(String playerName, String world) {
  192. return getBalance(playerName);
  193. }
  194. @Override
  195. public boolean has(String playerName, String worldName, double amount) {
  196. return has(playerName, amount);
  197. }
  198. @Override
  199. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  200. return withdrawPlayer(playerName, amount);
  201. }
  202. @Override
  203. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  204. return depositPlayer(playerName, amount);
  205. }
  206. @Override
  207. public boolean createPlayerAccount(String playerName, String worldName) {
  208. return createPlayerAccount(playerName);
  209. }
  210. }