[Solved] Some problems with Colors and Messaging MessageReceivers

Hi there :slightly_smiling:

I am having some difficulty working with Colors in text. I understand that the Sponge team has provided the Text builder API, however, for my use, this wont work.

There are a couple things that are causing me some difficulty. For instance if i try to create a message with color in it by hand (that is, without using the text builder API), i try this

Text.of("§bTest Message");

When i send this to both the console and a player, it is outputting “bTest Message”.

Is there any way to do this by hand, or do i have to figure out a way around this?

Secondly, I cannot seem to find a way to grab a TextColor without parsing the name of the color i want by hand (meaning, there is no TextColor.valueOf(String) or the equivalent) Am i looking at this the wrong way? I am so confused :frowning:

Thanks for reading :slightly_smiling:

The §-based color codes have been deprecated by Mojang. Use Text.builder("Insert Message Here").color(TextColors.INSERT_COLOR_HERE).build().

All the TextColor values are available in the similarly-named class called TextColors. Sponge often does stuff like this.

An equivalent to hard-coded colors (but the builder is recommended!! :slight_smile: ):

Text text = TextSerializers.formattingCode('&').deserializeUnchecked("&bTest Message");

or:

Text text = Text.of(TextColors.BLUE, "Test Message"); //Shortcut builder method thing

An equivalent to valueOf():

String color = ...
 //Not present if 'color' is not a registered color
Optional<TextColor> colorOptional = Sponge.getRegistry().getType(TextColor.class, color);

Note that this works for ANY catalouge type, such as ItemTypes:

String itemType = ...
 //Not present if 'itemType' is not a registered item type
Optional<ItemType> itemOptional = Sponge.getRegistry().getType(ItemType.class, itemType);
1 Like

For more information, please see our lovely section about this on the SpongeDocs!

2 Likes

I’m guessing the reason you can’t use the TextBuilder is that this the is loaded, for example, from a config?

As mentioned, best thing to use in that case is the TextSerializers. formattingCode will use the old coded system, however you can also use TEXT_XML or JSON to use the full depth of the new system, it’s just slightly less usable then the old system due to all the new features.

1 Like