PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 272 lines | 212 code | 44 blank | 16 comment | 16 complexity | 679cfdf08c7bee06ee53714c2a50524d 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.mjolnir.mineconomy.MineConomy;
  18. import me.mjolnir.mineconomy.exceptions.AccountNameConflictException;
  19. import me.mjolnir.mineconomy.exceptions.NoAccountException;
  20. import me.mjolnir.mineconomy.internal.MCCom;
  21. import me.mjolnir.mineconomy.internal.util.MCFormat;
  22. import net.milkbowl.vault.economy.AbstractEconomy;
  23. import net.milkbowl.vault.economy.EconomyResponse;
  24. import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
  25. import org.bukkit.Bukkit;
  26. import org.bukkit.event.EventHandler;
  27. import org.bukkit.event.EventPriority;
  28. import org.bukkit.event.Listener;
  29. import org.bukkit.event.server.PluginDisableEvent;
  30. import org.bukkit.event.server.PluginEnableEvent;
  31. import org.bukkit.plugin.Plugin;
  32. public class Economy_MineConomy extends AbstractEconomy {
  33. private static final Logger log = Logger.getLogger("Minecraft");
  34. private final String name = "MineConomy";
  35. private Plugin plugin = null;
  36. private MineConomy econ = null;
  37. public Economy_MineConomy(Plugin plugin) {
  38. this.plugin = plugin;
  39. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  40. // Load Plugin in case it was loaded before
  41. if (econ == null) {
  42. Plugin econ = plugin.getServer().getPluginManager().getPlugin("MineConomy");
  43. if (econ != null && econ.isEnabled()) {
  44. this.econ = (MineConomy) econ;
  45. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  46. }
  47. }
  48. }
  49. public class EconomyServerListener implements Listener {
  50. Economy_MineConomy economy = null;
  51. public EconomyServerListener(Economy_MineConomy economy) {
  52. this.economy = economy;
  53. }
  54. @EventHandler(priority = EventPriority.MONITOR)
  55. public void onPluginEnable(PluginEnableEvent event) {
  56. if (economy.econ == null) {
  57. Plugin eco = event.getPlugin();
  58. if (eco.getDescription().getName().equals("MineConomy")) {
  59. economy.econ = (MineConomy) eco;
  60. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  61. }
  62. }
  63. }
  64. @EventHandler(priority = EventPriority.MONITOR)
  65. public void onPluginDisable(PluginDisableEvent event) {
  66. if (economy.econ != null) {
  67. if (event.getPlugin().getDescription().getName().equals("MineConomy")) {
  68. economy.econ = null;
  69. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  70. }
  71. }
  72. }
  73. }
  74. public boolean isEnabled() {
  75. return econ != null;
  76. }
  77. public String getName() {
  78. return "MineConomy";
  79. }
  80. public String format(double amount) {
  81. return MCFormat.format(amount);
  82. }
  83. public String currencyNameSingular() {
  84. return MCCom.getDefaultCurrency();
  85. }
  86. public String currencyNamePlural() {
  87. return MCCom.getDefaultCurrency();
  88. }
  89. public double getBalance(String playerName) {
  90. try
  91. {
  92. return MCCom.getExternalBalance(playerName);
  93. }
  94. catch (NoAccountException e)
  95. {
  96. MCCom.create(playerName);
  97. return MCCom.getExternalBalance(playerName);
  98. }
  99. }
  100. @Override
  101. public boolean has(String playerName, double amount) {
  102. try {
  103. return MCCom.canExternalAfford(playerName, amount);
  104. } catch(NoAccountException e) {
  105. MCCom.create(playerName);
  106. return MCCom.canExternalAfford(playerName, amount);
  107. }
  108. }
  109. @Override
  110. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  111. double balance;
  112. try {
  113. balance = MCCom.getExternalBalance(playerName);
  114. } catch (NoAccountException e) {
  115. MCCom.create(playerName);
  116. balance = MCCom.getExternalBalance(playerName);
  117. }
  118. if(amount < 0.0D) {
  119. return new EconomyResponse(0.0D, balance, ResponseType.FAILURE, "Cannot withdraw negative funds");
  120. }
  121. if(balance >= amount) {
  122. double finalBalance = balance - amount;
  123. MCCom.setExternalBalance(playerName, finalBalance);
  124. return new EconomyResponse(amount, finalBalance, ResponseType.SUCCESS, null);
  125. } else {
  126. return new EconomyResponse(0.0D, balance, ResponseType.FAILURE, "Insufficient funds");
  127. }
  128. }
  129. @Override
  130. public EconomyResponse depositPlayer(String playerName, double amount) {
  131. double balance;
  132. try {
  133. balance = MCCom.getExternalBalance(playerName);
  134. } catch (NoAccountException e) {
  135. MCCom.create(playerName);
  136. balance = MCCom.getExternalBalance(playerName);
  137. }
  138. if(amount < 0.0D) {
  139. return new EconomyResponse(0.0D, 0.0, ResponseType.FAILURE, "Cannot deposit negative funds");
  140. }
  141. balance += amount;
  142. MCCom.setExternalBalance(playerName, balance);
  143. return new EconomyResponse(amount, balance, ResponseType.SUCCESS, null);
  144. }
  145. @Override
  146. public EconomyResponse createBank(String name, String player) {
  147. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  148. }
  149. @Override
  150. public EconomyResponse deleteBank(String name) {
  151. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  152. }
  153. @Override
  154. public EconomyResponse bankHas(String name, double amount) {
  155. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  156. }
  157. @Override
  158. public EconomyResponse bankWithdraw(String name, double amount) {
  159. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  160. }
  161. @Override
  162. public EconomyResponse bankDeposit(String name, double amount) {
  163. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  164. }
  165. @Override
  166. public EconomyResponse isBankOwner(String name, String playerName) {
  167. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  168. }
  169. @Override
  170. public EconomyResponse isBankMember(String name, String playerName) {
  171. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  172. }
  173. @Override
  174. public EconomyResponse bankBalance(String name) {
  175. return new EconomyResponse(0.0D, 0.0D, ResponseType.NOT_IMPLEMENTED, "MineConomy does not support bank accounts!");
  176. }
  177. @Override
  178. public List<String> getBanks() {
  179. return new ArrayList<String>();
  180. }
  181. @Override
  182. public boolean hasBankSupport() {
  183. return false;
  184. }
  185. @Override
  186. public boolean hasAccount(String playerName) {
  187. return MCCom.exists(playerName);
  188. }
  189. public boolean createPlayerAccount(String playerName) {
  190. try {
  191. MCCom.create(playerName);
  192. return true;
  193. } catch(AccountNameConflictException e) {
  194. return false;
  195. }
  196. }
  197. @Override
  198. public int fractionalDigits() {
  199. return 2;
  200. }
  201. @Override
  202. public boolean hasAccount(String playerName, String worldName) {
  203. return hasAccount(playerName);
  204. }
  205. @Override
  206. public double getBalance(String playerName, String world) {
  207. return getBalance(playerName);
  208. }
  209. @Override
  210. public boolean has(String playerName, String worldName, double amount) {
  211. return has(playerName, amount);
  212. }
  213. @Override
  214. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  215. return withdrawPlayer(playerName, amount);
  216. }
  217. @Override
  218. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  219. return depositPlayer(playerName, amount);
  220. }
  221. @Override
  222. public boolean createPlayerAccount(String playerName, String worldName) {
  223. return createPlayerAccount(playerName);
  224. }
  225. }