Solved Color code support

Discussion in 'Spigot Plugin Development' started by CreepersGamingMc, Jun 9, 2017.

Thread Status:
Not open for further replies.
  1. Hello, on my plugin, how would I add the ability to add color to the front of the MOTD with bukkit color codes such as &4
    My current code (if you do contribute help, please give an example such as adding it to my code, I am a beginner and am learning) also, if it would be possible to not have a plugin dependency?
    https://pastebin.com/BYFRCiSM
     
  2. ChatColor.translateAlternateColorCodes('char', "text")
     
  3. How would I put this into my current code?
     
  4. If you check the return type and read the function name it should tell you where to put it.
     
  5. Oh, ok!
     
  6. I'm still a bit confused where to put it, there are a LOT of ChatColor.'s
     
  7. Ok, so, you have a function. It returns a String, and it translates alternate color codes. That's literally what the function name is. Ok. It takes 2 inputs. A char, which needs to be '&' in your case since you want to translate '&' to color codes. And a String, which needs to be the text containing your characters that need to be converterted
    So in the end, this function will return your input text, but with the input character replaced with their respective color codes.
    Does that give you an idea where to put it?
     
  8. I'm a beginner, i'm terribly sorry, but I don't
     
  9. Ok, so in your onCommand(), you are sending a message from your config: getConfig().getString("message"), with the nontranslated color codes '&' if I am not mistaken. This needs to be the input String in translateAlternateColorCodes I mentioned before. So what you want to do is replace the getConfig().getString("message") with the String returned from translateAlternateColorCodes.
     
  10. Oh! So the code would be:
    Code (Text):
    ChatColor.translateAlternateColorCodes('&', "message")
    If i'm mistaken please correct me.
     
  11. Almost, because now you will simply get "message" when you send it to your player. You will need to replace that with getConfig().getString("message")
     
  12. I know I am being a bother but now I am confused how I do this? I'm sincerely sorry.
     
  13. *scrapes throat* Ok, here goes.

    translateAlternateColorCodes takes 2 inputs: char and String.
    char = the character to translate to color ('&' in your case)
    String = the text to translate. (The message from your config in your case)

    How to get the config message?
    Oh hey, getConfig().getString("message") returns a String, meaning it is a valid input for the String parameter in translateAlternateColorCodes.

    What will getConfig().getString("message") do? It returns the String written in your config under the name "message". Meaning if you had something like this:
    message: &6Hi
    getConfig().getString("message") would return "&6Hi".

    But wait thats wrong that needs to be translated.
    What if I just do this:
    ChatColor.translateAlternateColorCodes('&', getConfig().getString("message")) ?
    That would take my "message" string in my config, which is "&6Hi", and will replace "&6" with the appropriate
    chat color, which is gold.
    Now if I do String coloredMessage = ChatColor.translateAlternateColorCodes('&' getConfig().getString("message")),
    I have it in a String object meaning I can do whatever I want with it.
    So now my String coloredMessage is basically getConfig().getString("message"), but with the color codes translated, meaning we can now send it to a player with sendMessage().
     
  14. When I do this, a certain part is underlined with red
    Code (Text):
    '&' getConfig()
     
  15. Because you forgot to seperate the parameters with a comma.
     
  16. OMG THANKS!
     
    • Like Like x 1
  17. When I do /motd it doesn't do anything
     
  18. You need to learn Java before touching anything to do with the Spigot API, at least the very basics such as this. Do yourself a favour mate.
     
  19. Just add this to like a utilities class, or your main class, so whenever you wanna add color to text from any class such as commands, all you need to do is type (For example): player.sendMessage(color("&chello"));

    Code (Text):
    public static String color(String msg) {
    return ChatColor.translateAlternateColorCodes('&', msg);
    }
     
  20. All of these replies are just making me more confused, I know basic java but I can't find bukkit color codes ):
     
Thread Status:
Not open for further replies.