PageRenderTime 311ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 251 lines | 191 code | 45 blank | 15 comment | 19 complexity | 2454836fc518fb11cff94320ac92a497 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 co.uk.silvania.cities.digicoin.DigiCoin;
  25. import net.milkbowl.vault.economy.AbstractEconomy;
  26. import net.milkbowl.vault.economy.EconomyResponse;
  27. import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
  28. public class Economy_DigiCoin extends AbstractEconomy {
  29. private static final Logger log = Logger.getLogger("Minecraft");
  30. private final String name = "DigiCoin";
  31. private Plugin plugin = null;
  32. private DigiCoin economy = null;
  33. public Economy_DigiCoin(Plugin plugin){
  34. this.plugin = plugin;
  35. Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
  36. if (economy == null) {
  37. Plugin digicoin = plugin.getServer().getPluginManager().getPlugin(name);
  38. if (digicoin != null && digicoin.isEnabled()) {
  39. economy = (DigiCoin) digicoin;
  40. log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
  41. }
  42. }
  43. }
  44. public class EconomyServerListener implements Listener {
  45. Economy_DigiCoin economy = null;
  46. public EconomyServerListener(Economy_DigiCoin economy) {
  47. this.economy = economy;
  48. }
  49. @EventHandler(priority = EventPriority.MONITOR)
  50. public void onPluginEnable(PluginEnableEvent event) {
  51. if (economy.economy == null) {
  52. Plugin digicoin = event.getPlugin();
  53. if (digicoin.getDescription().getName().equals(economy.name)) {
  54. economy.economy = (DigiCoin) digicoin;
  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 -1;
  84. }
  85. @Override
  86. public String format(double amount) {
  87. if (amount == 1.0) {
  88. return String.format("%d %s", amount, currencyNameSingular());
  89. } else {
  90. return String.format("%d %s", amount, currencyNamePlural());
  91. }
  92. }
  93. @Override
  94. public String currencyNamePlural() {
  95. return "coins";
  96. }
  97. @Override
  98. public String currencyNameSingular() {
  99. return "coin";
  100. }
  101. @Override
  102. public boolean hasAccount(String playerName) {
  103. return true;
  104. }
  105. @Override
  106. public double getBalance(String playerName) {
  107. return economy.getBalance(playerName);
  108. }
  109. @Override
  110. public boolean has(String playerName, double amount) {
  111. return getBalance(playerName) >= amount;
  112. }
  113. @Override
  114. public EconomyResponse withdrawPlayer(String playerName, double amount) {
  115. ResponseType rt;
  116. String message;
  117. if (economy.removeBalance(playerName, amount)) {
  118. rt = ResponseType.SUCCESS;
  119. message = null;
  120. } else {
  121. rt = ResponseType.FAILURE;
  122. message = "Not enough money.";
  123. }
  124. return new EconomyResponse(amount, getBalance(playerName), rt, message);
  125. }
  126. @Override
  127. public EconomyResponse depositPlayer(String playerName, double amount) {
  128. ResponseType rt;
  129. String message;
  130. if (economy.addBalance(playerName, amount)) {
  131. rt = ResponseType.SUCCESS;
  132. message = null;
  133. } else {
  134. rt = ResponseType.FAILURE;
  135. message = "Failed to deposit balance.";
  136. }
  137. return new EconomyResponse(amount, getBalance(playerName), rt, message);
  138. }
  139. @Override
  140. public EconomyResponse createBank(String name, String player) {
  141. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  142. }
  143. @Override
  144. public EconomyResponse deleteBank(String name) {
  145. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  146. }
  147. @Override
  148. public EconomyResponse bankBalance(String name) {
  149. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  150. }
  151. @Override
  152. public EconomyResponse bankHas(String name, double amount) {
  153. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  154. }
  155. @Override
  156. public EconomyResponse bankWithdraw(String name, double amount) {
  157. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  158. }
  159. @Override
  160. public EconomyResponse bankDeposit(String name, double amount) {
  161. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  162. }
  163. @Override
  164. public EconomyResponse isBankOwner(String name, String playerName) {
  165. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  166. }
  167. @Override
  168. public EconomyResponse isBankMember(String name, String playerName) {
  169. return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "DigiCoin does not support bank accounts");
  170. }
  171. @Override
  172. public List<String> getBanks() {
  173. return new ArrayList<String>();
  174. }
  175. @Override
  176. public boolean createPlayerAccount(String playerName) {
  177. return false;
  178. }
  179. @Override
  180. public boolean hasAccount(String playerName, String worldName) {
  181. return true;
  182. }
  183. @Override
  184. public double getBalance(String playerName, String world) {
  185. return getBalance(playerName);
  186. }
  187. @Override
  188. public boolean has(String playerName, String worldName, double amount) {
  189. return has(playerName, amount);
  190. }
  191. @Override
  192. public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
  193. return withdrawPlayer(playerName, amount);
  194. }
  195. @Override
  196. public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
  197. return depositPlayer(playerName, amount);
  198. }
  199. @Override
  200. public boolean createPlayerAccount(String playerName, String worldName) {
  201. return false;
  202. }
  203. }