Making multiple args

Discussion in 'Spigot Plugin Development' started by ClickToAccept, Apr 24, 2017.

  1. I'm making an announcement plugin to my core for my server. So far, I've made it so that when you run the command /broadcast and put in a message after broadcast, it sends only 1 message, so if you have a space after one message. it wont send the rest. I have watched PogoStick29's multiple argument video in revamped bukkit tutorials, but it still doesn't work, I've asked a friend too but he said he wasn't sure.

    Here is my code:

    Code (Text):
    package core.utils;

    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 Anouncements implements CommandExecutor {
        public boolean onCommand(CommandSender sender, Command cmd, String commandLebal, String[] args) {
            if (cmd.getName().equalsIgnoreCase("broadcast")) {
                if (!sender.hasPermission("broadcast")) {
                    sender.sendMessage(ChatColor.RED + "You do not fit suffecient permissions to run this command.");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Correct usage: /broadcast <Message>");
                    return true;
            }
        }
                String msg = "";
                for (int i = 0; i < args.length; i++) {
                    msg += args[i] + " ";
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "[" + ChatColor.RED + sender.getName()
                            + ChatColor.LIGHT_PURPLE + "] " + ChatColor.LIGHT_PURPLE + msg);
                   msg = msg.trim();
                    return true;

                }


        return true;
        }
    }
     
    Thanks for the help if you know any solutions :)
     
  2. PHP:

    String s = null;
    for (int x = 0; x < args.lenght(); x++){
        if (s == null){
            s = args[x];
        }else{
            s += " " + args[x];
        }
    }
     
     
  3. I prefer to do it like this:
    Code (Text):
    String out = "";
    if (args.length != 0) {
        out = args[0];
        for (int i = 1; i < args.length; i++) {
            out += " " + args[i];
        }
    }
     
    • Like Like x 1
  4. You simply need to doublecheck your for loop ;)

    You are building your message in a loop, so far so good. But directly after adding the first argument to the message you already send the broadcast and return true. You need the loop to run through all args so that your message is complete, that means sending the brodcast inside the loop and returning is a bad idea. Move your broadcast and the return statement out of the loop and it should work more as aspected ;)
     
  5. Mas

    Mas

    Just saying that you should be using a StringBuilder/StringJoiner when combining strings in a loop.


    EDIT: In this case just can just String.join(" ", args);
     
    • Like Like x 1
  6. This is what the StringBuilder is for.

    Append the arguments to the string builder, then call toString() and trim on it when done.