CushyPro

AddonMoney

Jun 3rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package mc.CushyPro.AddonMoney;
  2.  
  3. import org.bukkit.plugin.RegisteredServiceProvider;
  4. import org.luaj.vm2.LuaTable;
  5. import org.luaj.vm2.LuaValue;
  6. import org.luaj.vm2.lib.OneArgFunction;
  7. import org.luaj.vm2.lib.TwoArgFunction;
  8. import org.luaj.vm2.lib.ZeroArgFunction;
  9.  
  10. import mc.CushyPro.HanaBot.APIS.HanaAddon;
  11. import net.milkbowl.vault.economy.Economy;
  12.  
  13. public class Main extends HanaAddon {
  14.  
  15.     private static Economy econ = null;
  16.  
  17.     public void onEnable() {
  18.         if (setupEconomy()) {
  19.             System.out.print("Fail to Load Economy");
  20.             return;
  21.         }
  22.        
  23.         System.out.print("Load Economy Successful");
  24.        
  25.         AddLibrary("money", new Money());
  26.     }
  27.  
  28.     public class Money extends ZeroArgFunction {
  29.  
  30.         @Override
  31.         public LuaValue call() {
  32.             LuaTable table = new LuaTable();
  33.             table.set("get", new getMoney());
  34.             return table;
  35.         }
  36.        
  37.         public class setMoney extends TwoArgFunction {
  38.  
  39.             @SuppressWarnings("deprecation")
  40.             @Override
  41.             public LuaValue call(LuaValue args0,LuaValue args1) {
  42.                 if (args0.isstring() && args1.isnumber()) {
  43.                     double newmo = args1.checkdouble();
  44.                     double money = econ.getBalance(args0.checkjstring());
  45.                     if (money > newmo) {
  46.                         double a = money - newmo;
  47.                         econ.withdrawPlayer(args0.checkjstring(), a);
  48.                         return LuaValue.valueOf(money - a);
  49.                     } else if (newmo > money) {
  50.                         double a = newmo - money;
  51.                         econ.depositPlayer(args0.checkjstring(), a);
  52.                         return LuaValue.valueOf(a + money);
  53.                     }
  54.                 }
  55.                 return LuaValue.valueOf("getMoney(string playername) double");
  56.             }
  57.            
  58.         }
  59.  
  60.         public class getMoney extends OneArgFunction {
  61.  
  62.             @SuppressWarnings("deprecation")
  63.             @Override
  64.             public LuaValue call(LuaValue args) {
  65.                 if (args.isstring()) {
  66.                     return LuaValue.valueOf(econ.getBalance(args.checkjstring()));
  67.                 }
  68.                 return LuaValue.valueOf("getMoney(string playername) double");
  69.             }
  70.  
  71.         }
  72.  
  73.     }
  74.  
  75.     private boolean setupEconomy() {
  76.         if (getServer().getPluginManager().getPlugin("Vault") == null) {
  77.             return false;
  78.         }
  79.         RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
  80.         if (rsp == null) {
  81.             return false;
  82.         }
  83.         econ = rsp.getProvider();
  84.         return econ != null;
  85.     }
  86.  
  87. }
Add Comment
Please, Sign In to add comment