SpigotMC / BungeeCord

BungeeCord, the 6th in a generation of server portal suites. Efficiently proxies and maintains connections and transport between multiple Minecraft servers.
https://www.spigotmc.org/go/bungeecord
Other
1.58k stars 1.11k forks source link

Constructor TextComponent(String text) breaks rgb color codes #3095

Open LaserSlime opened 3 years ago

LaserSlime commented 3 years ago

The TextComponent(String text) constructor only works with regular color codes, but not with rgb color codes like TextComponent.fromLegacyText() does.

TextComponent component = new TextComponent("test"); component.setColor(ChatColor.of(new Color(0x69420))); ping.setDescriptionComponent(new TextComponent(component.toLegacyText()));

I know this code is weird. It's only for demonstration. The message sent to the player will be black instead of green. The problem with using TextComponent.fromLegacyText() is that it returns a BaseComponent[] which can't be used in for example MOTD's. To fix that I would need to put it in another TextComponent constructor which looks really messy.

TextComponent component = new TextComponent("test"); component.setColor(ChatColor.of(new Color(0x69420))); ping.setDescriptionComponent(new TextComponent(TextComponent.fromLegacyText(component.toLegacyText())));

Again, I know this code doesn't make any sense, It's just for demonstration. I need to do this since I want to automatically center my motd and I need to know which characters are bold and I can only do that with legacy text (As far as I'm aware), but I don't want to use the legacy text methods since they are deprecated and might get removed in the future.

There are two ways to fix this problem:

  1. Parse RGB codes in the TextComponent(String text) constructor.
  2. Add a method that accepts BaseComponent[] to all component related methods including MOTD's.
md-5 commented 3 years ago
  1. Is definitely not the correct fix, see https://www.spigotmc.org/wiki/the-chat-component-api/#common-pitfalls

For 2., Can't you just use ComponentBuilder? You should always be using ComponentBuilder rather than individually making components.

LaserSlime commented 3 years ago

For 2., Can't you just use ComponentBuilder? You should always be using ComponentBuilder rather than individually making components.

Yes, but ComponentBuilder returns a BaseComponent[] which I can't use in MOTD's. The only way to get around this would be to create a TextComponent() and pass the BaseComponent[] as a parameter (As suggested in https://www.spigotmc.org/threads/rgb-colored-motd-using-componentbuilder.503842/#post-4167435), which like I said is really messy and shouldn't be necessary.

md-5 commented 3 years ago

Probably ComponentBuilder needs a .buildSingle method, but ultimately it would just internally do what you call 'messy' which is correct.

LaserSlime commented 3 years ago

Can't you just add a method that accepts a BaseComponent[] for setting the server motd?

md-5 commented 3 years ago

Can't you just add a method that accepts a BaseComponent[] for setting the server motd?

Not unless the Mojang client supports that, which given the method was never added in the first place, it likely doesn't.

luchs888 commented 10 months ago

Hey, I would like to add a question to this.

With legacy text this would work with hex code:

player.sendMessage(text.replaceAll("&", "§"));

but not with a TextComponent:

player.sendMessage(new TextComponent(text.replaceAll("&", "§")));

This raises the question of why TextComponent does not support Hex-code? It wouldn't be the best solution, but it would be one way of dealing with it.

md-5 commented 10 months ago

Because that's a common pitfall https://www.spigotmc.org/wiki/the-chat-component-api/#common-pitfalls

You need to use a component builder and fromlegacy