PageRenderTime 71ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 316 lines | 259 code | 41 blank | 16 comment | 44 complexity | 1ec435fb8f6ad5effec9e334322bdf39 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 com.flobi.GoldIsMoney2.GoldIsMoney;
  27. public class Economy_GoldIsMoney2 extends AbstractEconomy {
  28. private static final Logger log = Logger.getLogger("Minecraft");
  29. private final String name = "GoldIsMoney";
  30. private Plugin plugin = null;
  31. protected GoldIsMoney economy = null;
  32. public Economy_GoldIsMoney2(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 ec = plugin.getServer().getPluginManager().getPlugin("GoldIsMoney");
  38. if (ec != null && ec.isEnabled() && ec.getClass().getName().equals("com.flobi.GoldIsMoney2.GoldIsMoney")) {
  39. economy = (GoldIsMoney) ec;
  40. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  41. }
  42. }
  43. }
  44. @Override
  45. public boolean isEnabled() {
  46. if (economy == null) {
  47. return false;
  48. } else {
  49. return economy.isEnabled();
  50. }
  51. }
  52. @Override
  53. public String getName() {
  54. return name;
  55. }
  56. @Override
  57. public boolean hasBankSupport() {
  58. return GoldIsMoney.hasBankSupport();
  59. }
  60. @Override
  61. public int fractionalDigits() {
  62. return GoldIsMoney.fractionalDigits();
  63. }
  64. @Override
  65. public String format(double amount) {
  66. return GoldIsMoney.format(amount);
  67. }
  68. @Override
  69. public String currencyNamePlural() {
  70. return GoldIsMoney.currencyNamePlural();
  71. }
  72. @Override
  73. public String currencyNameSingular() {
  74. return GoldIsMoney.currencyNameSingular();
  75. }
  76. @Override
  77. public boolean hasAccount(String playerName) {
  78. return GoldIsMoney.hasAccount(playerName);
  79. }
  80. @Override
  81. public double getBalance(String playerName) {
  82. return GoldIsMoney.getBalance(playerName);
  83. }
  84. @Override
  85. public boolean has(String playerName, double amount) {
  86. return GoldIsMoney.has(playerName, amount);
  87. }
  88. @Override
  89. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  90. if (amount < 0) {
  91. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds!");
  92. }
  93. if (!GoldIsMoney.hasAccount(playerName)) {
  94. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That player does not have an account!");
  95. }
  96. if (!GoldIsMoney.has(playerName, amount)) {
  97. return new EconomyResponse(0, GoldIsMoney.getBalance(playerName), ResponseType.FAILURE, "Insufficient funds");
  98. }
  99. if (!GoldIsMoney.withdrawPlayer(playerName, amount)) {
  100. return new EconomyResponse(0, GoldIsMoney.getBalance(playerName), ResponseType.FAILURE, "Unable to withdraw funds!");
  101. }
  102. return new EconomyResponse(amount, GoldIsMoney.getBalance(playerName), ResponseType.SUCCESS, null);
  103. }
  104. @Override
  105. public EconomyResponse depositPlayer(String playerName, double amount) {
  106. if (amount < 0) {
  107. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds!");
  108. }
  109. if (!GoldIsMoney.hasAccount(playerName)) {
  110. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That player does not have an account!");
  111. }
  112. if (!GoldIsMoney.depositPlayer(playerName, amount)) {
  113. return new EconomyResponse(0, GoldIsMoney.getBalance(playerName), ResponseType.FAILURE, "Unable to deposit funds!");
  114. }
  115. return new EconomyResponse(amount, GoldIsMoney.getBalance(playerName), ResponseType.SUCCESS, null);
  116. }
  117. @Override
  118. public EconomyResponse createBank(String name, String player) {
  119. if (!GoldIsMoney.hasBankSupport()) {
  120. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  121. }
  122. if (!GoldIsMoney.createBank(name, player)) {
  123. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Unable to create bank account.");
  124. }
  125. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.SUCCESS, "");
  126. }
  127. @Override
  128. public EconomyResponse deleteBank(String name) {
  129. if (!GoldIsMoney.hasBankSupport()) {
  130. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  131. }
  132. if (!GoldIsMoney.deleteBank(name)) {
  133. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Unable to remove bank account.");
  134. }
  135. return new EconomyResponse(0, 0, ResponseType.SUCCESS, "");
  136. }
  137. @Override
  138. public EconomyResponse bankBalance(String name) {
  139. if (!GoldIsMoney.hasBankSupport()) {
  140. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  141. }
  142. if (!GoldIsMoney.bankExists(name)) {
  143. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  144. }
  145. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.SUCCESS, "");
  146. }
  147. @Override
  148. public EconomyResponse bankHas(String name, double amount) {
  149. if (!GoldIsMoney.hasBankSupport()) {
  150. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  151. }
  152. if (!GoldIsMoney.bankExists(name)) {
  153. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  154. }
  155. if (GoldIsMoney.bankHas(name, amount)) {
  156. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.FAILURE, "The bank does not have enough money!");
  157. }
  158. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.SUCCESS, "");
  159. }
  160. @Override
  161. public EconomyResponse bankWithdraw(String name, double amount) {
  162. if (!GoldIsMoney.hasBankSupport()) {
  163. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  164. }
  165. if (!GoldIsMoney.bankExists(name)) {
  166. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  167. }
  168. if (!GoldIsMoney.bankHas(name, amount)) {
  169. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.FAILURE, "The bank does not have enough money!");
  170. }
  171. if (!GoldIsMoney.bankWithdraw(name, amount)) {
  172. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.FAILURE, "Unable to withdraw from that bank account!");
  173. }
  174. return new EconomyResponse(amount, GoldIsMoney.bankBalance(name), ResponseType.SUCCESS, "");
  175. }
  176. @Override
  177. public EconomyResponse bankDeposit(String name, double amount) {
  178. if (!GoldIsMoney.hasBankSupport()) {
  179. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  180. }
  181. if (!GoldIsMoney.bankExists(name)) {
  182. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  183. }
  184. if (!GoldIsMoney.bankDeposit(name, amount)) {
  185. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.FAILURE, "Unable to deposit to that bank account!");
  186. }
  187. return new EconomyResponse(amount, GoldIsMoney.bankBalance(name), ResponseType.SUCCESS, "");
  188. }
  189. @Override
  190. public EconomyResponse isBankOwner(String name, String playerName) {
  191. if (!GoldIsMoney.hasBankSupport()) {
  192. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  193. }
  194. if (!GoldIsMoney.bankExists(name)) {
  195. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  196. }
  197. if (!GoldIsMoney.isBankOwner(name, playerName)) {
  198. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That player does not own that bank!");
  199. }
  200. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.SUCCESS, "");
  201. }
  202. @Override
  203. public EconomyResponse isBankMember(String name, String playerName) {
  204. if (!GoldIsMoney.hasBankSupport()) {
  205. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "GoldIsMoney bank support is disabled!");
  206. }
  207. if (!GoldIsMoney.bankExists(name)) {
  208. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  209. }
  210. if (!GoldIsMoney.isBankMember(name, playerName)) {
  211. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That player is not a member of that bank!");
  212. }
  213. return new EconomyResponse(0, GoldIsMoney.bankBalance(name), ResponseType.SUCCESS, "");
  214. }
  215. @Override
  216. public List<String> getBanks() {
  217. return GoldIsMoney.getBanks();
  218. }
  219. @Override
  220. public boolean createPlayerAccount(String playerName) {
  221. return GoldIsMoney.createPlayerAccount(playerName);
  222. }
  223. public class EconomyServerListener implements Listener {
  224. Economy_GoldIsMoney2 economy = null;
  225. public EconomyServerListener(Economy_GoldIsMoney2 economy_GoldIsMoney2) {
  226. this.economy = economy_GoldIsMoney2;
  227. }
  228. @EventHandler(priority = EventPriority.MONITOR)
  229. public void onPluginEnable(PluginEnableEvent event) {
  230. if (economy.economy == null) {
  231. Plugin ec = event.getPlugin();
  232. if (ec.getClass().getName().equals("com.flobi.GoldIsMoney2.GoldIsMoney")) {
  233. economy.economy = (GoldIsMoney) ec;
  234. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  235. }
  236. }
  237. }
  238. @EventHandler(priority = EventPriority.MONITOR)
  239. public void onPluginDisable(PluginDisableEvent event) {
  240. if (economy.economy != null) {
  241. if (event.getPlugin().getDescription().getName().equals("GoldIsMoney")) {
  242. economy.economy = null;
  243. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  244. }
  245. }
  246. }
  247. }
  248. @Override
  249. public boolean hasAccount(String playerName, String worldName) {
  250. return hasAccount(playerName);
  251. }
  252. @Override
  253. public double getBalance(String playerName, String world) {
  254. return getBalance(playerName);
  255. }
  256. @Override
  257. public boolean has(String playerName, String worldName, double amount) {
  258. return has(playerName, amount);
  259. }
  260. @Override
  261. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  262. return withdrawPlayer(playerName, amount);
  263. }
  264. @Override
  265. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  266. return depositPlayer(playerName, amount);
  267. }
  268. @Override
  269. public boolean createPlayerAccount(String playerName, String worldName) {
  270. return createPlayerAccount(playerName);
  271. }
  272. }