Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EconomyUtils {
- private static Economy economy;
- public static boolean setupEconomy() {
- if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
- return false;
- }
- RegisteredServiceProvider rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
- if (rsp == null) {
- return false;
- }
- economy = (Economy)rsp.getProvider();
- return economy != null;
- }
- public static boolean hasValidEconomy() {
- return Bukkit.getServer() != null && economy != null;
- }
- public static Economy getEconomy() {
- if (!hasValidEconomy()) throw new IllegalStateException("Economy plugin was not found!");
- return economy;
- }
- public static double getMoney(Player player) {
- if (!hasValidEconomy()) throw new IllegalStateException("getEconomy() plugin was not found!");
- return getEconomy().getBalance(player.getName(), player.getWorld().getName());
- }
- public static boolean hasMoney(Player player, double minimum) {
- if(CompileFlag.FOR_SERVER) {
- if (!hasValidEconomy()) {
- throw new IllegalStateException("getEconomy() plugin was not found!");
- }
- if (minimum < 0.0D) throw new IllegalArgumentException("Invalid amount of money: " + minimum);
- double balance = getEconomy().getBalance(player.getName(), player.getWorld().getName());
- if (balance < minimum) {
- return false;
- }
- }
- return true;
- }
- public static boolean takeMoney(Player player, double amount) {
- if(CompileFlag.FOR_SERVER) {
- if (!hasValidEconomy()) throw new IllegalStateException("getEconomy() plugin was not found!");
- if (amount < 0.0D) throw new IllegalArgumentException("Invalid amount of money: " + amount);
- EconomyResponse response = getEconomy().withdrawPlayer(player.getName(), player.getWorld().getName(), amount);
- return response.transactionSuccess();
- }
- return false;
- }
- public static boolean giveMoney(Player player, double amount) {
- if(CompileFlag.FOR_SERVER) {
- if (!hasValidEconomy()) throw new IllegalStateException("getEconomy() plugin was not found!");
- if (amount < 0.0D) throw new IllegalArgumentException("Invalid amount of money: " + amount);
- EconomyResponse response = getEconomy().depositPlayer(player.getName(), player.getWorld().getName(), amount);
- return response.transactionSuccess();
- }
- return false;
- }
- public static String formatMoney(double amount) {
- if (hasValidEconomy()) {
- return getEconomy().format(amount);
- }
- return Double.toString(amount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement