[SOLVED] Subtract Money With Vault

Discussion in 'Plugin Development' started by flaminsnowman99, Jul 11, 2012.

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

    flaminsnowman99

    I need to do as the title says. Let me show you the code so I can better explain.
    Code:
    public void subtractMoney(Player player, int cost) {
        String playername = player.getName();
        if(useEcon) {
            double balance = econ.getBalance(playername);
            //needs to subtract money here
        }
    }
    Where you see the comment. I need something like econ.setbalance or something equivalent to it. Any suggestions?
     
  2. Offline

    flaminsnowman99

    Hm... I tried using code similar to yours, however it doesnt work.
     
  3. Offline

    chaseoes

    I use this:
    Code:
    economy.withdrawPlayer(playername, amount)
     
  4. Why don't you post it and post any errors that you may have ?

    But yeah, basically you just do economy.depositPlayer(playername, amount), amount can be a negative value to subtract or just use economy.withdrawPlayer(playername, amount) after you've set up the economy which is fairly simple, just place this in your main class:
    Code:
    private Economy economy;
    
    // in onEnable()
    
    if(Bukkit.getPluginManager().getPlugin("Vault") instanceof Vault)
    {
    	RegisteredServiceProvider<Economy> service = Bukkit.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    
    	if(service != null)
    		economy = service.getProvider();
    }
    
    // .. in whatever event
    
    economy.depositPlayer(player.getName(), -100); // subtract 100 money from player
    That's all there is to it... but ofc check if economy != null before doing something economy related, to be sure vault is loaded... that unless you want vault as a requirement, in my class vault is optional.
     
    SumrioL likes this.
  5. Offline

    desht

    Actually, the amount may NOT be negative, as I learned from Sleaker recently (and the Vault API docs have been updated to make this clear now). To remove money, you must use withdrawPlayer().

    https://github.com/MilkBowl/Vault/issues/196
     
  6. desht
    Oukay.
    I used that initially but then changed it so I don't have to use Math.abs() on that value and so many conditions xD
    It would've been easier to just use one method for adding or removing.
     
  7. Offline

    desht

  8. Offline

    flaminsnowman99

    Okay. It's working now. Thank you all for the help!
     
Thread Status:
Not open for further replies.

Share This Page