PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 292 lines | 232 code | 44 blank | 16 comment | 32 complexity | a4ef6619ac73d75e959ceb281c046959 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.iCo6.Constants;
  27. import com.iCo6.iConomy;
  28. import com.iCo6.system.Accounts;
  29. import com.iCo6.system.Holdings;
  30. public class Economy_iConomy6 extends AbstractEconomy {
  31. private static final Logger log = Logger.getLogger("Minecraft");
  32. private String name = "iConomy ";
  33. private Plugin plugin = null;
  34. protected iConomy economy = null;
  35. private Accounts accounts;
  36. public Economy_iConomy6(Plugin plugin) {
  37. this.plugin = plugin;
  38. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  39. log.warning("iConomy - If you are using Flatfile storage be aware that versions 6, 7 and 8 have a CRITICAL bug which can wipe ALL iconomy data.");
  40. log.warning("if you're using Votifier, or any other plugin which handles economy data in a threaded manner your server is at risk!");
  41. log.warning("it is highly suggested to use SQL with iCo6 or to use an alternative economy plugin!");
  42. // Load Plugin in case it was loaded before
  43. if (economy == null) {
  44. Plugin ec = plugin.getServer().getPluginManager().getPlugin("iConomy");
  45. if (ec != null && ec.isEnabled() && ec.getClass().getName().equals("com.iCo6.iConomy")) {
  46. String version = ec.getDescription().getVersion().split("\\.")[0];
  47. name += version;
  48. economy = (iConomy) ec;
  49. accounts = new Accounts();
  50. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  51. }
  52. }
  53. }
  54. public class EconomyServerListener implements Listener {
  55. Economy_iConomy6 economy = null;
  56. public EconomyServerListener(Economy_iConomy6 economy) {
  57. this.economy = economy;
  58. }
  59. @EventHandler(priority = EventPriority.MONITOR)
  60. public void onPluginEnable(PluginEnableEvent event) {
  61. if (economy.economy == null) {
  62. Plugin ec = event.getPlugin();
  63. if (ec.getClass().getName().equals("com.iCo6.iConomy")) {
  64. String version = ec.getDescription().getVersion().split("\\.")[0];
  65. name += version;
  66. economy.economy = (iConomy) ec;
  67. accounts = new Accounts();
  68. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  69. }
  70. }
  71. }
  72. @EventHandler(priority = EventPriority.MONITOR)
  73. public void onPluginDisable(PluginDisableEvent event) {
  74. if (economy.economy != null) {
  75. if (event.getPlugin().getDescription().getName().equals("iConomy")) {
  76. economy.economy = null;
  77. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  78. }
  79. }
  80. }
  81. }
  82. @Override
  83. public boolean isEnabled() {
  84. if (economy == null) {
  85. return false;
  86. } else {
  87. return economy.isEnabled();
  88. }
  89. }
  90. @Override
  91. public String getName() {
  92. return name;
  93. }
  94. @Override
  95. public String format(double amount) {
  96. return iConomy.format(amount);
  97. }
  98. @Override
  99. public String currencyNameSingular() {
  100. return Constants.Nodes.Major.getStringList().get(0);
  101. }
  102. @Override
  103. public String currencyNamePlural() {
  104. return Constants.Nodes.Major.getStringList().get(1);
  105. }
  106. @Override
  107. public double getBalance(String playerName) {
  108. if (accounts.exists(playerName)) {
  109. return accounts.get(playerName).getHoldings().getBalance();
  110. } else {
  111. return 0;
  112. }
  113. }
  114. @Override
  115. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  116. if (amount < 0) {
  117. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds");
  118. }
  119. Holdings holdings = accounts.get(playerName).getHoldings();
  120. if (holdings.hasEnough(amount)) {
  121. holdings.subtract(amount);
  122. return new EconomyResponse(amount, holdings.getBalance(), ResponseType.SUCCESS, null);
  123. } else {
  124. return new EconomyResponse(0, holdings.getBalance(), ResponseType.FAILURE, "Insufficient funds");
  125. }
  126. }
  127. @Override
  128. public EconomyResponse depositPlayer(String playerName, double amount) {
  129. if (amount < 0) {
  130. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds");
  131. }
  132. Holdings holdings = accounts.get(playerName).getHoldings();
  133. holdings.add(amount);
  134. return new EconomyResponse(amount, holdings.getBalance(), ResponseType.SUCCESS, null);
  135. }
  136. @Override
  137. public boolean has(String playerName, double amount) {
  138. return getBalance(playerName) >= amount;
  139. }
  140. @Override
  141. public EconomyResponse createBank(String name, String player) {
  142. if (accounts.exists(name)) {
  143. return new EconomyResponse(0, accounts.get(name).getHoldings().getBalance(), ResponseType.FAILURE, "That account already exists.");
  144. }
  145. boolean created = accounts.create(name);
  146. if (created) {
  147. return new EconomyResponse(0, 0, ResponseType.SUCCESS, "");
  148. } else {
  149. return new EconomyResponse(0, 0, ResponseType.FAILURE, "There was an error creating the account");
  150. }
  151. }
  152. @Override
  153. public EconomyResponse deleteBank(String name) {
  154. if (accounts.exists(name)) {
  155. accounts.remove(name);
  156. return new EconomyResponse(0, 0, ResponseType.SUCCESS, "");
  157. }
  158. return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank account does not exist.");
  159. }
  160. @Override
  161. public EconomyResponse bankHas(String name, double amount) {
  162. if (has(name, amount)) {
  163. return new EconomyResponse(0, amount, ResponseType.SUCCESS, "");
  164. } else {
  165. return new EconomyResponse(0, accounts.get(name).getHoldings().getBalance(), ResponseType.FAILURE, "The account does not have enough!");
  166. }
  167. }
  168. @Override
  169. public EconomyResponse bankWithdraw(String name, double amount) {
  170. if (amount < 0) {
  171. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds");
  172. }
  173. return withdrawPlayer(name, amount);
  174. }
  175. @Override
  176. public EconomyResponse bankDeposit(String name, double amount) {
  177. if (amount < 0) {
  178. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds");
  179. }
  180. return depositPlayer(name, amount);
  181. }
  182. @Override
  183. public EconomyResponse isBankOwner(String name, String playerName) {
  184. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "iConomy 6 does not support Bank owners.");
  185. }
  186. @Override
  187. public EconomyResponse isBankMember(String name, String playerName) {
  188. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "iConomy 6 does not support Bank members.");
  189. }
  190. @Override
  191. public EconomyResponse bankBalance(String name) {
  192. if (!accounts.exists(name)) {
  193. return new EconomyResponse(0, 0, ResponseType.FAILURE, "There is no bank account with that name");
  194. } else {
  195. return new EconomyResponse(0, accounts.get(name).getHoldings().getBalance(), ResponseType.SUCCESS, null);
  196. }
  197. }
  198. @Override
  199. public List<String> getBanks() {
  200. throw new UnsupportedOperationException("iConomy does not support listing of bank accounts");
  201. }
  202. @Override
  203. public boolean hasBankSupport() {
  204. return true;
  205. }
  206. @Override
  207. public boolean hasAccount(String playerName) {
  208. return accounts.exists(playerName);
  209. }
  210. @Override
  211. public boolean createPlayerAccount(String playerName) {
  212. if (hasAccount(playerName)) {
  213. return false;
  214. }
  215. return accounts.create(playerName);
  216. }
  217. @Override
  218. public int fractionalDigits() {
  219. return -1;
  220. }
  221. @Override
  222. public boolean hasAccount(String playerName, String worldName) {
  223. return hasAccount(playerName);
  224. }
  225. @Override
  226. public double getBalance(String playerName, String world) {
  227. return getBalance(playerName);
  228. }
  229. @Override
  230. public boolean has(String playerName, String worldName, double amount) {
  231. return has(playerName, amount);
  232. }
  233. @Override
  234. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  235. return withdrawPlayer(playerName, amount);
  236. }
  237. @Override
  238. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  239. return depositPlayer(playerName, amount);
  240. }
  241. @Override
  242. public boolean createPlayerAccount(String playerName, String worldName) {
  243. return createPlayerAccount(playerName);
  244. }
  245. }