How to know if a command argument is an integer?

Discussion in 'Spigot Plugin Development' started by Firewall801, Oct 2, 2016.

  1. So,
    I haven't been on my computer in roughly 8 months, so I haven't used the spigot API in a while. I'm trying to get back into it with some practice but I've been a large road bump. I'm coding a health modifying plugin where a command can change the health of a player. I can't figure out how to let the plugin know that the 3rd argument is an integer and let the command change the health of the person to the argument. Any help with this issue, and advice on my coding style, would be much appreciated. CommandExecutor Class(Still in progress):
    Code (Text):

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    public class Commands implements CommandExecutor {
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            FileConfiguration config = Main.getInstance().getConfig();
            String incorrectusage = config.getString("incorrect-command-usage");
            String couldnotfindplayer = config.getString("could-not-find-player").replaceAll("%player%", args[1]);
           
            if(cmd.getName().equalsIgnoreCase("health")){
                if(!(args.length == 3)){
                    sender.sendMessage(incorrectusage);
                    return true;}
               
                if(!(args[0].equalsIgnoreCase("set") || !args[0].equalsIgnoreCase("add") ||
                        !args[0].equalsIgnoreCase("subtract"))){
                    sender.sendMessage(incorrectusage);
                    return true;}
               
                Player target = Bukkit.getPlayerExact(args[1]);
                if(target == null){
                    sender.sendMessage(couldnotfindplayer);
                    return true;
                }
                public boolean isInt(String s){
                   try {
                       Integer.parseInt(s);
                       return true;}
                   catch (NumberFormatException e) {
                       return false;
                   }
                   if(isInt(args[2])){
                      int health = Integer.parseInt(args[2]);
                      int t = args[2];
                   }
               }
                if(args[0].equalsIgnoreCase("set")){
                    target.setHealth(args[2]);
                }
                if(args[0].equalsIgnoreCase("add")){
                   
                }
                if(args[0].equalsIgnoreCase("subtract")){
                   
                }
               
            }
            return false;
        }

    }
     
     
  2. Integer.valueOf(String);
     
    • Like Like x 1
  3. Where would I put that in the code?
     
  4. In an if statement?
     
  5. Mas

    Mas

    Using Integer.parseInt(String) you can make something like this;

    Code (Java):
    boolean isInteger(String s)
    {
        try
        {
            Integer.parseInt(s);
            return true;
        }
        catch (NumberFormatException ex)
        {
            return false;
        }
    }
     
    • Like Like x 1
  6. @ItsMas_ I tried using that method, but it didn't work. I ended up doing this:
    Code (Text):

                if(args[0].equalsIgnoreCase("set")){
                    target.setHealth(Integer.valueOf(args[2]));
                }
     
    Hopefully It actually works!
     
  7. That will be fine until someone sends a string instead of an integer...
     
  8. But when attempting to use the parse method, it says a boolean is not allowed, only a final.
     
  9. Mas

    Mas

    Well the method works, so idk what you are doing...
    With your current code an error will be thrown every time someone puts a non-valid number ;P
     
    • Agree Agree x 1
  10. Can someone give me an example of the parse method implemented into my code or similar code working? I've tried it, but it also throws an error.
     
  11. @ItsMas_ has already given you the exact code. Paste what you did, you must have done something wrong.
     
  12. Mas

    Mas

    This is really basic java but oh well...
    Code (Java):
    if (isInteger(<ARGUMENT>))
    {
        // do whatever with the player
    }
    else
    {
        player.sendMessage("that isn't a number...");
    }
     
    • Agree Agree x 1
    • Winner Winner x 6
  13. Choco

    Moderator

    Apache Commons comes with a fancy class for you to use if you would like
    Code (Java):
    NumberUtils.isNumber(String);
     
    • Agree Agree x 1
  14. That's not your intended effect, although this should be:
    Code (Text):
    if(!(args[0].equalsIgnoreCase("set") || args[0].equalsIgnoreCase("add") ||
                        args[0].equalsIgnoreCase("subtract"))){
     
    • Agree Agree x 1
  15. You can also do:
    Code (Text):
    if(StringUtils.isNumeric(args[2])){
    i think