Stop hasmap going less than 0

Discussion in 'Plugin Development' started by Ape101, Mar 7, 2014.

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

    Ape101

    So i've got a hashmap, and i'm using it for a currency
    and i've got a minus method

    Code:java
    1. public void minusCoins(String player, int amount) {
    2. coins.put(player, coins.get(player) - amount);
    3. Bukkit.getLogger().info(player + " had " + Integer.valueOf(amount) + " minused from their account");
    4. }
    5.  


    So how do stop it dropping into negatives, and send the player that they don't have enough coins yadadadada
     
  2. Offline

    RainoBoy97

    This will return false if the player dont have enough money to remove X amount from, and true otherwise.
    Code:
    public boolean minusCoins(String player, int amount) {
      int has = coins.get(player);
      if((has - amount) < 0) return false;
      coins.put(player, has - amount);
      return true;
    } 
    
     
Thread Status:
Not open for further replies.

Share This Page