PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 369 lines | 280 code | 73 blank | 16 comment | 45 complexity | bacd2534e341d931bc9772899b954942 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 com.gmail.bleedobsidian.miconomy.Main;
  15. import com.gmail.bleedobsidian.miconomy.MiConomy;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.logging.Logger;
  19. import net.milkbowl.vault.economy.AbstractEconomy;
  20. import net.milkbowl.vault.economy.EconomyResponse;
  21. import org.bukkit.Bukkit;
  22. import org.bukkit.OfflinePlayer;
  23. import org.bukkit.World;
  24. import org.bukkit.event.EventHandler;
  25. import org.bukkit.event.EventPriority;
  26. import org.bukkit.event.Listener;
  27. import org.bukkit.event.server.PluginDisableEvent;
  28. import org.bukkit.event.server.PluginEnableEvent;
  29. import org.bukkit.plugin.Plugin;
  30. public class Economy_MiConomy extends AbstractEconomy {
  31. private static final Logger log = Logger.getLogger("Minecraft");
  32. private final String name = "MiConomy";
  33. private Plugin plugin;
  34. private MiConomy economy;
  35. private Main miConomy;
  36. public Economy_MiConomy(Plugin plugin) {
  37. this.plugin = plugin;
  38. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  39. // Load Plugin in case it was loaded before
  40. if (miConomy == null) {
  41. Plugin miConomyPlugin = plugin.getServer().getPluginManager().getPlugin("MiConomy");
  42. if (miConomy != null) {
  43. miConomy = (Main) miConomyPlugin;
  44. economy = miConomy.getInstance();
  45. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  46. }
  47. }
  48. }
  49. @Override
  50. public boolean isEnabled() {
  51. if(miConomy == null) {
  52. return false;
  53. } else {
  54. return miConomy.isEnabled();
  55. }
  56. }
  57. @Override
  58. public String getName() {
  59. return name;
  60. }
  61. @Override
  62. public boolean hasBankSupport() {
  63. return true;
  64. }
  65. @Override
  66. public int fractionalDigits() {
  67. return 2;
  68. }
  69. @Override
  70. public String format(double amount) {
  71. return economy.getFormattedValue(amount);
  72. }
  73. @Override
  74. public String currencyNamePlural() {
  75. return miConomy.getPluginConfig().MoneyNamePlural;
  76. }
  77. @Override
  78. public String currencyNameSingular() {
  79. return miConomy.getPluginConfig().MoneyName;
  80. }
  81. @Override
  82. public boolean hasAccount(String playerName) {
  83. List<World> worlds = plugin.getServer().getWorlds();
  84. return hasAccount(playerName, worlds.get(0).getName());
  85. }
  86. @Override
  87. public boolean hasAccount(String playerName, String worldName) {
  88. OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
  89. World world = plugin.getServer().getWorld(worldName);
  90. return economy.isAccountCreated(player, world);
  91. }
  92. @Override
  93. public double getBalance(String playerName) {
  94. List<World> worlds = plugin.getServer().getWorlds();
  95. return getBalance(playerName, worlds.get(0).getName());
  96. }
  97. @Override
  98. public double getBalance(String playerName, String worldName) {
  99. OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
  100. World world = plugin.getServer().getWorld(worldName);
  101. return economy.getAccountBalance(player, world);
  102. }
  103. @Override
  104. public boolean has(String playerName, double amount) {
  105. List<World> worlds = plugin.getServer().getWorlds();
  106. return has(playerName, worlds.get(0).getName(), amount);
  107. }
  108. @Override
  109. public boolean has(String playerName, String worldName, double amount) {
  110. OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
  111. World world = plugin.getServer().getWorld(worldName);
  112. double playerBalance = economy.getAccountBalance(player, world);
  113. if(playerBalance >= amount) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. @Override
  120. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  121. List<World> worlds = plugin.getServer().getWorlds();
  122. return withdrawPlayer(playerName, worlds.get(0).getName(), amount);
  123. }
  124. @Override
  125. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  126. OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
  127. World world = plugin.getServer().getWorld(worldName);
  128. double balance = economy.getAccountBalance(player, world);
  129. if(getBalance(playerName, worldName) < amount) {
  130. return new EconomyResponse(0, balance, EconomyResponse.ResponseType.FAILURE, "Insufficient funds");
  131. } else {
  132. if(economy.removeAccountBalance(player, amount, world)) {
  133. balance = economy.getAccountBalance(player, world);
  134. return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.SUCCESS, "");
  135. } else {
  136. return new EconomyResponse(0, balance, EconomyResponse.ResponseType.FAILURE, "Failed to remove funds from account");
  137. }
  138. }
  139. }
  140. @Override
  141. public EconomyResponse depositPlayer(String playerName, double amount) {
  142. List<World> worlds = plugin.getServer().getWorlds();
  143. return depositPlayer(playerName, worlds.get(0).getName(), amount);
  144. }
  145. @Override
  146. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  147. OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
  148. World world = plugin.getServer().getWorld(worldName);
  149. double balance = economy.getAccountBalance(player, world);
  150. if(economy.addAccountBalance(player, amount, world)) {
  151. balance = economy.getAccountBalance(player, world);
  152. return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.SUCCESS, "");
  153. } else {
  154. return new EconomyResponse(0, balance, EconomyResponse.ResponseType.FAILURE, "Failed to add funds to account");
  155. }
  156. }
  157. @Override
  158. public EconomyResponse createBank(String name, String player) {
  159. OfflinePlayer owner = plugin.getServer().getOfflinePlayer(player);
  160. ArrayList<OfflinePlayer> owners = new ArrayList<OfflinePlayer>();
  161. owners.add(owner);
  162. if(!economy.isBankCreated(name)) {
  163. economy.createBank(name, owners, new ArrayList<String>(), false);
  164. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.SUCCESS, "");
  165. } else {
  166. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "A bank with this name already exists");
  167. }
  168. }
  169. @Override
  170. public EconomyResponse deleteBank(String name) {
  171. if(economy.isBankCreated(name)) {
  172. economy.deleteBank(name);
  173. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.SUCCESS, "");
  174. } else {
  175. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Bank doesn't exist");
  176. }
  177. }
  178. @Override
  179. public EconomyResponse bankBalance(String name) {
  180. if(economy.isBankCreated(name)) {
  181. double balance = economy.getBankBalance(name);
  182. return new EconomyResponse(0, balance, EconomyResponse.ResponseType.SUCCESS, "");
  183. } else {
  184. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Bank doesn't exist");
  185. }
  186. }
  187. @Override
  188. public EconomyResponse bankHas(String name, double amount) {
  189. if(economy.isBankCreated(name)) {
  190. double balance = economy.getBankBalance(name);
  191. if(balance >= amount) {
  192. return new EconomyResponse(0, balance, EconomyResponse.ResponseType.SUCCESS, "");
  193. } else {
  194. return new EconomyResponse(0, balance, EconomyResponse.ResponseType.FAILURE, "The bank does not have enough money!");
  195. }
  196. } else {
  197. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Bank doesn't exist");
  198. }
  199. }
  200. @Override
  201. public EconomyResponse bankWithdraw(String name, double amount) {
  202. if(economy.isBankCreated(name)) {
  203. economy.removeBankBalance(name, amount);
  204. double balance = economy.getBankBalance(name);
  205. return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.SUCCESS, "");
  206. } else {
  207. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Bank doesn't exist");
  208. }
  209. }
  210. @Override
  211. public EconomyResponse bankDeposit(String name, double amount) {
  212. if(economy.isBankCreated(name)) {
  213. economy.addBankBalance(name, amount);
  214. double balance = economy.getBankBalance(name);
  215. return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.SUCCESS, "");
  216. } else {
  217. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Bank doesn't exist");
  218. }
  219. }
  220. @Override
  221. public EconomyResponse isBankOwner(String name, String playerName) {
  222. OfflinePlayer owner = plugin.getServer().getOfflinePlayer(playerName);
  223. if(economy.isBankCreated(name)) {
  224. if(economy.isPlayerBankOwner(name, owner)) {
  225. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.SUCCESS, "");
  226. } else {
  227. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "The player is not a bank owner");
  228. }
  229. } else {
  230. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Bank doesn't exist");
  231. }
  232. }
  233. @Override
  234. public EconomyResponse isBankMember(String name, String playerName) {
  235. OfflinePlayer owner = plugin.getServer().getOfflinePlayer(playerName);
  236. if(economy.isBankCreated(name)) {
  237. if(economy.isPlayerBankMember(name, owner)) {
  238. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.SUCCESS, "");
  239. } else {
  240. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "The player is not a bank member");
  241. }
  242. } else {
  243. return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Bank doesn't exist");
  244. }
  245. }
  246. @Override
  247. public List<String> getBanks() {
  248. return economy.getBanks();
  249. }
  250. @Override
  251. public boolean createPlayerAccount(String playerName) {
  252. List<World> worlds = plugin.getServer().getWorlds();
  253. return createPlayerAccount(playerName, worlds.get(0).getName());
  254. }
  255. @Override
  256. public boolean createPlayerAccount(String playerName, String worldName) {
  257. OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
  258. World world = plugin.getServer().getWorld(worldName);
  259. if(!economy.isAccountCreated(player, world)) {
  260. economy.createAccount(player, 0, world);
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. }
  266. public class EconomyServerListener implements Listener {
  267. Economy_MiConomy economy = null;
  268. public EconomyServerListener(Economy_MiConomy economy) {
  269. this.economy = economy;
  270. }
  271. @EventHandler(priority = EventPriority.MONITOR)
  272. public void onPluginEnable(PluginEnableEvent event) {
  273. if (economy.economy == null) {
  274. Plugin miConomyPlugin = event.getPlugin();
  275. if (miConomyPlugin.getDescription().getName().equals("MiConomy")) {
  276. economy.miConomy = (Main) miConomyPlugin;
  277. economy.economy = miConomy.getInstance();
  278. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  279. }
  280. }
  281. }
  282. @EventHandler(priority = EventPriority.MONITOR)
  283. public void onPluginDisable(PluginDisableEvent event) {
  284. if (economy.economy != null) {
  285. if (event.getPlugin().getDescription().getName().equals("MiConomy")) {
  286. economy.miConomy = null;
  287. economy.economy = null;
  288. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  289. }
  290. }
  291. }
  292. }
  293. }