Solved ChatComponentAPI color codes to JSON colors

Discussion in 'Spigot Plugin Development' started by iDoomfull, Jul 25, 2018.

Thread Status:
Not open for further replies.
  1. Hello, I am stuck with some code and I don't know how to convert color symbols like "&6" to JSON format. I tried with fromLegacyText, but it returns BaseComponent and I don't know how to apply it to my code block. Have a look for yourselves:
    Code (Java):
                    TextComponent message = new TextComponent(texts.get(events.get(index)));
                    for(String parameter : parameters.get(events.get(index))) {
                        if (parameter.contains("show_text") && message.getHoverEvent() == null) {
                            message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                                    new ComponentBuilder(parameter.replace(">", "").split("=")[1]).create()));
                            message.setText(texts.get(events.get(index)).replace("<" + parameter + "> ", ""));
                        if(message.getClickEvent() != null) continue;
                        if(parameter.contains("open_url")) {
                            message.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, parameter.replace(">", "").split("=")[1]));
                        if(parameter.contains("run_command")) {
                            message.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, parameter.replace(">", "").split("=")[1]));
                        if(parameter.contains("suggest_command")) {
                            message.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, parameter.replace(">", "").split("=")[1]));
                        message.setText(texts.get(events.get(index)).replace("[" + events.get(index) + "]", ""));
    I have these if statements in my one-line configurable JSON events that check what sort of event does the string perform, the events work and all, but the strings I input in the ClickEvents contain color codes and I don't know how to convert those to JSON color format
  2. TextComponent#fromLegacyText(String) or something like that.
  3. It returns BaseComponent and I can't use that inside ClickEvent, that's the problem, I don't know where
  4. Are you using the NMS or the bungeecord api?
  5. I use the Bungeecord ChatComponentAPI yeah
  6. A TextComponent is a BaseComponent.......

    That's like saying "I need a vehicle, not a car!"
  7. I can't reinitialise the "message" TextComponent variable with it tho
  8. You know that click event does not support formatted text?
    Code (Text):
    clickEvent: JSON object - Defines an event that occurs when this component is clicked. Contains an action key and a value key. value is internally handled as a String, although it can be any type of JSON primitive.
    This is also the reason that a click event in the API will only accept a string:
    Code (Java):
    message.setClickEvent( new ClickEvent( ClickEvent.Action.OPEN_URL, "" ) );
  9. I don't think you fully understand this API.. a TextComponent is a single thing. If you add color/style to it it becomes multiple things. That's why fromLegacyText returns a BaseComponent[] array...
  10. The how would I go about it?
  11. I see my post is getting ignored thus i will tell it once again: click events don't support formatted text, so what you want is impossible.
    • Informative Informative x 1
  12. Ok, I finally got it, it's obvious you can't use color codes in click events such as RUN_COMMAND and I am stupid for for getiing it earlier, but now I want to know only one thing: how would I color the text shown in hover events then? I tried this
    Code (Java):
                            message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
    but it doesn't convert &6 to color gold
  13. He wants the clickable text to be formatted, not the event itself... Calm down.

    You have to realize your full message is a collection of components, each one with it's own style/color/event.

    For example like "This is link" is 3 components. The 2nd one having a click event. Etc etc.
    • Friendly Friendly x 1
  14. I got it to work eventually! Here's how I did it:

    Code (Java):
                            message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                                    TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', string)));)));
    • Like Like x 1
Thread Status:
Not open for further replies.