Custom Balance

Discussion in 'Spigot Plugin Development' started by Rawlins, May 13, 2017.

  1. Code (Text):
    public void takeCoins(Player p, int i){
        getConfig().set(p.getUniqueId().toString() + ".Coins", getConfig().getInt(p.getUniqueId().toString() + ".Coins", 0) - i ||);
        if (!(i != null));
        saveConfig();
        p.sendMessage("§3" + i + " coins taken!");
    }else{
    p.sendMessage("§4Error);
    }
    How would I make it so if they try to takecoins that will make their balance a neg it will stop the acting from happening and display an error message
     
  2. example:

    Code (Java):

    public void take(Player player, int amount) {
         if (BalanceManager.get(player).getAmount() - amount < 0) {
              //error
              return;
         }
         BalanceManager.get(player).subtractAmount(amount);
    }
     
    also if you did not want to provide an 'error' type message and default the balance to 0 if its a negative value you could do something like:

    Code (Java):

    public void take(Player player, int amount) {
         BalanceManager.get(player).setAmount((BalanceManager.get(player).getAmount() - amount < 0) ? 0 : BalanceManager.get(player).getAmount() - amount);
    }
     
  3. How should I create a balance management ​
     
  4. what i provided is just an example.

    you could create a managing class that manipulates the player's balance (from config in this case), with methods such as the ones i provided.
     
  5. Just follow this video.