Solved Custom Broadcast plugin has no spaces in messages being sent.

Discussion in 'Plugin Development' started by Dragonfire967, Jun 14, 2016.

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

    Dragonfire967

    I am trying to make a small plugin for my server (CraftBukkit 1.8.8) to broadcast a simple custom message to the server when someone donates on our website.

    The plugin itself works in concept, except for one issue. It won't include ANY whitespace / spaces in the message, so "hello world" it broadcasted as "helloworld" which is not what I want.

    This is my first time trying to program using java, or for bukkit plugins in general, so I am still VERY new to this. Most articles I see regarding broadcasts don't touch on my issue, so any help would be appreciated.

    Code:
    Show Spoiler
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class DonateBroadcast extends JavaPlugin {
       
        public void onEnable() {
           
            System.out.println("DonateBroadcast enabled.");
           
        }
       
        public void onDisable() {
           
            System.out.println("DonateBroadcast disabled.");
       
        }
       
        public boolean onCommand(CommandSender sender, Command command, String cmd, String[] args) {
            if(cmd.equalsIgnoreCase("dbcast")) {
                if((sender instanceof Player)) {
                    sender.sendMessage("This command must be executed by the console");
                } else {
                    if(args.length == 0) {
                        System.out.println(ChatColor.DARK_RED + "You need to type in a message!");
                    } else {
                        getServer().broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GREEN + "WEB" + ChatColor.DARK_GRAY + "] " + ChatColor.GOLD + message(args));
                    }
                   
                }
            }
            return false;
        }
       
        public String message(String[] args) {
            StringBuilder builder = new StringBuilder();
            for(int i = 0; i < args.length; i++)
            builder.append(args[i]);
            builder.append(" ");
            return builder.toString();       
        }
    }
    


    Please note that I coded this following a tutorial on youtube.

    Attached is a screenshot showing what the plugin is outputting when I type in console:

    /dbcast Testing Message System

    2016-06-14_17.21.27.png
     
  2. Offline

    Zombie_Striker

    @Dragonfire967
    This is a simple encapsulation problem:
    Code:
         for(int i = 0; i < args.length; i++)
            builder.append(args[i]);
            builder.append(" ");
    You see how you are missing a bracket at the end of the for loop? That means it will Only read the next line. Encapsulate and your problem will be solved.
     
  3. Offline

    Dragonfire967

    so basically:

    Code:
    for(int i = 0; i < args.length; i++)
            {
                builder.append(args[i]);
                builder.append(" ");
            }
    
    or did I misunderstand what you said?

    EDIT: just tried that, and it works now. Thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page