All onCommand args to a string except one

Discussion in 'Plugin Development' started by Grovert11, Jul 9, 2012.

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

    Grovert11

    I'm making a plugin to talk as if you were someone else.
    The problem I have now is that when I want to get all arguments, it gives the name of the person to say what I want too.
    Here's my code so far:
    Code:
    package me.Grovert11.FakeTalk;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class ftcommand implements CommandExecutor{
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {     
       
            String Italic = ChatColor.ITALIC.toString();
            String Red = ChatColor.RED.toString();
            String Green = ChatColor.GREEN.toString();
            String DarkRed = ChatColor.DARK_RED.toString();
           
            if(commandLabel.equalsIgnoreCase("ft")){
                    if(args.length <= 2){
                        sender.sendMessage(Green + "[FakeTalk] " + Red + Italic + "Please use the command like this: " + DarkRed + "/ft PERSON MESSAGE" + Red + ".");
                    }
                    else{
                        Player player = Bukkit.getPlayer(args[0]);
                        String msg = "";
                        for (String s : args){
                            msg = msg + s + " " ;
                        }
                        player.chat(msg);
                    }
                }
            return true;
        }
     
    } 
    Eg. When I do "/ft Grovert11 Hello all!" it looks like I'm saying: "Grovert11 Hello all!" instead of "Hello all!"
    What I could do is put args[0] = "" but then everything said will have a space in front of it.
     
  2. Offline

    bitWolfy

    I am not sure if I understand you correctly, but this:
    Code:
    String message = args[1];
    for(int i = 1; i < args.length(); i++) {
         message += " " + args[i];
    }
    
    The String message will contain the string of arguments starting with the second one.
     
  3. This exact thing was posted before, why doesn't anyone search ? :/ You would get the result alot faster that way.

    http://forums.bukkit.org/threads/help-unkown-amount-of-args-read-more.85252/
    http://forums.bukkit.org/threads/solved-get-multiple-args-from-command.84319/
    And there are more :}

    But I recommend V10lator's code:
    Code:
    public String buildMessage(String[] input, int startArg) {
      StringBuilder sb = new StringBuilder(input[startArg]);
      for(int i = ++startArg; i < input.length; i++) {
        sb.append(' ').append(input[i]);
      }
      return sb.toString();
    }
     
    ferrybig likes this.
  4. Offline

    TannerLittle

    "alot" No no no no no! It's "a lot"! :mad:
     
Thread Status:
Not open for further replies.

Share This Page