PageRenderTime 89ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 259 lines | 199 code | 45 blank | 15 comment | 21 complexity | 234faa84be0eb951483608d872fff7e3 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 org.bukkit.Bukkit;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.EventPriority;
  20. import org.bukkit.event.Listener;
  21. import org.bukkit.event.server.PluginDisableEvent;
  22. import org.bukkit.event.server.PluginEnableEvent;
  23. import org.bukkit.plugin.Plugin;
  24. import net.milkbowl.vault.economy.AbstractEconomy;
  25. import net.milkbowl.vault.economy.EconomyResponse;
  26. import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
  27. import net.teamalpha.taecon.TAEcon;
  28. public class Economy_TAEcon extends AbstractEconomy {
  29. private static final Logger log = Logger.getLogger("Minecraft");
  30. private final String name = "TAEcon";
  31. private Plugin plugin = null;
  32. private TAEcon economy = null;
  33. public Economy_TAEcon(Plugin plugin){
  34. this.plugin = plugin;
  35. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  36. if (economy == null) {
  37. Plugin taecon = plugin.getServer().getPluginManager().getPlugin(name);
  38. if (taecon != null && taecon.isEnabled()) {
  39. economy = (TAEcon) taecon;
  40. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  41. }
  42. }
  43. }
  44. public class EconomyServerListener implements Listener {
  45. Economy_TAEcon economy = null;
  46. public EconomyServerListener(Economy_TAEcon economy) {
  47. this.economy = economy;
  48. }
  49. @EventHandler(priority = EventPriority.MONITOR)
  50. public void onPluginEnable(PluginEnableEvent event) {
  51. if (economy.economy == null) {
  52. Plugin taecon = event.getPlugin();
  53. if (taecon.getDescription().getName().equals(economy.name)) {
  54. economy.economy = (TAEcon) taecon;
  55. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
  56. }
  57. }
  58. }
  59. @EventHandler(priority = EventPriority.MONITOR)
  60. public void onPluginDisable(PluginDisableEvent event) {
  61. if (economy.economy != null) {
  62. if (event.getPlugin().getDescription().getName().equals(economy.name)) {
  63. economy.economy = null;
  64. log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
  65. }
  66. }
  67. }
  68. }
  69. @Override
  70. public boolean isEnabled() {
  71. return economy != null;
  72. }
  73. @Override
  74. public String getName() {
  75. return name;
  76. }
  77. @Override
  78. public boolean hasBankSupport() {
  79. return false;
  80. }
  81. @Override
  82. public int fractionalDigits() {
  83. return 0;
  84. }
  85. @Override
  86. public String format(double amount) {
  87. amount = Math.ceil(amount);
  88. if (amount == 1) {
  89. return String.format("%d %s", (int)amount, currencyNameSingular());
  90. } else {
  91. return String.format("%d %s", (int)amount, currencyNamePlural());
  92. }
  93. }
  94. @Override
  95. public String currencyNamePlural() {
  96. return economy.getCurrencyName(true);
  97. }
  98. @Override
  99. public String currencyNameSingular() {
  100. return economy.getCurrencyName(false);
  101. }
  102. @Override
  103. public boolean hasAccount(String playerName) {
  104. return true;
  105. }
  106. @Override
  107. public double getBalance(String playerName) {
  108. return economy.getBalance(playerName);
  109. }
  110. @Override
  111. public boolean has(String playerName, double amount) {
  112. return getBalance(playerName) >= amount;
  113. }
  114. @Override
  115. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  116. ResponseType rt;
  117. String message;
  118. int iamount = (int)Math.ceil(amount);
  119. if (has(playerName, amount)) {
  120. if (economy.removeBalance(playerName, iamount)) {
  121. rt = ResponseType.SUCCESS;
  122. message = null;
  123. } else {
  124. rt = ResponseType.SUCCESS;
  125. message = "ERROR";
  126. }
  127. } else {
  128. rt = ResponseType.FAILURE;
  129. message = "Not enough money";
  130. }
  131. return new EconomyResponse(iamount, getBalance(playerName), rt, message);
  132. }
  133. @Override
  134. public EconomyResponse depositPlayer(String playerName, double amount) {
  135. ResponseType rt;
  136. String message;
  137. int iamount = (int)Math.floor(amount);
  138. if (economy.addBalance(playerName, iamount)) {
  139. rt = ResponseType.SUCCESS;
  140. message = null;
  141. } else {
  142. rt = ResponseType.SUCCESS;
  143. message = "ERROR";
  144. }
  145. return new EconomyResponse(iamount, getBalance(playerName), rt, message);
  146. }
  147. @Override
  148. public EconomyResponse createBank(String name, String player) {
  149. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  150. }
  151. @Override
  152. public EconomyResponse deleteBank(String name) {
  153. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  154. }
  155. @Override
  156. public EconomyResponse bankBalance(String name) {
  157. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  158. }
  159. @Override
  160. public EconomyResponse bankHas(String name, double amount) {
  161. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  162. }
  163. @Override
  164. public EconomyResponse bankWithdraw(String name, double amount) {
  165. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  166. }
  167. @Override
  168. public EconomyResponse bankDeposit(String name, double amount) {
  169. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  170. }
  171. @Override
  172. public EconomyResponse isBankOwner(String name, String playerName) {
  173. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  174. }
  175. @Override
  176. public EconomyResponse isBankMember(String name, String playerName) {
  177. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "TAEcon does not support bank accounts");
  178. }
  179. @Override
  180. public List<String> getBanks() {
  181. return new ArrayList<String>();
  182. }
  183. @Override
  184. public boolean createPlayerAccount(String playerName) {
  185. return false;
  186. }
  187. @Override
  188. public boolean hasAccount(String playerName, String worldName) {
  189. return true;
  190. }
  191. @Override
  192. public double getBalance(String playerName, String world) {
  193. return getBalance(playerName);
  194. }
  195. @Override
  196. public boolean has(String playerName, String worldName, double amount) {
  197. return has(playerName, amount);
  198. }
  199. @Override
  200. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  201. return withdrawPlayer(playerName, amount);
  202. }
  203. @Override
  204. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  205. return depositPlayer(playerName, amount);
  206. }
  207. @Override
  208. public boolean createPlayerAccount(String playerName, String worldName) {
  209. return false;
  210. }
  211. }