MOTD: !!java.lang.StringBuilder {} PROBLEM!!

Discussion in 'Plugin Development' started by johnny boy, Jan 5, 2017.

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

    johnny boy

    I have made a /motd command. The commands work fine, but when I start up my plugin it is saying the motd is !!java.lang.StringBuilder {} when it should say what I set my default config to. Please point me in the right direction! (Code)
    Code:
    package me.memplexowner.motd;
    
    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 MotdChange implements CommandExecutor {
       
        MainClass config;
       
        public MotdChange(MainClass config) {
            this.config = config;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Sorry! Only players can use this command!");
                return false;
            }
            Player player = (Player) sender;
            if (args.length == 0) {
                String motd = config.getConfig().getString("MOTD");
                System.out.println(motd);
                String motd2 = ChatColor.translateAlternateColorCodes('&', motd);
                player.sendMessage(motd2);
                return true;
            } else {
                if (args[0].equalsIgnoreCase("help")) {
                    player.sendMessage(ChatColor.GOLD + "MOTD Help");
                    player.sendMessage(ChatColor.BLUE + "/motd help " + ChatColor.GRAY + "- Shows help for motd.");
                    player.sendMessage(ChatColor.BLUE + "/motd "+ ChatColor.GRAY + "- Shows the motd.");
                    player.sendMessage(ChatColor.BLUE + "/motd set <Message> "+ ChatColor.GRAY + "- Allows you to set a new MOTD.");
                    return true;
                } 
                if (args.length >= 2) {
                    StringBuilder motd = new StringBuilder();
                if (args[0].equalsIgnoreCase("set")) {
                    for (int x = 1; x < args.length; x++) {
                        String motdMessage = args[x] + " ";
                        motd.append(motdMessage);
                    }
                    config.getConfig().set("MOTD", motd);
                    String motdNoColour = config.getConfig().getString("MOTD");
                    String  motdColour= ChatColor.translateAlternateColorCodes('&', motdNoColour);
                    player.sendMessage("MOTD set to: " + motdColour);
                    config.getConfig().options().copyDefaults(true);
                    return true;
                        } else {
                            player.sendMessage("You have not put the MOTD");
                            return false;
                        }
                    }
                }
            player.sendMessage(ChatColor.RED + "Error! Do /motd help for help setting your motd!");
                return false;
        } 
     }
    
    
    
           
    
    
       
       
    
    
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    johnny boy

    The problem still persists.. Here is updated code :p

    Code:
    package me.memplexowner.motd;
    
    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 MotdChange implements CommandExecutor {
     
        MainClass config;
     
        public MotdChange(MainClass config) {
            this.config = config;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Sorry! Only players can use this command!");
                return false;
            }
            Player player = (Player) sender;
            if (args.length == 0) {
                String motd = config.getConfig().getString("MOTD");
                System.out.println(motd);
                String motd2 = ChatColor.translateAlternateColorCodes('&', motd);
                player.sendMessage(motd2);
                return true;
            } else {
                if (args[0].equalsIgnoreCase("help")) {
                    player.sendMessage(ChatColor.GOLD + "MOTD Help");
                    player.sendMessage(ChatColor.BLUE + "/motd help " + ChatColor.GRAY + "- Shows help for motd.");
                    player.sendMessage(ChatColor.BLUE + "/motd "+ ChatColor.GRAY + "- Shows the motd.");
                    player.sendMessage(ChatColor.BLUE + "/motd set <Message> "+ ChatColor.GRAY + "- Allows you to set a new MOTD.");
                    return true;
                }
                if (args.length >= 2) {
                    StringBuilder motd = new StringBuilder();
                if (args[0].equalsIgnoreCase("set")) {
                    for (int x = 1; x < args.length; x++) {
                        String motdMessage = args[x] + " ";
                        motd.append(motdMessage);
                    }
                    motd.toString(); // NEW LINE IS HERE < ---------------------------------------------------- >
                    config.getConfig().set("MOTD", motd);
                    String motdNoColour = config.getConfig().getString("MOTD");
                    String motdColour = ChatColor.translateAlternateColorCodes('&', motdNoColour);
                    player.sendMessage("MOTD set to: " + motdColour);
                    config.saveConfig();
                    return true;
                        } else {
                            player.sendMessage("You have not put the MOTD");
                            return false;
                        }
                    }
                }
            player.sendMessage(ChatColor.RED + "Error! Do /motd help for help setting your motd!");
                return false;
        }
    }
    
    
    
         
    
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    Zombie_Striker

    @MemeplexOwner
    The .toString method does not make the MOTD a string, it returns the string from the stringbuilder.
     
  6. Offline

    johnny boy

    I really don't understand what your trying to do. Debug?
     
  7. Offline

    0verFull

    @MemeplexOwner

    Change your line:
    Code:
    config.getConfig().set("MOTD", motd);
    By:
    Code:
    config.getConfig().set("MOTD", motd.toString());
    Explanations:

    In fact, "motd" is a StringBuilder object so when you use "config.getConfig().set("MOTD", motd);" you set the "MOTD" 's path in the config file by the StringBuilder object.
    To set the MOTD's path in the config file by the string of the StringBuilder you have to use StringBuilder#toString() which return a String, the String of your StringBuilder.
     
    Last edited: Jan 5, 2017
    Zombie_Striker likes this.
  8. Offline

    johnny boy

    Works. Thank you so much! I can consider my project done. (And I understand now why it didn't work!!).
     
  9. Offline

    Zombie_Striker

    @MemeplexOwner
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page