How to get all the arguments?

Discussion in 'Plugin Development' started by blok601, Dec 5, 2015.

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

    blok601

    How would I be able to get all the arguments the user specified such as /command send Hello my name is blok601.
    It would send the message to the player: send Hello my name is blok601. How would I do that? I was trying to use stringbuilder but it would send the player something like:

    send
    send Hello
    send Hello my name
    send Hello my name is
    send Hello my name is blok601.

    Any ideas?
     
  2. Offline

    Scimiguy

    This is a problem with your code, notably, your for loop containing your StringBuilder.

    Move your Player#sendMessage() outside of your for loop
     
  3. Offline

    blok601

    Here is my code:

    Code:
     
                   for(int i = 1; i < args.length; i++) {
                        sb.append(args[i]).append(" ");
                    }
                    Bukkit.broadcastMessage(sb.toString());
     
  4. Offline

    mcdorli

    Can you show the full class please?
     
  5. Offline

    blok601

    Code:
    public class Main extends JavaPlugin{
       
       
        StringBuilder sb = new StringBuilder("");
       
       
        String info = "";
       
       
        String MSG1 = "Test Message 1";
        String MSG2 = "Test Message 2";
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(cmd.getName().equalsIgnoreCase("test")){
                Player p = (Player) sender;
                if(args.length == 0){
                p.sendMessage("Test!");
                }
                if(args.length == 1){
                    if(args[0].equals("1")){
                        p.sendMessage(MSG1);
                    }else if(args[0].equals("2")){
                        p.sendMessage(MSG2);
                    }else if(args[0].equals("3")){
                        sb.append(MSG1).append(" ").append(MSG2);
                        p.sendMessage("Hello " + sb.toString());
                    }
                }
            }else if(cmd.getName().equalsIgnoreCase("command")){
                if(args.length > 1){
                    for(int i = 1; i < args.length; i++) {
                        sb.append(args[i]).append(" ");
                    }
                    Bukkit.broadcastMessage(sb.toString());
                }
            }
            return false;
           
        }
    
    }
     
  6. Offline

    Scimiguy

    Why is your stringbuilder outside of your method?
     
  7. Offline

    mcdorli

    1.: Blind casting a sender to a player is like ppaying shoot the apple in a sandstorm
    2.: You don't need to use a string bulder to small tasks like the first real command, you lose more memory, than you actually save.
    3.: You can just call the stringbuilders constructor without any arguments, you dpn't need empty ""-s

    I don't see, why the code is wrong, did you tried to rebuild it?
     
  8. Offline

    elian1203

    Arrays.toString(args).replace("[", "").replace("]", "").replace(",", "") maybe?
     
  9. Offline

    Scimiguy

    @elian1203
    If you have a potentially large array, use a StringBuilder instead.

    There's nothing wrong with StringBuilders, you just have to use them properly
     
  10. Offline

    teej107

    elian1203 likes this.
  11. Offline

    blok601

    This was a test plugin. I was testing ideas for another plugin.

    EDIT: @teej107 it worked perfectly. Just one question though, how would I get the args starting at a certain number of argument?
     
    Last edited: Dec 6, 2015
  12. Offline

    elian1203

    This works for me:
    StringBuilder msg = new StringBuilder(args[1]);
    for (int arg = 2; arg < args.length; arg++) {
    msg.append(" ").append(args[arg]);
    }
     
  13. Offline

    teej107

    There are several join methods to do what you want to achieve. Take a look in the JavaDocs that I linked.
     
Thread Status:
Not open for further replies.

Share This Page