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

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

https://gitlab.com/MineYourMind/Vault
Java | 327 lines | 261 code | 49 blank | 17 comment | 42 complexity | 9104697a653687a06295690cff7d0773 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.Iterator;
  16. import java.util.List;
  17. import java.util.logging.Logger;
  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. import com.greatmancode.craftconomy3.Cause;
  29. import com.greatmancode.craftconomy3.Common;
  30. import com.greatmancode.craftconomy3.account.Account;
  31. import com.greatmancode.craftconomy3.database.tables.AccountTable;
  32. import com.greatmancode.craftconomy3.groups.WorldGroupsManager;
  33. import com.greatmancode.craftconomy3.tools.interfaces.BukkitLoader;
  34. public class Economy_Craftconomy3 extends AbstractEconomy {
  35. private static final Logger log = Logger.getLogger("Minecraft");
  36. private final String name = "Craftconomy3";
  37. private Plugin plugin = null;
  38. protected BukkitLoader economy = null;
  39. public Economy_Craftconomy3(Plugin plugin) {
  40. this.plugin = plugin;
  41. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  42. // Load Plugin in case it was loaded before
  43. if (economy == null) {
  44. Plugin ec = plugin.getServer().getPluginManager().getPlugin("Craftconomy3");
  45. if (ec != null && ec.isEnabled() && ec.getClass().getName().equals("com.greatmancode.craftconomy3.BukkitLoader")) {
  46. economy = (BukkitLoader) ec;
  47. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  48. }
  49. }
  50. }
  51. public class EconomyServerListener implements Listener {
  52. Economy_Craftconomy3 economy = null;
  53. public EconomyServerListener(Economy_Craftconomy3 economy) {
  54. this.economy = economy;
  55. }
  56. @EventHandler(priority = EventPriority.MONITOR)
  57. public void onPluginEnable(PluginEnableEvent event) {
  58. if (economy.economy == null) {
  59. Plugin ec = event.getPlugin();
  60. if (ec.getDescription().getName().equals("Craftconomy3") && ec.getClass().getName().equals("com.greatmancode.craftconomy3.tools.interfaces.BukkitLoader")) {
  61. economy.economy = (BukkitLoader) ec;
  62. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  63. }
  64. }
  65. }
  66. @EventHandler(priority = EventPriority.MONITOR)
  67. public void onPluginDisable(PluginDisableEvent event) {
  68. if (economy.economy != null) {
  69. if (event.getPlugin().getDescription().getName().equals("Craftconomy3")) {
  70. economy.economy = null;
  71. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  72. }
  73. }
  74. }
  75. }
  76. @Override
  77. public boolean isEnabled() {
  78. if (economy == null) {
  79. return false;
  80. } else {
  81. return economy.isEnabled();
  82. }
  83. }
  84. @Override
  85. public String getName() {
  86. return name;
  87. }
  88. @Override
  89. public String format(double amount) {
  90. return Common.getInstance().format(null, Common.getInstance().getCurrencyManager().getDefaultCurrency(), amount);
  91. }
  92. @Override
  93. public String currencyNameSingular() {
  94. return Common.getInstance().getCurrencyManager().getDefaultCurrency().getName();
  95. }
  96. @Override
  97. public String currencyNamePlural() {
  98. return Common.getInstance().getCurrencyManager().getDefaultCurrency().getPlural();
  99. }
  100. @Override
  101. public double getBalance(String playerName) {
  102. return getBalance(playerName, WorldGroupsManager.DEFAULT_GROUP_NAME);
  103. }
  104. @Override
  105. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  106. return withdrawPlayer(playerName, WorldGroupsManager.DEFAULT_GROUP_NAME, amount);
  107. }
  108. @Override
  109. public EconomyResponse depositPlayer(String playerName, double amount) {
  110. return depositPlayer(playerName, WorldGroupsManager.DEFAULT_GROUP_NAME, amount);
  111. }
  112. @Override
  113. public boolean has(String playerName, double amount) {
  114. return has(playerName, WorldGroupsManager.DEFAULT_GROUP_NAME, amount);
  115. }
  116. @Override
  117. public EconomyResponse createBank(String name, String player) {
  118. boolean success = false;
  119. if (!Common.getInstance().getAccountManager().exist(Account.BANK_PREFIX + name)) {
  120. Common.getInstance().getAccountManager().getAccount(Account.BANK_PREFIX + name).getAccountACL().set(player, true, true, true, true, true);
  121. success = true;
  122. }
  123. if (success) {
  124. return new EconomyResponse(0, 0, ResponseType.SUCCESS, "");
  125. }
  126. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Unable to create that bank account. It already exists!");
  127. }
  128. @Override
  129. public EconomyResponse deleteBank(String name) {
  130. boolean success = Common.getInstance().getAccountManager().delete(Account.BANK_PREFIX + name);
  131. if (success) {
  132. return new EconomyResponse(0, 0, ResponseType.SUCCESS, "");
  133. }
  134. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Unable to delete that bank account.");
  135. }
  136. @Override
  137. public EconomyResponse bankHas(String name, double amount) {
  138. if (Common.getInstance().getAccountManager().exist(Account.BANK_PREFIX + name)) {
  139. Account account = Common.getInstance().getAccountManager().getAccount(Account.BANK_PREFIX + name);
  140. if (account.hasEnough(amount, Common.getInstance().getServerCaller().getDefaultWorld(), Common.getInstance().getCurrencyManager().getDefaultCurrency().getName())) {
  141. return new EconomyResponse(0, bankBalance(Account.BANK_PREFIX + name).balance, ResponseType.SUCCESS, "");
  142. } else {
  143. return new EconomyResponse(0, bankBalance(Account.BANK_PREFIX + name).balance, ResponseType.FAILURE, "The bank does not have enough money!");
  144. }
  145. }
  146. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  147. }
  148. @Override
  149. public EconomyResponse bankWithdraw(String name, double amount) {
  150. if (amount < 0) {
  151. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds");
  152. }
  153. EconomyResponse er = bankHas(name, amount);
  154. if (!er.transactionSuccess()) {
  155. return er;
  156. } else {
  157. if (Common.getInstance().getAccountManager().exist(Account.BANK_PREFIX + name)) {
  158. return new EconomyResponse(0, withdrawPlayer(Account.BANK_PREFIX + name, amount).balance, ResponseType.SUCCESS, "");
  159. }
  160. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  161. }
  162. }
  163. @Override
  164. public EconomyResponse bankDeposit(String name, double amount) {
  165. if (amount < 0) {
  166. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds");
  167. }
  168. if (Common.getInstance().getAccountManager().exist(Account.BANK_PREFIX + name)) {
  169. return new EconomyResponse(0, depositPlayer(Account.BANK_PREFIX + name, amount).balance, ResponseType.SUCCESS, "");
  170. }
  171. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  172. }
  173. @Override
  174. public EconomyResponse isBankOwner(String name, String playerName) {
  175. if (Common.getInstance().getAccountManager().exist(Account.BANK_PREFIX + name)) {
  176. if (Common.getInstance().getAccountManager().getAccount(Account.BANK_PREFIX + name).getAccountACL().isOwner(playerName)) {
  177. return new EconomyResponse(0, bankBalance(Account.BANK_PREFIX + name).balance, ResponseType.SUCCESS, "");
  178. }
  179. return new EconomyResponse(0, 0, ResponseType.FAILURE, "This player is not the owner of the bank!");
  180. }
  181. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  182. }
  183. @Override
  184. public EconomyResponse isBankMember(String name, String playerName) {
  185. // Basicly here if the user have access to deposit & withdraw he's a member
  186. EconomyResponse er = isBankOwner(name, playerName);
  187. if (er.transactionSuccess()) {
  188. return er;
  189. } else {
  190. if (Common.getInstance().getAccountManager().exist(Account.BANK_PREFIX + name)) {
  191. Account account = Common.getInstance().getAccountManager().getAccount(Account.BANK_PREFIX + name);
  192. if (account.getAccountACL().canDeposit(playerName) && account.getAccountACL().canWithdraw(playerName)) {
  193. return new EconomyResponse(0, bankBalance(name).balance, ResponseType.SUCCESS, "");
  194. }
  195. }
  196. return new EconomyResponse(0, 0, ResponseType.FAILURE, "This player is not a member of the bank!");
  197. }
  198. }
  199. @Override
  200. public EconomyResponse bankBalance(String name) {
  201. if (Common.getInstance().getAccountManager().exist(Account.BANK_PREFIX + name)) {
  202. return new EconomyResponse(0, getBalance(Account.BANK_PREFIX + name), ResponseType.SUCCESS, "");
  203. }
  204. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
  205. }
  206. @Override
  207. public List<String> getBanks() {
  208. List<AccountTable> accountList = Common.getInstance().getDatabaseManager().getDatabase().select(AccountTable.class).where().contains("name", Account.BANK_PREFIX).execute().find();
  209. ArrayList<String> list = new ArrayList<String>();
  210. Iterator<AccountTable> iterator = accountList.iterator();
  211. while (iterator.hasNext()) {
  212. list.add(iterator.next().getName().replaceFirst(Account.BANK_PREFIX, ""));
  213. }
  214. return list;
  215. }
  216. @Override
  217. public boolean hasBankSupport() {
  218. return true;
  219. }
  220. @Override
  221. public boolean hasAccount(String playerName) {
  222. return Common.getInstance().getAccountManager().exist(playerName);
  223. }
  224. @Override
  225. public boolean createPlayerAccount(String playerName) {
  226. if (Common.getInstance().getAccountManager().exist(playerName)) {
  227. return false;
  228. }
  229. Common.getInstance().getAccountManager().getAccount(playerName);
  230. return true;
  231. }
  232. @Override
  233. public int fractionalDigits() {
  234. return -1;
  235. }
  236. @Override
  237. public boolean hasAccount(String playerName, String worldName) {
  238. return hasAccount(playerName);
  239. }
  240. @Override
  241. public double getBalance(String playerName, String world) {
  242. return Common.getInstance().getAccountManager().getAccount(playerName).getBalance(world, Common.getInstance().getCurrencyManager().getDefaultCurrency().getName());
  243. }
  244. @Override
  245. public boolean has(String playerName, String worldName, double amount) {
  246. return Common.getInstance().getAccountManager().getAccount(playerName).hasEnough(amount, worldName, Common.getInstance().getCurrencyManager().getDefaultCurrency().getName());
  247. }
  248. @Override
  249. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  250. if (amount < 0) {
  251. return new EconomyResponse(0, getBalance(playerName, worldName), ResponseType.FAILURE, "Cannot withdraw negative funds");
  252. }
  253. double balance;
  254. Account account = Common.getInstance().getAccountManager().getAccount(playerName);
  255. if (account.hasEnough(amount, worldName, Common.getInstance().getCurrencyManager().getDefaultCurrency().getName())) {
  256. balance = account.withdraw(amount, worldName, Common.getInstance().getCurrencyManager().getDefaultCurrency().getName(), Cause.VAULT, null);
  257. return new EconomyResponse(amount, balance, ResponseType.SUCCESS, "");
  258. } else {
  259. return new EconomyResponse(0, getBalance(playerName, worldName), ResponseType.FAILURE, "Insufficient funds");
  260. }
  261. }
  262. @Override
  263. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  264. if (amount < 0) {
  265. return new EconomyResponse(0, getBalance(playerName, worldName), ResponseType.FAILURE, "Cannot desposit negative funds");
  266. }
  267. Account account = Common.getInstance().getAccountManager().getAccount(playerName);
  268. double balance = account.deposit(amount, worldName, Common.getInstance().getCurrencyManager().getDefaultCurrency().getName(), Cause.VAULT, null);
  269. return new EconomyResponse(amount, balance, ResponseType.SUCCESS, null);
  270. }
  271. @Override
  272. public boolean createPlayerAccount(String playerName, String worldName) {
  273. return createPlayerAccount(playerName);
  274. }
  275. }