Hook Into Vault

Discussion in 'Wiki Discussion' started by jflory7, May 27, 2015.

  1. jflory7

    Retired Benefactor

    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));
    }
     
    • Informative Informative x 1
  2. jflory7

    Retired Benefactor

    @Rendadus
    I think this will be a great snippet once it's completed – however, when do you think it might be complete? It might not be the best thing to publish the article yet if it's not fully completed. I definitely think this would be a useful snippet for beginning developers!
     
  3. Ty for uploading this into "Wiki Discussion"
    I'll finish it today, :)
     
    • Like Like x 1
  4. Too bad the OP was banned before he could finish it.
     
    • Funny Funny x 1
  5. Does there really need to be a wiki-page about this though? For one, Vault is already well-documented, and their documentation covers the exact same things as this tutorial is supposed to. Furthermore, this would basically be an API-documentation for a specific plugin hosted in the spigot wiki, and even though Vault is very popular I don't think that's a precedent that is wise to establish.
     
    • Agree Agree x 2
  6. While this wiki page has been recently updated, there's a broken link near the top:
    "Now, you're going to add "Vault" to your build path, If you don't know how, Please redirect to this wiki."
    I could really use that information.
     
  7. What IDE are you using?

    If you use eclipse, download the Vault Jar... create your project in eclipse, right click it and click "Build Path" & "Add External Jars".. then select Vault. You can now start using the Vault API :D
     

Share This Page