PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 389 lines | 276 code | 95 blank | 18 comment | 48 complexity | 98a9d639e53c46e9db9afb805a902a45 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.gmail.mirelatrue.xpbank.API;
  27. import com.gmail.mirelatrue.xpbank.Account;
  28. import com.gmail.mirelatrue.xpbank.GroupBank;
  29. import com.gmail.mirelatrue.xpbank.XPBank;
  30. public class Economy_XPBank extends AbstractEconomy {
  31. private static final Logger log = Logger.getLogger("Minecraft");
  32. private final String name = "XPBank";
  33. private Plugin plugin = null;
  34. private XPBank XPB = null;
  35. private API api = null;
  36. public Economy_XPBank (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 (XPB == null) {
  41. Plugin economy = plugin.getServer().getPluginManager().getPlugin("XPBank");
  42. if (economy != null && economy.isEnabled()) {
  43. XPB = (XPBank) economy;
  44. api = XPB.getAPI();
  45. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  46. }
  47. }
  48. }
  49. public class EconomyServerListener implements Listener {
  50. Economy_XPBank economy = null;
  51. public EconomyServerListener (Economy_XPBank economy_XPBank) {
  52. this.economy = economy_XPBank;
  53. }
  54. @EventHandler (priority = EventPriority.MONITOR)
  55. public void onPluginEnable (PluginEnableEvent event) {
  56. if (economy.XPB == null) {
  57. Plugin eco = event.getPlugin();
  58. if (eco.getDescription().getName().equals("XPBank")) {
  59. economy.XPB = (XPBank) eco;
  60. api = XPB.getAPI();
  61. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  62. }
  63. }
  64. }
  65. @EventHandler (priority = EventPriority.MONITOR)
  66. public void onPluginDisable (PluginDisableEvent event) {
  67. if (economy.XPB != null) {
  68. if (event.getPlugin().getDescription().getName().equals("XPBank")) {
  69. economy.XPB = null;
  70. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  71. }
  72. }
  73. }
  74. }
  75. @Override
  76. public boolean isEnabled () {
  77. return this.XPB != null;
  78. }
  79. @Override
  80. public String getName () {
  81. return name;
  82. }
  83. @Override
  84. public boolean hasBankSupport () {
  85. return true;
  86. }
  87. @Override
  88. public int fractionalDigits () {
  89. return 0;
  90. }
  91. @Override
  92. public String format (double amount) {
  93. return String.format("%d %s", (int) amount, api.currencyName((int) amount));
  94. }
  95. @Override
  96. public String currencyNamePlural () {
  97. return api.getMsg("CurrencyNamePlural");
  98. }
  99. @Override
  100. public String currencyNameSingular () {
  101. return api.getMsg("currencyName");
  102. }
  103. @Override
  104. public boolean hasAccount (String playerName) {
  105. Account account = api.getAccount(playerName);
  106. if (account != null) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. @Override
  112. public double getBalance (String playerName) {
  113. Account account = api.getAccount(playerName);
  114. return account.getBalance();
  115. }
  116. @Override
  117. public boolean has (String playerName, double amount) {
  118. Account account = api.getAccount(playerName);
  119. if (account.getBalance() >= (int) amount) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. @Override
  125. public EconomyResponse withdrawPlayer (String playerName, double amount) {
  126. Account account = api.getAccount(playerName);
  127. if (account == null) {
  128. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("Player doesn't exist."));
  129. }
  130. int value = (int) amount;
  131. int balance = account.getBalance();
  132. if (value < 1) {
  133. return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
  134. }
  135. if (value > balance) {
  136. return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
  137. }
  138. account.modifyBalance(-value);
  139. return new EconomyResponse(value, balance - value, ResponseType.SUCCESS, null);
  140. }
  141. @Override
  142. public EconomyResponse depositPlayer (String playerName, double amount) {
  143. Account account = api.getAccount(playerName);
  144. if (account == null) {
  145. // Stupid plugins that use fake players without creating them first...
  146. // return new EconomyResponse(0, 0, ResponseType.FAILURE, "Player doesn't exist");
  147. this.createPlayerAccount(playerName);
  148. }
  149. int value = (int) amount;
  150. int balance = account.getBalance();
  151. if (value < 1) {
  152. return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
  153. }
  154. account.addTaxableIncome(value);
  155. return new EconomyResponse(value, balance + value, ResponseType.SUCCESS, null);
  156. }
  157. @Override
  158. public EconomyResponse createBank (String name, String player) {
  159. GroupBank groupBank = api.getGroupBank(name);
  160. if (groupBank != null) {
  161. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, String.format(api.getMsg("GroupBankExists"), name));
  162. }
  163. Account account = api.getAccount(player);
  164. groupBank = api.createGroupBank(name, account);
  165. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
  166. }
  167. @Override
  168. public EconomyResponse deleteBank (String name) {
  169. GroupBank groupBank = api.getGroupBank(name);
  170. if (groupBank == null) {
  171. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
  172. }
  173. api.deleteGroupBank(groupBank, String.format(api.getMsg("Disbanded"), groupBank.getName()));
  174. return new EconomyResponse(0, 0, ResponseType.SUCCESS, null);
  175. }
  176. @Override
  177. public EconomyResponse bankBalance (String name) {
  178. GroupBank groupBank = api.getGroupBank(name);
  179. if (groupBank == null) {
  180. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
  181. }
  182. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
  183. }
  184. @Override
  185. public EconomyResponse bankHas (String name, double amount) {
  186. GroupBank groupBank = api.getGroupBank(name);
  187. if (groupBank == null) {
  188. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
  189. }
  190. int value = (int) amount;
  191. int balance = groupBank.getBalance();
  192. if (balance >= value) {
  193. return new EconomyResponse(0, balance, ResponseType.SUCCESS, null);
  194. }
  195. return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
  196. }
  197. @Override
  198. public EconomyResponse bankWithdraw (String name, double amount) {
  199. GroupBank groupBank = api.getGroupBank(name);
  200. if (groupBank == null) {
  201. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
  202. }
  203. int value = (int) amount;
  204. int balance = groupBank.getBalance();
  205. if (value < 1) {
  206. return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
  207. }
  208. if (value > balance) {
  209. return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
  210. }
  211. groupBank.modifyBalance(-value);
  212. return new EconomyResponse(value, balance - value, ResponseType.SUCCESS, null);
  213. }
  214. @Override
  215. public EconomyResponse bankDeposit (String name, double amount) {
  216. GroupBank groupBank = api.getGroupBank(name);
  217. if (groupBank == null) {
  218. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
  219. }
  220. int value = (int) amount;
  221. int balance = groupBank.getBalance();
  222. if (value < 1) {
  223. return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
  224. }
  225. groupBank.modifyBalance(value);
  226. return new EconomyResponse(value, balance + value, ResponseType.SUCCESS, null);
  227. }
  228. @Override
  229. public EconomyResponse isBankOwner (String name, String playerName) {
  230. GroupBank groupBank = api.getGroupBank(name);
  231. if (groupBank == null) {
  232. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
  233. }
  234. Account account = api.getAccount(name);
  235. if (account == null) {
  236. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, api.getMsg("PlayerNotExist"));
  237. }
  238. if (groupBank.getOwner().equalsIgnoreCase(name)) {
  239. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
  240. }
  241. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, String.format(api.getMsg("PlayerNotOwner"), account.getName(), groupBank.getName()));
  242. }
  243. @Override
  244. public EconomyResponse isBankMember (String name, String playerName) {
  245. GroupBank groupBank = api.getGroupBank(name);
  246. if (groupBank == null) {
  247. return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
  248. }
  249. Account account = api.getAccount(name);
  250. if (account == null) {
  251. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, api.getMsg("PlayerNotExist"));
  252. }
  253. if (groupBank.groupMembers.getMembers().containsKey(playerName)) {
  254. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
  255. }
  256. return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, String.format(api.getMsg("NotAMemberOf"), groupBank.getName(), account.getName()));
  257. }
  258. @Override
  259. public List<String> getBanks () {
  260. return api.getAllGroupBanks();
  261. }
  262. @Override
  263. public boolean createPlayerAccount (String playerName) {
  264. api.createAccount(playerName);
  265. return true;
  266. }
  267. @Override
  268. public boolean hasAccount(String playerName, String worldName) {
  269. return hasAccount(playerName);
  270. }
  271. @Override
  272. public double getBalance(String playerName, String world) {
  273. return getBalance(playerName);
  274. }
  275. @Override
  276. public boolean has(String playerName, String worldName, double amount) {
  277. return has(playerName, amount);
  278. }
  279. @Override
  280. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  281. return withdrawPlayer(playerName, amount);
  282. }
  283. @Override
  284. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  285. return depositPlayer(playerName, amount);
  286. }
  287. @Override
  288. public boolean createPlayerAccount(String playerName, String worldName) {
  289. return createPlayerAccount(playerName);
  290. }
  291. }