Solved Alternative to ChatColor.translateAlternateColorCodes

LABSTORM

New member
Oct 10, 2023
2
0
1
I have a config file wich looks something like this:

YAML:
# colour codes start with &
# variables are enclosed by %
Messages:
  NoPermission: '&4Denied access! You are not allowed to do this.'
  Join: '&a>>&7 %Player%'
...

In my old code I just used
Java:
ChatColor.translateAlternateColorCodes('&', config.getString("Messages.Join"))
to give me a String with colour information on which I later used String#replace to change the '%Player%' to the actual player name...
Now I want to use components.
I know how to make colourful components, but I don't know how the user could set the colours easily in the config file.
Can you give me a hint how this could be implemented?
 
Version Output
1.20.2

electronicboy

Administrator
Staff member
Dec 11, 2021
227
10
38
28
We recommend using minimessage for config files, in part because it supports modern features in a way which is cross platform, but also the fact that it's generally seeing more adoption over the past year or two, so people are becoming familiar with it as-is, and it supports stuff like placeholders, etc

 

stefvanschie

Moderator
Staff member
Dec 17, 2021
102
3
16
18
I have a config file wich looks something like this:

YAML:
# colour codes start with &
# variables are enclosed by %
Messages:
  NoPermission: '&4Denied access! You are not allowed to do this.'
  Join: '&a>>&7 %Player%'
...

In my old code I just used
Java:
ChatColor.translateAlternateColorCodes('&', config.getString("Messages.Join"))
to give me a String with colour information on which I later used String#replace to change the '%Player%' to the actual player name...
Now I want to use components.
I know how to make colourful components, but I don't know how the user could set the colours easily in the config file.
Can you give me a hint how this could be implemented?
Take a look at the LegacyComponentSerializer. This can turn your text into a component.
 
  • Sad
Reactions: 4drian3d

Silal

New member
Feb 13, 2024
4
0
1
If the Problem isn`t solved, i would solfe it like this:

Component comp = Component.text(config.getString("Messages.Join").replace("&", "§"));

If you also want to replace %player% with the playername:

String playername = "lol"; Component comp = Component.text(config.getString("Messages.Join").replace("&", "§").replace("%player%"), playername);

I hope this solves your Problem :D
 

4drian3d

Member
Jan 5, 2022
60
4
8
Perú
If the Problem isn`t solved, i would solfe it like this:

Component comp = Component.text(config.getString("Messages.Join").replace("&", "§"));

If you also want to replace %player% with the playername:

String playername = "lol"; Component comp = Component.text(config.getString("Messages.Join").replace("&", "§").replace("%player%"), playername);

I hope this solves your Problem :D
That is a terrible use of components, you must use the LegacyComponentSerializer if you want to use that deprecated format or you can migrate to MiniMessage