Hook Into Vault

Feb 24, 2018
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 Economy econ;
        private Permission perms;
        private Chat chat;

        @Override
        public void onEnable(){
            if (!setupEconomy()) {
                this.getLogger().severe("Disabled due to no Vault dependency found!");
                Bukkit.getPluginManager().disablePlugin(this);
                return;
            }
            this.setupPermissions();
            this.setupChat();
        }

        private boolean setupEconomy() {
            if (Bukkit.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 Economy getEcononomy() {
            return econ;
        }

        public Permission getPermissions() {
            return perms;
        }

        public 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):
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    }
    Next, we will start the first command, resetbalance by retrieving the "econ" variable from the main class through the use of its getter method, #getEconomy(). Of course, in order to do that, an instance of your main class must be readily available. Whether that's through the use of a singleton or constructor dependency injection (recommended), an instance of your main class is required. This instance will be denoted by the variable "mainInstance".

    Code (Java):
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Economy economy = mainInstance.getEconomy();

        if (args.length == 0) { // Ensure that there was specified a player
            sender.sendMessage("You must specify the name of the player whose balance you would like to reset");
            return true;
        }
        String playerName = String.join(" ", args);
        OfflinePlayer target = Bukkit.getOfflinePlayer(playerName);
        if (target == null) { // Check if player has joined the server
            sender.sendMessage("A player with the name '" + playerName + "' has never joined this server");
            return true;
        }

        // Reset the player's balance to 0
        economy.withdrawPlayer(target, economy.getBalance(target));
    }
  • Loading...
  • Loading...