Check if an int isn't going under 0

Discussion in 'Spigot Plugin Development' started by TheGameKing77, Jul 25, 2018.

  1. Hi,

    I have a /tokens send <player> <amount> command. I use the following code for it basicly:

    Code (Java):
    if(userData.getInt("user." + target.getUniqueId().toString() + ".tokenAmount") >= sendAmount) {
                                                int ownCurrent = userData.getInt("user." + p.getUniqueId().toString() + ".tokenAmount");
                                                int targetCurrent = userData.getInt("user." + target.getUniqueId().toString() + ".tokenAmount");
                                                // player has enough money, take amount from player
                                                // add amount to target
                                                userData.set("user." + p.getUniqueId().toString() + ".tokenAmount", ownCurrent - sendAmount);
                                                userData.set("user." + target.getUniqueId().toString() + ".tokenAmount", + targetCurrent + sendAmount);
                                                //send message to player
                                                int amount = userData.getInt("user." + p.getUniqueId().toString() + ".tokenAmount");
                                                String pname = p.getName();
                                                String puuid = p.getUniqueId().toString();
                                                String targetname = target.getName();
                                                String amountString = Integer.toString(amount);
                                                String sendString = Integer.toString(sendAmount);
                                                p.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&', messages.getString("tokensSent").replace("%player_name%", pname).replace("%player_uuid%", puuid).replace("%amount%", amountString).replace("%send_amount%", sendString).replace("%target_player%", targetname)));
                                                //send message to target
                                                target.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&', messages.getString("tokensReceived").replace("%player_name%", pname).replace("%player_uuid%", puuid).replace("%amount%", amountString).replace("%receive_amount%", sendString).replace("%target_player%", targetname)));
    I was just messing around to find bugs, and found out that when the player wants to send more than the starting amount (25) they can't. But they can send for example 10, and stay sending it. The int can go under 0. How can i fix this? And how can i fix that they can just send any amount, as long as the int "tokenAmount" in their userData.yml is >=?
  2. You've got a logic mistake in the first line:

    Code (Java):
    if(userData.getInt("user." + target.getUniqueId().toString() + ".tokenAmount") >= sendAmount) { // WRONG
    if(userData.getInt("user." + p.getUniqueId().toString() + ".tokenAmount") >= sendAmount) { // RIGHT
  3. haha oops, didn't see that ;) Thanks