Need Help With Simple Code

Discussion in 'Plugin Development' started by Techno360!, Aug 28, 2018.

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



    I'm a quite new developer, still learning the basics.
    How do I make a command that, when /bc <message> is typed. It will broadcast the message (with color codes). I'm not sure how to make an argument a variable.

    Code: (this is after Main Class has routed /bc to BroadcastCommand which is this file):

  2. Offline


  3. @Techno360!
    Also as an addition if you don't know how to get all of your message into 1 string you have to use a StringBuiler like so
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < args.length; i++)
      sb.append(args[i]).append(" ");
    String message = sb.toString().trim();
    and then you can do ChatColor.translaterAlternateColorCodes('&', message);
  4. Offline


    Thanks, guys.
    It works but I don’t understand how that code makes a string an argument. Is there a logical explanation somewhere?
    Also would I do
    ChatColor.translateAllColorcodes(‘&’, mesage);
  5. Offline


    If you want color codes then yes, do:
    1. ChatColor.translateAlternateColorCodes('&', message);
  6. @Techno360!
    In your Command method after Player p add , String message
    Then when you call your method. Use the string you just made
  7. Offline


    What the string builder does it creates a string out of other string, so it builds one.
    In your for loop:
    for(int i = 0; i < args.length; i++)
    You are telling the code to carry on only while the temporary variable called i is Les then how many arguments you put.
    In that for loop, you have this
    s.append(args + " ");
    What this does it it gets the argument at the value of i, starting at index 0 (Java starts at 0) and adding it to the string builder with a space.
    ChatColor.translateAlternateColorCodes('&', s.toString();
    Will allow the user to use the & symbol to add colours and effects. Hope this helps :)
    (PS this was written from my phone XD)

    EDIT: s in this case is your string builder object.
  8. Offline


  9. @Techno360!
    p.sendMessage(c.h("Announcement") + ChatColor.translateAlternateColorCodes('&', message));
  10. Offline


    As Blackwing_Forged said above, use 'ChatColor.translateAlternateColorCodes()' like you use 'ChatColor.AQUA', from there you can color the message with '&'
  11. Offline


    Oh that makes more sense. I’ll try this in a few days.
  12. Offline


    It worked, thanks guys!
  13. Offline


    Glad to know you got it working. :)
Thread Status:
Not open for further replies.

Share This Page