Solved Stringbuilder issues

Discussion in 'Plugin Development' started by KarimAKL, May 25, 2018.

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

    KarimAKL

    I have just made this little plugin where i want arguments as one message, example: "/command Hey, my name is whatever and i want to bake a cake" then that whole message should be sent instead of just "Hey," or something like that. :p
    Current code:
    Code:Java
    1.  
    2. public class Commands implements CommandExecutor {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public Commands(Main plugin) {
    8. this.plugin = plugin;
    9. plugin.getCommand("command").setExecutor(this);
    10. }
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    14. if (cmd.getName().equalsIgnoreCase("command")) {
    15. if (sender instanceof Player && !sender.hasPermission("command.use")) {
    16. sender.sendMessage(Color.here("&cYou do not have permission to use this command!"));
    17. } else {
    18. if (args.length == 0) {
    19. sender.sendMessage(Color.here("&cYou need to put a message!"));
    20. return true;
    21. } else {
    22. StringBuilder sb = new StringBuilder();
    23. for (int i = 0; i < args.length; i++) {
    24. sb.append(args).append(" ");
    25. }
    26. String message = sb.toString().trim();
    27. if (!(sender instanceof Player)) {
    28. Bukkit.getServer().getConsoleSender().sendMessage(Color.here("Console: "+message));
    29. Bukkit.broadcast(Color.here("Console: "+message), "message.see");
    30. return true;
    31. } else {
    32. Player p = (Player) sender;
    33. Bukkit.getServer().getConsoleSender().sendMessage(Color.here(p.getName()+": "+message));
    34. Bukkit.broadcast(Color.here(p.getName()+": "+message), "message.see");
    35. return true;
    36. }
    37. }
    38. }
    39. }
    40. return false;
    41. }
    42. }
    43.  
    44.  

    When i do this: "command ajidsfn" in the console the message comes like this: "Console: [Ljava.lang.String;@779abb3e" the "779abb3e" is different everytime i execute the command but if i were to do this: "command ajknsdf jkansjdkf najskdfn" then the message would be "Console: [Ljava.lang.String;@779abb3e [Ljava.lang.String;@779abb3e [Ljava.lang.String;@779abb3e" (For every argument) this happens for both players and the console, not only console. The code comes with no errors and when i load the plugin it comes with no errors aswell, it's only the message that isn't what it's supposed to be. :7
     
  2. Online

    timtower Ninja on the waves Moderator

    Changed title.
    @KarimAKL Don't append the full args (line 24) append args
     
  3. Offline

    KarimAKL

    @timtower Thank you, it works now. :) EDIT: Just noticed, thank you for changing the title. :p
     
Thread Status:
Not open for further replies.

Share This Page