How can i use Vault for economy?

Discussion in 'Plugin Development' started by fr0zenst0rm, Jan 12, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    fr0zenst0rm

    I don't understant it :(
    Can someone post an example on Pastebin or here?
    I want to let an command cost some iConomy money!
    Thank you!
     
  2. Offline

    desht

  3. Offline

    fr0zenst0rm

    I added this in my MainClass already:
    Code:
    public static Economy economy = null;
    private Boolean setupEconomy()
        {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }
    
            return (economy != null);
        }
    Here are the JavaDocs: http://mythcraft.dyndns.org/javadoc/vault/

    I want to see an easy example for:

    A player uses a command, exl: /heal . Then he pay x$ (iConomy money) and then he is healed. How can i do this? Can you give me an easy example?

    For Permissions I use PEX, there is an easy native API: https://github.com/t3hk0d3/PermissionsEx/wiki/Native-API-example
     
  4. Offline

    maxp0wer789

    @Override
    public boolean onCommand(CommandSender cs, Command cmd, String string, String[] args) {

    if(!(cs instanceof Player))​
    return true;​

    Player player = (Player) cs;​

    if(cmd.getName().equalsIgnoreCase("heal") ) {​

    if(!economy.has(player.getName(), costsForTheCommand) {​
    cs.sendMessage("Not enough money!");​
    return true;​
    }​

    economy.withdrawPlayer(player.getName(), costsForTheCommand);​
    player.setHealth(20);​
    return true;​
    }​

    return false;​
    }

    I never used vault before but i`m pretty sure that'll work out
    If you want to add costs to a Command from another Plugin just use the onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) in a PlayerListener.
     
  5. Offline

    desht

    Yep, pretty much. The only other thing you might want to do is verify that economy is not null (if economy support is a hard requirement in your plugin, you'd do that in your onEnable() and refuse to continue if no economy is detected, and if economy support is a soft requirement, just check that economy is not null whenever you want to use it).

    You have the Javadocs, the Economy methods are pretty much explained in there. You've seen the has() and withdrawPlayer() methods as posted by @maxp0wer789 - that's pretty much what you need for your purposes.

    You didn't make it clear if you're actually calling the setupEconomy() method you defined, I'll assume that you are :) If not, just a note that it needs to be called from your onEnable().

    And since you already have Vault loaded, why not just get its permissions provider too? Then your plugin would automatically work with every perms plugin out there.
     
  6. I also found them confusing at first but I got them to work.

    It's quite easy, I've done something like this (live example in my WIP TravelCraft plugin):
    Code:
    public static Economy economy;
    
    
    // in onEnable():
    if(pm.getPlugin("Vault") instanceof Vault)
    {
    	final RegisteredServiceProvider<Economy> service = sv.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    			
    	if(service != null)
    	{
    		economy = service.getProvider();
    				
    		if(economy != null)
    			Travel.log("Vault detected and loaded, economy available.");
    	}
    }
    
    
    // where I use it (Travel is my main class pointer):
    if(Travel.economy != null)
    {
    	if(Travel.economy.getBalance(playerName) >= transport.price) // player has enough money
    		Travel.economy.withdrawPlayer(playerName, transport.price); // subtract price from playerName.
    }
    This is for having Vault and economy as optional mainly, but you can use it safely for required as well.

    And for permissions is enough to use player.hasPermission() from bukkit, you don't *need* vault for that.
     
    Minecrafter_NL likes this.
Thread Status:
Not open for further replies.

Share This Page