Solved Turning args into ints

Discussion in 'Plugin Development' started by pingpong1999, Apr 10, 2016.

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

    pingpong1999

    Hey Guys, I am in a bit of a struggle. I honestly have no idea how to get around it.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args){
        
            FileConfiguration config = this.getConfig();
            if (cmd.getName().equalsIgnoreCase("et") && sender instanceof Player){
                Player player = (Player) sender;
    
                int total = config.getInt("tokens." + player.getName().toLowerCase());
                player.sendMessage(ChatColor.GREEN + "You have " + ChatColor.GRAY + total + ChatColor.GREEN + " Tokens");
            return true;
        
            }else if (cmd.getName().equalsIgnoreCase("etgive") && sender instanceof Player){
                Player player = (Player) sender;
                int length = args.length;
                if (length == 1) {
                    for (Player playerToGiveToken : Bukkit.getServer().getOnlinePlayers()){
                        if(playerToGiveToken.getName().equalsIgnoreCase(args[0])){
                            playerToGiveToken.getInventory().addItem(new ItemStack(Material.BEDROCK, 1));
                            playerToGiveToken.sendMessage(ChatColor.GOLD + player.getName() + " Has Given You a Token!");
                            player.sendMessage(ChatColor.GOLD + "You have given " + playerToGiveToken.getName() + " A Token!");
                        }
                    }
                }    
            
            return true;
        
            }
        return false;
        }
    
    I have highlighted the area where I am having trouble, So Where it gives me bedrock, I want to change the 1 to args[1] but then I cant do that, because I declared args as a String. I have tried
    Code:
     Int amount = args[1]; 
    But then there is an error, any ideas?
    -Edit you cant see the highlighted area, sorry. Problem is on line 18 and 2
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    pingpong1999

    well that was simple o_o, thanks!
     
  4. Offline

    timtower Administrator Administrator Moderator

    @pingpong1999 Try to input something that isn't an int, it will throw a nice error for you.
     
  5. Offline

    bennie3211

    @pingpong1999 use a try/catch statement where you parse the string to a int (or whatever) and check for NumberFormatException to catch the error so you can display the player that the value is not valid.
     
Thread Status:
Not open for further replies.

Share This Page