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

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

https://gitlab.com/MineYourMind/Vault
Java | 276 lines | 202 code | 58 blank | 16 comment | 25 complexity | 6905a51c9f7ea7cca59db10e3084c819 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.Level;
  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.OfflinePlayer;
  23. import org.bukkit.event.EventHandler;
  24. import org.bukkit.event.EventPriority;
  25. import org.bukkit.event.Listener;
  26. import org.bukkit.event.server.PluginDisableEvent;
  27. import org.bukkit.event.server.PluginEnableEvent;
  28. import org.bukkit.plugin.Plugin;
  29. import ca.agnate.EconXP.EconXP;
  30. public class Economy_EconXP extends AbstractEconomy {
  31. private static final Logger log = Logger.getLogger("Minecraft");
  32. private final String name = "EconXP";
  33. private Plugin plugin = null;
  34. private EconXP econ = null;
  35. public Economy_EconXP(Plugin plugin) {
  36. this.plugin = plugin;
  37. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  38. log.log(Level.WARNING, "EconXP is an integer only economy, you may notice inconsistencies with accounts if you do not setup your other econ using plugins accordingly!");
  39. // Load Plugin in case it was loaded before
  40. if (econ == null) {
  41. Plugin econ = plugin.getServer().getPluginManager().getPlugin("EconXP");
  42. if (econ != null && econ.isEnabled()) {
  43. this.econ = (EconXP) econ;
  44. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  45. }
  46. }
  47. }
  48. public class EconomyServerListener implements Listener {
  49. Economy_EconXP economy = null;
  50. public EconomyServerListener(Economy_EconXP economy) {
  51. this.economy = economy;
  52. }
  53. @EventHandler(priority = EventPriority.MONITOR)
  54. public void onPluginEnable(PluginEnableEvent event) {
  55. if (economy.econ == null) {
  56. Plugin eco = event.getPlugin();
  57. if (eco.getDescription().getName().equals("EconXP")) {
  58. economy.econ = (EconXP) eco;
  59. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  60. }
  61. }
  62. }
  63. @EventHandler(priority = EventPriority.MONITOR)
  64. public void onPluginDisable(PluginDisableEvent event) {
  65. if (economy.econ != null) {
  66. if (event.getPlugin().getDescription().getName().equals("EconXP")) {
  67. economy.econ = null;
  68. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  69. }
  70. }
  71. }
  72. }
  73. @Override
  74. public boolean isEnabled() {
  75. return this.econ != null;
  76. }
  77. @Override
  78. public String getName() {
  79. return name;
  80. }
  81. @Override
  82. public String format(double amount) {
  83. amount = Math.ceil(amount);
  84. return String.format("%d %s", (int)amount, "experience");
  85. }
  86. @Override
  87. public String currencyNamePlural() {
  88. return "experience";
  89. }
  90. @Override
  91. public String currencyNameSingular() {
  92. return "experience";
  93. }
  94. @Override
  95. public double getBalance(String playerName) {
  96. OfflinePlayer player = econ.getPlayer(playerName);
  97. if ( player == null ) { return 0; }
  98. return econ.getExp(player);
  99. }
  100. @Override
  101. public boolean has(String playerName, double amount) {
  102. OfflinePlayer player = econ.getPlayer(playerName);
  103. if ( player == null ) { return false; }
  104. return econ.hasExp(player, (int) Math.ceil(amount) );
  105. }
  106. @Override
  107. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  108. OfflinePlayer player = econ.getPlayer(playerName);
  109. if ( player == null ) {
  110. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Player does not exist");
  111. }
  112. double balance = econ.getExp(player);
  113. amount = Math.ceil(amount);
  114. if (amount < 0) {
  115. return new EconomyResponse(0, balance, ResponseType.FAILURE, "Cannot withdraw negative funds");
  116. }
  117. if ( econ.hasExp(player, (int) amount) == false ) {
  118. return new EconomyResponse(0, balance, ResponseType.FAILURE, "Insufficient funds");
  119. }
  120. econ.removeExp(player, (int) amount);
  121. double finalBalance = econ.getExp(player);
  122. return new EconomyResponse(amount, finalBalance, ResponseType.SUCCESS, null);
  123. }
  124. @Override
  125. public EconomyResponse depositPlayer(String playerName, double amount) {
  126. OfflinePlayer player = econ.getPlayer(playerName);
  127. if ( player == null ) {
  128. return new EconomyResponse(0, 0, ResponseType.FAILURE, "Player does not exist");
  129. }
  130. double balance = econ.getExp(player);
  131. amount = Math.ceil(amount);
  132. if (amount < 0) {
  133. return new EconomyResponse(0, balance, ResponseType.FAILURE, "Cannot withdraw negative funds");
  134. }
  135. econ.addExp(player, (int) amount );
  136. balance = econ.getExp(player);
  137. return new EconomyResponse(amount, balance, ResponseType.SUCCESS, null);
  138. }
  139. @Override
  140. public EconomyResponse createBank(String name, String player) {
  141. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  142. }
  143. @Override
  144. public EconomyResponse deleteBank(String name) {
  145. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  146. }
  147. @Override
  148. public EconomyResponse bankHas(String name, double amount) {
  149. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  150. }
  151. @Override
  152. public EconomyResponse bankWithdraw(String name, double amount) {
  153. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  154. }
  155. @Override
  156. public EconomyResponse bankDeposit(String name, double amount) {
  157. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  158. }
  159. @Override
  160. public EconomyResponse isBankOwner(String name, String playerName) {
  161. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  162. }
  163. @Override
  164. public EconomyResponse isBankMember(String name, String playerName) {
  165. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  166. }
  167. @Override
  168. public EconomyResponse bankBalance(String name) {
  169. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "EconXP does not support bank accounts!");
  170. }
  171. @Override
  172. public List<String> getBanks() {
  173. return new ArrayList<String>();
  174. }
  175. @Override
  176. public boolean hasBankSupport() {
  177. return false;
  178. }
  179. @Override
  180. public boolean hasAccount(String playerName) {
  181. return econ.getPlayer(playerName) != null;
  182. }
  183. @Override
  184. public boolean createPlayerAccount(String playerName) {
  185. return false;
  186. }
  187. @Override
  188. public int fractionalDigits() {
  189. return 0;
  190. }
  191. @Override
  192. public boolean hasAccount(String playerName, String worldName) {
  193. return hasAccount(playerName);
  194. }
  195. @Override
  196. public double getBalance(String playerName, String world) {
  197. return getBalance(playerName);
  198. }
  199. @Override
  200. public boolean has(String playerName, String worldName, double amount) {
  201. return has(playerName, amount);
  202. }
  203. @Override
  204. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  205. return withdrawPlayer(playerName, amount);
  206. }
  207. @Override
  208. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  209. return depositPlayer(playerName, amount);
  210. }
  211. @Override
  212. public boolean createPlayerAccount(String playerName, String worldName) {
  213. return createPlayerAccount(playerName);
  214. }
  215. }