For Loop and String not working toghther

Discussion in 'Plugin Development' started by Niknea, Aug 11, 2014.

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

    Niknea

    Hello everyone, so I am using the following code:
    PHP:
     String kickMessage "raw";
    for(
    int i 2== args.lengthi++){
    kickMessage kickMessage " " args[i];
    }
    victim.kickPlayer(ChatColor.translateAlternateColorCodes('&'kickMessage));
    p.sendMessage(root.prefix "§aUser: §6" args[0] + " §successfully kicked!");
    And no matter what, the kick message is "raw", any ideas?

    Thanks,

    Niknea.
     
  2. Offline

    Necrodoom

    Niknea look at your for loop. It would never work unless args.length is 2, in which case it would throw an array out of bounds exception, because arrays are zero indexed, so args[2] does not exist.
     
  3. Offline

    Niknea

    Necrodoom My apologies, I should've included more code, here is a bit more, as you can see, I checked if the length is 2 or higher.
    PHP:
                        if (args.length >= 2) {
                            
    Player victim Bukkit.getServer().getPlayerExact(args[0]);
                            if (
    victim != null) {
                                
    String kickMessage "raw";
                                for(
    int i 2== args.lengthi++){
                                    
    kickMessage kickMessage " " args[i];
                                }
                                
    victim.kickPlayer(ChatColor.translateAlternateColorCodes('&'kickMessage));
                                
    p.sendMessage(root.prefix "§aUser: §6" args[0] + " §successfully kicked!");
                            }
                            else{
                                
    p.sendMessage(root.prefix "§cThe player: §6" args[0] + " §cis currently not online!");
                            }
     
  4. Offline

    theguynextdoor

    Because your for loop has the experession i == args.length. Therefore when I execute the command and type 6 args, i < args.length therefore your boolean expression will be false and won't execute.
     
    Niknea and hintss like this.
  5. Offline

    Necrodoom

    Niknea exact same answer as before. You messed up your for loop.
     
    Niknea likes this.
  6. Offline

    desht

    Niknea you're using "==" instead of "<=", which is why your loop doesn't work.

    However, here's a way to build the message which doesn't suffer from this problem here:

    PHP:
    String kickMessage "raw " Joiner.on(" ").join(Arrays.copyOfRange(args2args.length));
    Although, I have a suspicion you want to be starting at array index 1; arrays are 0-indexed in Java, so if your intended syntax is /command <player> <message text...>, the first argument of your message is at index 1, not 2.
     
    Niknea likes this.
  7. Offline

    Niknea

Thread Status:
Not open for further replies.

Share This Page