Including spacing in cmd args

Discussion in 'Plugin Development' started by Dolphindalt, Jul 8, 2015.

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

    Dolphindalt

    So I have this code here and I'm trying to filter through my args and add spacing but it doesn't work.


    Code:
    package me.dalton.utils;
    
    import me.dalton.MyEssentials;
    
    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 ChatUtilities implements CommandExecutor {
      
        MyEssentials plugin;
      
        public ChatUtilities(MyEssentials passedPlugin) {
            this.plugin = passedPlugin;
        } //Give instance of class
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("broadcast")) {
                     if (!sender.hasPermission("myessentials.broadcast")) {
                             sender.sendMessage(ChatColor.RED + "You are not permitted to do this!");
                             return true;
                    }
                    if (args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Please specify a message!");
                        return true;
                    }
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        args.toString();
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; i < args.length; i++) {
                        builder.append(args[i]);
                        builder.append(" ");
                        player.sendMessage(ChatColor.RED + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.RED + "] " + ChatColor.GREEN + args[0]);
                        return true;
                    }
                }
          
            }
            return true;
        }
    
    } 
    I get an 'warning' for the i++ saying it is unused.
     
    Last edited: Jul 8, 2015
  2. Online

    timtower Administrator Administrator Moderator

    @Dolphindalt That is a warning, not an error, and try hitting this:
    ctrl-a, ctrl-i, then check hat is executed when, because I think that you are returning early
     
  3. Offline

    meguy26

    @Dolphindalt
    You are returning early....

    this:
    Code:
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        args.toString();
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; i < args.length; i++) {
                        builder.append(args[i]);
                        builder.append(" ");
                        player.sendMessage(ChatColor.RED + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.RED + "] " + ChatColor.GREEN + args[0]);
                        return true; //inside for loop! this will cause the for loop to only execute once
                    }
    should be this:
    Code:
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        args.toString();
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; i < args.length; i++) {
                        builder.append(args[i]);
                        builder.append(" ");
                        player.sendMessage(ChatColor.RED + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.RED + "] " + ChatColor.GREEN + args[0]);
                    }
        return true; //return AFTER the for loop is executed

    also, quick typing @timtower
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    Dolphindalt

    Oh, I did return early. But now whenever the message is sent instead of spacing the args it places them on another line.
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    Dolphindalt

    I got it to work, thanks.
     
  8. Offline

    meguy26

    @timtower @Dolphindalt
    well guess what? he's also sending the wrong variable in the broadcast.
    Code:
    player.sendMessage(ChatColor.RED + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.RED + "] " + ChatColor.GREEN + args[0]);
    should be:
    Code:
    player.sendMessage(ChatColor.RED + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.RED + "] " + ChatColor.GREEN + builder.toString();
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page