[SOLVED] Broadcasting w/ args error!

Discussion in 'Spigot Plugin Development' started by SmashDev, Jul 12, 2015.

  1. Hi, I am new to spigot/bukkit and still learning Java but there is one problem with a plugin that I have made!
    So say when I do /bc hi my name is smash it outputs this:
    Code (Text):
    [ChangeMe] [Ljava.lang.String;@3ecb91b8
    [ChangeMe] [Ljava.lang.String;@5b2709a4
    I have no clue why!
    Code (Text):
    package com.smash.utils;

    import java.util.logging.Logger;

    import org.bukkit.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    //import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public void onEnable() {
            logger.info("Plugin Enabled");
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            //boolean isPlayer = sender instanceof Player;
            //Player p = (Player) sender;
            //Player target = Bukkit.getServer().getPlayer(args[1]);
                    if(args.length == 1 ){
                        Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e[" + getConfig().getString("prefix") + "&e] " + args));
                        sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "Not enough args! Do /broadcast <Message!>");
                }else if(sender.hasPermission("serverutils.config")){
                        if(args.length == 1){
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&e&lConfig reloaded! &c&lThanks for using ServerUtils!"));
                            }else if(args[0].equalsIgnoreCase("CrateConfig")){
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&e&lConfig created! &c&lThanks for using ServerUtils!"));
            return false;
    I have setup the aliases properly in the plugin.yml so I can do /bc or /bcast!
    SO please help!
  2. Also I can only broadcast 1 word so I cant just do like:
    /bc Hi my name is smashlols
  3. args is an array of strings. You cant broadcast args as it is, you will need to construct a string from the contents of the array.
  4. So say I would need to do this:
    Code (Text):
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e[" + getConfig().getString("prefix") + "&e] " + args[0] + args[1] + etc...));
  5. Except you won't know how large args is. Construct the string before that line by using a StringBuilder and a for loop to iterate over args.
  6. so say like:
    Code (Text):
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e[" + getConfig().getString("prefix") + "&e] " + StringBuilder()));
    Like I said I am still learning java
  7. I recommend trying this.
    Code (Text):
    String message = "";
    for(String part : args)
    if(message != "") message += " ";
    message += part;
  8. How would I implement this to my code?
    And also how would I set the prefix?
  9. If I understood correctly, you are trying to broadcast what you write. I did a simple for loop which can do that. Of course, this loop is to be used in the onCommand boolean.
  10. So now I can use args as a whole or what ever?
  11. will do some StringBuilder ..
    Code (Text):
    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < args.length; i++) {
    builder.append(args[i] + " ");

    String msg = builder.toString();

    // you may now do the
    • Agree Agree x 2
  12. Not exactly. My code hasn't interpreted what you write after the command as arguments, rather as parts of a String. This way is, in my own personal opinion much simpler than having to define each individual argument just to be able to broadcast them.
  13. I'm suggesting you to give .. a beginner a simple code at start
  14. Learn java.
    • Agree Agree x 2
  15. Thanks for your help! It is now working!
    • Like Like x 1
  16. .I think he might know java because he is making a plugin ! ;; he is confused not lack of knowledge
  17. Also my config isnt saving! So I if you look at the code at the top! Please help!
  18. at your onEnable()

    instead of using saveConfig();
    Code (Text):
    public void onEnable() {
    // do something
  19. In order to successfully save a config, in your onEnable you should either do getConfig().options().copyDefaults(true); and saveConfig();
    or just do saveDefaultConfig();
  20. I'm pretty sure everyone who knows java knows you don't have to concatenate array's contents repetitively to get the contents of them.
    #20 NaJated, Jul 12, 2015
    Last edited: Jul 12, 2015
    • Agree Agree x 2