Hook Into Vault

Sep 24, 2017
Hook Into Vault
  • Hook Into Vault

    Utilize the Vault API to connect your own plugin.



    We will create 3 main commands:
    • resetbalance - Sets the player's balance to 0
    • givemoney - Puts $100 in the player's balance.
    • money - Checks the player's balance
    First thing's first, we're going to download Vault. You can get a fresh version here.
    Now, you're going to add "Vault" to your build path, If you don't know how, Please redirect to this wiki.

    Now we can start coding. You'll need a new class hooking into your economy and your onEnable.

    Code (Java):

    package me.yourpackage.vault;

    import net.milkbowl.vault.economy.Economy;
    import org.bukkit.java.JavaPlugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{

        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
        private static Permission perms = null;
        private static Chat chat = null;

        @Override
        public void onEnable(){
    if (!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            setupPermissions();
            setupChat();
        }

    public void onDisable(){
    log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
    }

        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }

        private boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
        }

        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            perms = rsp.getProvider();
            return perms != null;
        }

        public static Economy getEcononomy() {
            return econ;
        }

        public static Permission getPermissions() {
            return perms;
        }

        public static Chat getChat() {
            return chat;
        }

    }
     
    Now, we can start on our first command.

    So, first, we create our command, shown in "Creating A Basic Command". I will be doing this in a different class.

    Code (Java):

    public boolean onCommand(CommandSender player, Command cmd, String label, String[] args) {

    }
     
    Next, we will start the first command, resetbalance. I personally have had errors making econ In a different class, so I will be using the econ in Main

    Code (Java):

    Main.econ.withdrawPlayer(args[0], Main.econ.getBalance(args[0]));
    if (Bukkit.getPlayer(args[0]) != null){
               Main.econ.withdrawPlayer(args[0], Main.econ.getBalance(args[0]));
               return true;
    } else {
              //Player Isnt Online
              return true;
    }


     
  • Loading...
  • Loading...