PageRenderTime 39ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 318 lines | 253 code | 49 blank | 16 comment | 21 complexity | 117f35bb36731f47c9b9b2843e419dfb 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 net.milkbowl.vault.economy.AbstractEconomy;
  18. import net.milkbowl.vault.economy.EconomyResponse;
  19. import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
  20. import org.bukkit.Bukkit;
  21. import org.bukkit.event.EventHandler;
  22. import org.bukkit.event.EventPriority;
  23. import org.bukkit.event.Listener;
  24. import org.bukkit.event.server.PluginDisableEvent;
  25. import org.bukkit.event.server.PluginEnableEvent;
  26. import org.bukkit.plugin.Plugin;
  27. import com.earth2me.essentials.Essentials;
  28. import com.earth2me.essentials.api.NoLoanPermittedException;
  29. import com.earth2me.essentials.api.UserDoesNotExistException;
  30. public class Economy_Essentials extends AbstractEconomy {
  31. private static final Logger log = Logger.getLogger("Minecraft");
  32. private final String name = "Essentials Economy";
  33. private Plugin plugin = null;
  34. private Essentials ess = null;
  35. public Economy_Essentials(Plugin plugin) {
  36. this.plugin = plugin;
  37. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  38. // Load Plugin in case it was loaded before
  39. if (ess == null) {
  40. Plugin essentials = plugin.getServer().getPluginManager().getPlugin("Essentials");
  41. if (essentials != null && essentials.isEnabled()) {
  42. ess = (Essentials) essentials;
  43. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  44. }
  45. }
  46. }
  47. @Override
  48. public boolean isEnabled() {
  49. if (ess == null) {
  50. return false;
  51. } else {
  52. return ess.isEnabled();
  53. }
  54. }
  55. @Override
  56. public String getName() {
  57. return name;
  58. }
  59. @Override
  60. public double getBalance(String playerName) {
  61. double balance;
  62. try {
  63. balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
  64. } catch (UserDoesNotExistException e) {
  65. createPlayerAccount(playerName);
  66. balance = 0;
  67. }
  68. return balance;
  69. }
  70. @Override
  71. public boolean createPlayerAccount(String playerName) {
  72. if (hasAccount(playerName)) {
  73. return false;
  74. }
  75. return com.earth2me.essentials.api.Economy.createNPC(playerName);
  76. }
  77. @Override
  78. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  79. if (amount < 0) {
  80. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot withdraw negative funds");
  81. }
  82. double balance;
  83. EconomyResponse.ResponseType type;
  84. String errorMessage = null;
  85. try {
  86. com.earth2me.essentials.api.Economy.subtract(playerName, amount);
  87. balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
  88. type = EconomyResponse.ResponseType.SUCCESS;
  89. } catch (UserDoesNotExistException e) {
  90. if (createPlayerAccount(playerName)) {
  91. return withdrawPlayer(playerName, amount);
  92. } else {
  93. amount = 0;
  94. balance = 0;
  95. type = EconomyResponse.ResponseType.FAILURE;
  96. errorMessage = "User does not exist";
  97. }
  98. } catch (NoLoanPermittedException e) {
  99. try {
  100. balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
  101. amount = 0;
  102. type = EconomyResponse.ResponseType.FAILURE;
  103. errorMessage = "Loan was not permitted";
  104. } catch (UserDoesNotExistException e1) {
  105. amount = 0;
  106. balance = 0;
  107. type = EconomyResponse.ResponseType.FAILURE;
  108. errorMessage = "User does not exist";
  109. }
  110. }
  111. return new EconomyResponse(amount, balance, type, errorMessage);
  112. }
  113. @Override
  114. public EconomyResponse depositPlayer(String playerName, double amount) {
  115. if (amount < 0) {
  116. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Cannot desposit negative funds");
  117. }
  118. double balance;
  119. EconomyResponse.ResponseType type;
  120. String errorMessage = null;
  121. try {
  122. com.earth2me.essentials.api.Economy.add(playerName, amount);
  123. balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
  124. type = EconomyResponse.ResponseType.SUCCESS;
  125. } catch (UserDoesNotExistException e) {
  126. if (createPlayerAccount(playerName)) {
  127. return depositPlayer(playerName, amount);
  128. } else {
  129. amount = 0;
  130. balance = 0;
  131. type = EconomyResponse.ResponseType.FAILURE;
  132. errorMessage = "User does not exist";
  133. }
  134. } catch (NoLoanPermittedException e) {
  135. try {
  136. balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
  137. amount = 0;
  138. type = EconomyResponse.ResponseType.FAILURE;
  139. errorMessage = "Loan was not permitted";
  140. } catch (UserDoesNotExistException e1) {
  141. balance = 0;
  142. amount = 0;
  143. type = EconomyResponse.ResponseType.FAILURE;
  144. errorMessage = "Loan was not permitted";
  145. }
  146. }
  147. return new EconomyResponse(amount, balance, type, errorMessage);
  148. }
  149. public class EconomyServerListener implements Listener {
  150. Economy_Essentials economy = null;
  151. public EconomyServerListener(Economy_Essentials economy) {
  152. this.economy = economy;
  153. }
  154. @EventHandler(priority = EventPriority.MONITOR)
  155. public void onPluginEnable(PluginEnableEvent event) {
  156. if (economy.ess == null) {
  157. Plugin essentials = event.getPlugin();
  158. if (essentials.getDescription().getName().equals("Essentials")) {
  159. economy.ess = (Essentials) essentials;
  160. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  161. }
  162. }
  163. }
  164. @EventHandler(priority = EventPriority.MONITOR)
  165. public void onPluginDisable(PluginDisableEvent event) {
  166. if (economy.ess != null) {
  167. if (event.getPlugin().getDescription().getName().equals("Essentials")) {
  168. economy.ess = null;
  169. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  170. }
  171. }
  172. }
  173. }
  174. @Override
  175. public String format(double amount) {
  176. return com.earth2me.essentials.api.Economy.format(amount);
  177. }
  178. @Override
  179. public String currencyNameSingular() {
  180. return "";
  181. }
  182. @Override
  183. public String currencyNamePlural() {
  184. return "";
  185. }
  186. @Override
  187. public boolean has(String playerName, double amount) {
  188. try {
  189. return com.earth2me.essentials.api.Economy.hasEnough(playerName, amount);
  190. } catch (UserDoesNotExistException e) {
  191. return false;
  192. }
  193. }
  194. @Override
  195. public EconomyResponse createBank(String name, String player) {
  196. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  197. }
  198. @Override
  199. public EconomyResponse deleteBank(String name) {
  200. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  201. }
  202. @Override
  203. public EconomyResponse bankHas(String name, double amount) {
  204. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  205. }
  206. @Override
  207. public EconomyResponse bankWithdraw(String name, double amount) {
  208. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  209. }
  210. @Override
  211. public EconomyResponse bankDeposit(String name, double amount) {
  212. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  213. }
  214. @Override
  215. public EconomyResponse isBankOwner(String name, String playerName) {
  216. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  217. }
  218. @Override
  219. public EconomyResponse isBankMember(String name, String playerName) {
  220. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  221. }
  222. @Override
  223. public EconomyResponse bankBalance(String name) {
  224. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
  225. }
  226. @Override
  227. public List<String> getBanks() {
  228. return new ArrayList<String>();
  229. }
  230. @Override
  231. public boolean hasBankSupport() {
  232. return false;
  233. }
  234. @Override
  235. public boolean hasAccount(String playerName) {
  236. return com.earth2me.essentials.api.Economy.playerExists(playerName);
  237. }
  238. @Override
  239. public int fractionalDigits() {
  240. return -1;
  241. }
  242. @Override
  243. public boolean hasAccount(String playerName, String worldName) {
  244. return hasAccount(playerName);
  245. }
  246. @Override
  247. public double getBalance(String playerName, String world) {
  248. return getBalance(playerName);
  249. }
  250. @Override
  251. public boolean has(String playerName, String worldName, double amount) {
  252. return has(playerName, amount);
  253. }
  254. @Override
  255. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  256. return withdrawPlayer(playerName, amount);
  257. }
  258. @Override
  259. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  260. return depositPlayer(playerName, amount);
  261. }
  262. @Override
  263. public boolean createPlayerAccount(String playerName, String worldName) {
  264. return createPlayerAccount(playerName);
  265. }
  266. }