I need help trying to stop my integers going to negative!

Discussion in 'Plugin Development' started by PerezHD, Jan 16, 2015.

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

    PerezHD

    Hey guys, I sort of need some help here, well, I am tired of having this issues in most methods I attempt to create, lets say I made a withdraw sign plugin, and a player has per say 10 coins, then he withdraws a sign that says you can withdraw 50, usually in my plugin, it would subtract 50 from the players coin balance, and make it -20, which gets annoying. That is just an example and I really do need some help :/

    MAIN CLASS CAUSING ISSUE:
    Code:
      @EventHandler
      public void onSignClick(PlayerInteractEvent e){  
        Player p = e.getPlayer();
        if ((e.getClickedBlock().getState() instanceof Sign)){
          Sign s = (Sign)e.getClickedBlock().getState();
          int amnt = Integer.valueOf(ChatColor.stripColor(s.getLine(1))).intValue();
          ItemStack etoken1 = new ItemStack(Material.MAGMA_CREAM, amnt);
          ItemMeta etokens = etoken1.getItemMeta();
          etokens.setDisplayName("§eEnchantment Coin §7(Right Click)");
          List<String> etokenlore1 = new ArrayList();
          etokenlore1.add(ChatColor.GRAY + "Right click them and use them at /warp enchants.");
          etokens.setLore(etokenlore1);
          etoken1.setItemMeta(etokens);
         
          if (s.getLine(0).equalsIgnoreCase("§a[Redeem]")){
              /*
               * Not Enough Etokens Area
               */
              if (this.main.getCoins(p) < 1){
                  p.sendMessage(ChatColor.RED + "You to not have enough /etokens to exchange!");
                  return;
              }
              /*
               * Etokens withdraw.
               */
              if (this.main.getCoins(p) > 0){
                  this.main.removeCoins(p, amnt);
                 
                  p.getInventory().addItem(new ItemStack[] { etoken1 });
                  p.updateInventory();
                 
                  p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "-" + Integer.valueOf(ChatColor.stripColor(s.getLine(1))) + " Enchantment Coin(s)");
                  return;
              }
            }
          }
        }

    MY REMOVE COIN METHOD:
    Code:
      public void removeCoins(Player p, int newCoins) {
          int oldCoins = getCoins(p);
          int newpoint = oldCoins - newCoins;
          getUsers().set("Users." + p.getUniqueId(), newpoint);
          saveUsers();
          p.sendMessage(ChatColor.GRAY + "You now currently have " + ChatColor.AQUA + getCoins(p) + ChatColor.GRAY + " /ecoins");
      }
     
  2. Offline

    teej107

    Get the amount and if the new amount would be less than zero, don't continue on.
     
    PerezHD likes this.
  3. Offline

    CubieX

    Ha? Is there really so much imagination necessary to solve this problem? :confused:
    Just calculate what would be left after the withdrawal before actually making it.
    And if the result is < 0, limit the amount to withdraw to the amount the player currently has.
    So he will end up with "0 Coins".
    And if he has to pay something, go with what teej107 said. Check if he has enough coins and if not, abort the action and message him.
     
    PerezHD and Minermax7 like this.
  4. Offline

    Minermax7

    Code:
    if(newcoins < 0) {
    player.sendmessage("You don't have enough coins.");
    }
     
    PerezHD likes this.
Thread Status:
Not open for further replies.

Share This Page