PageRenderTime 42ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

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