Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. public class EconomyUtils {
  2.     private static Economy economy;
  3.     public static boolean setupEconomy() {
  4.         if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
  5.             return false;
  6.         }
  7.         RegisteredServiceProvider rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
  8.         if (rsp == null) {
  9.             return false;
  10.         }
  11.         economy = (Economy)rsp.getProvider();
  12.         return economy != null;
  13.     }
  14.  
  15.     public static boolean hasValidEconomy() {
  16.         return Bukkit.getServer() != null && economy != null;
  17.     }
  18.  
  19.     public static Economy getEconomy() {
  20.         if (!hasValidEconomy()) throw new IllegalStateException("Economy plugin was not found!");
  21.         return economy;
  22.     }
  23.  
  24.     public static double getMoney(Player player) {
  25.         if (!hasValidEconomy()) throw new IllegalStateException("getEconomy() plugin was not found!");
  26.         return getEconomy().getBalance(player.getName(), player.getWorld().getName());
  27.     }
  28.  
  29.     public static boolean hasMoney(Player player, double minimum) {
  30.         if(CompileFlag.FOR_SERVER) {
  31.             if (!hasValidEconomy()) {
  32.                 throw new IllegalStateException("getEconomy() plugin was not found!");
  33.             }
  34.             if (minimum < 0.0D) throw new IllegalArgumentException("Invalid amount of money: " + minimum);
  35.    
  36.             double balance = getEconomy().getBalance(player.getName(), player.getWorld().getName());
  37.    
  38.             if (balance < minimum) {
  39.                 return false;
  40.             }
  41.         }
  42.         return true;
  43.     }
  44.  
  45.     public static boolean takeMoney(Player player, double amount) {
  46.         if(CompileFlag.FOR_SERVER) {
  47.             if (!hasValidEconomy()) throw new IllegalStateException("getEconomy() plugin was not found!");
  48.             if (amount < 0.0D) throw new IllegalArgumentException("Invalid amount of money: " + amount);
  49.            
  50.             EconomyResponse response = getEconomy().withdrawPlayer(player.getName(), player.getWorld().getName(), amount);
  51.             return response.transactionSuccess();
  52.         }
  53.         return false;
  54.     }
  55.  
  56.     public static boolean giveMoney(Player player, double amount) {
  57.         if(CompileFlag.FOR_SERVER) {
  58.             if (!hasValidEconomy()) throw new IllegalStateException("getEconomy() plugin was not found!");
  59.             if (amount < 0.0D) throw new IllegalArgumentException("Invalid amount of money: " + amount);
  60.    
  61.             EconomyResponse response = getEconomy().depositPlayer(player.getName(), player.getWorld().getName(), amount);
  62.             return response.transactionSuccess();
  63.         }
  64.         return false;
  65.     }
  66.  
  67.     public static String formatMoney(double amount) {
  68.         if (hasValidEconomy()) {
  69.             return getEconomy().format(amount);
  70.         }
  71.         return Double.toString(amount);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement