Implementing Vault support

Discussion in 'Plugin Development' started by Adrian Pasternak, Oct 19, 2016.

Thread Status:
Not open for further replies.
  1. Hello,
    I have an economy plugin that I made that I want to be supported by vault. I've done some research on this myself, and I believe I have to make a class and implement Economy. Which I did. Though, I'm sure there are tons of things I did wrong with this.
    (I made a quick plugin to test out the support, and it didn't work but I figured it was much deeper than just a stack trace)
    I also looked at some of the code for Fe to see how they did it,

    VaultHandler class
    http://hastebin.com/qajowedugu.java
    (And ignore all the static methods, I was doing some debugging earlier with the custom config file (made a post on that but fixed it) and just haven't changed them all back and passed the instance again)

    and for my main class I copied a method from Fe (and yes I understand what it does)
    Code:
    public void setupVault(Plugin pl){
            Plugin vault = getServer().getPluginManager().getPlugin("Vault");
    
               if (vault == null) {
                   return;
               }
    
               getServer().getServicesManager().register(Economy.class, new VaultHandler(this), this, ServicePriority.Highest);
        }
    
    }
    
    Any help is appreciated!
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Well, the EconomyResponse variable in my onCommand method is null
    Code:
    [Server] INFO at me.adrian.test.TestCMD.onCommand(TestCMD.java:23) ~[?:?]
    My command class
    Code:
     
    package me.adrian.test;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import net.milkbowl.vault.economy.EconomyResponse;
    
    public class TestCMD implements CommandExecutor{
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                if(!(sender instanceof Player)){
                    return true;
                }
                Player p = (Player) sender;
                if(p.isOp()){
                    p.sendMessage("Already opped!");
                    return true;
                }
    else{
                p.setOp(true);
               
                EconomyResponse r = TestMain.econ.depositPlayer(p, 50.0);
                if(r.transactionSuccess()){
                    p.sendMessage("IT worked yayyy");
                }
                return true;
           }
           
           
           
           
        }
    
    }
    
    main test class
    Code:
    package me.adrian.test;
    
    
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.milkbowl.vault.economy.Economy;
    
    public class TestMain extends JavaPlugin{
        public static Economy econ = null;
    
        public void onEnable(){
            setupEconomy();
            getCommand("opme").setExecutor(new TestCMD());
           
        }
        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;
           }
    
       
       
    }
    
    Basically, I'm not really sure how to correctly implement Economy in my vault handler class which I think thats the error. Also not sure if thats what I even should be doing. (See original post for Vault Handler class)
    As always,
    Adrian
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Adrian Pasternak Did you depend on Vault?
    And why aren't you doing anything with the boolean that gets returned by setupEconomy()
     
  5. @timtower
    I did depend Vault in my test plugin.yml

    I've added this
    Code:
    public void onEnable(){
            if(!setupEconomy()){
                getLogger().warning("Couldn't find an economy plugin!");
                return;
            }
                getCommand("opme").setExecutor(new TestCMD());
           
        }
    
    The message did not show when I loaded my server, though I'm still not getting the $50 as I should be for typing the command.
    EDIT: Just realized the depositPlayer method takes in a Player, where in the Economy interface it takes in a string?
    Code:
    @Override
        public EconomyResponse depositPlayer(String arg0, double arg1) {
            return new EconomyResponse(arg1, FileManager.setBalance(arg0, arg1), ResponseType.SUCCESS, "Success!");
        }
    
    should I change it to a player as well? Or would that just mess this up.
    Basically just the depositPlayer and the withdrawPlayer are the two things I want to work (for now), going to test withdraw next.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Adrian Pasternak Vault has many interfaces.
    The one that takes OfflinePlayer should be used, the string version is an old one.
     
  7. @timtower

    Okay, redoing my Vault Handler class because I've added OfflinePlayer support to my economy now.
    Just want to clarify before I start filling out all these methods, would this be the correct way of doing this?

    Code:
    @Override
        public EconomyResponse depositPlayer(OfflinePlayer arg0, double arg1) {
            return new EconomyResponse(arg1, MyEconomyAPI.deposit(arg0, arg1), ResponseType.SUCCESS, "");
        }
    
    MyEconomyAPI class is here http://hastebin.com/iyajikejab.swift
    Thanks,
    Edit: (Yes, I know, I abuse static, but for now I'm going to abuse static until it actually does something to me)
     
    Last edited: Oct 22, 2016
Thread Status:
Not open for further replies.

Share This Page