TextColor and TextStyle deserialization

Is there currently a way to deserialize a TextColor or TextStyle from a String? I see no utility methods inside Texts, TextColors, nor in TextStyles. Is this in Sponge somewhere, and if not is it planned to be added?

Probably not, but my solution is to convert tranditional &# to §# symbols to set the colors in the string in configuration file and convert the string to Text using Texts.of(...). I am not 100% sure if it’ll work or not. I have not tested it yet, but assumed it may.

Ah well I know how to manually do it, was just hoping for a quicker integrated solution.

Take a look at the gameregistry’s getType method. This would look something like:

@Inject private Game game;

public Optional<TextColor> getColor(String name) {
    return game.getRegistry().getType(TextColor.class, name.toUpperCase());
}

to get a TextColor by name.

2 Likes

Odd ? http://prntscr.com/7f7w1u

Thanks btw, I guess I’ll have to learn about catalogues in sponge!

Solution:

public Optional<TextColor> getColor(String name) {
    return Optional.of((TextColor) game.getRegistry().getType(TextColor.class, name.toUpperCase()));
}

Can you show an example of this in action? For example say you have the string ${RED}This is red text".

That won’t work because you cannot cast an Optional to a TextColor
The following code works for me:

public Optional<TextColor> getColor(String name) {
    return game.getRegistry().getType(TextColor.class, name.toUpperCase());
}

If it doesn’t for you maybe you just need to clean and rebuild (sometimes that fixes weird things in eclipse).

Woah yeah you’re totally right! Yeah I’ll try to clean the project- intellij though. Every IDE has their odd bugs.