Broadcast command only uses one argument

Discussion in 'Plugin Development' started by CarstenBMG, Apr 8, 2017.

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

    CarstenBMG

    Hello Guys, I want to have a command you can use to broadcast a message to all online players. It's working and everything, but it only broadcasts one argument of the command.
    Here is the code:
    Code:
     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
          if (cmd.getName().equalsIgnoreCase("broadcast")) {
              if (sender.hasPermission("adjustments.broadcast")) {
                  if (args.length != 0) {
                      String message = "";
                        for (String part : args) {
                            if (message != "") message += " ";
                            message += part;
                            Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.DARK_GREEN + "Yggdrasil" + ChatColor.WHITE + "] " + ChatColor.BOLD + ChatColor.DARK_PURPLE + message);
                            return true;
                        }
                  }
                  return false;
              }
              sender.sendMessage(ChatColor.WHITE + "[" + ChatColor.DARK_GREEN + "Yggdrasil" + ChatColor.WHITE + "] " + ChatColor.BOLD + ChatColor.DARK_PURPLE + "You have no permission to make a broadcast.");
          }
    return true;
    ]
    Can you help me? Thx.
     
  2. Offline

    martian3333

    @CarstenBMG
    You have a return statement that executes during the first successful iteration of your for loop.

    Code:
                            Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.DARK_GREEN + "Yggdrasil" + ChatColor.WHITE + "] " + ChatColor.BOLD + ChatColor.DARK_PURPLE + message);
                            return true;
    That should be moved outside of the for loop.
     
  3. Offline

    Edvio

    You could use StringUtils.join to make this easier.
     
Thread Status:
Not open for further replies.

Share This Page