Simple way to turn args into sentence?

Discussion in 'Plugin Development' started by mrdude123, Jun 10, 2015.

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

    mrdude123

    Hi. This is what I have so far.
    Thank you.

    Code:
    package me.thecerealkill3r.adminchat;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    
    public class AdminChat implements CommandExecutor, Listener {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args){
            if((sender instanceof Player));
            Player player = (Player) sender;
           
            if(args.length < 1){
                sender.sendMessage(ChatColor.RED + "You didn't include a message!");
            }
           
            if(args.length > 1){
              sender.sendMessage( /*/ What would I put here? Thank you. /*/ );
            }
            return false;
    }
    }
     
  2. Offline

    Jackson12

    From my understanding of what you are wanting to do, you could use this
    Code:
    player.sendMessage(args[1]);
     
  3. Offline

    mrdude123

    @Jackson12 That only picks the second out of all your arguments. For example:

    /ac 1 2 3 4 5 6

    args[1] would pick argument number 2
     
  4. Offline

    Adamki11s

    I'd use a string builder to concatenate all of the arguments, simply done as so:

    Code:
    StringBuilder message = new StringBuilder();
    for(int i = 1; i < args.length; i++){
      message.append(args[i]);
      if( (i + 1) != args.length){
        //this is to add a space between args, but not the last word
        message.append(" ");
      }
    }
    
    player.sendMessage(message.toString());
     
    seanliam2000 likes this.
  5. Offline

    seanliam2000

    Oh cool! I was wondering how to do this. Also your signature for your website takes me to a blank page, I had to remove the index part to get onto it
     
  6. Offline

    TheDiamond06

    @mrdude123 Another way you can do it would be to do:
    Code:java
    1.  
    2. String message = "";
    3. for(String s : args)
    4. {
    5. message = message + s + " ";
    6. }
    7. // Variable message will be the message
     
  7. Offline

    CraftBang

  8. Offline

    DuskFireHD

    @mrdude123 i dont know if this is what you mean? StringUtils.join(args, " ");
     
  9. Offline

    teej107

    Is a good way to achieve this. StringBuilder should be used for something like this
    Is an inefficient way. You should use a StringBuilder and append inside the loop.
    Is another good suggestion. If you don't want to append the strings yourself, use the StringUtils class from the Apache Commons lib that is already packaged in the Bukkit API.
     
  10. Offline

    Adamki11s

    Thanks for pointing that out, it's fixed now.
     
    seanliam2000 likes this.
Thread Status:
Not open for further replies.

Share This Page