Use Minecraft color codes in my plugin config

Hey guys,
I know how to create a plugin which can load data from an external config file. My Problem is, that i want to use color Codes like &e … in my config file, because this is very uncomfortable:

player.sendMessage(Texts.builder(sql.getMotd()).style(TextStyles.NONE).color(TextColors.WHITE).build());

Thats right. I don’t use a config file here (i use a mysql database), but i think, that this doesn’t make any difference when it comes to the color codes :).

Second question: How can I use line breaks in my “config” ?
Thanks,
Greets :slight_smile:

The old color codes are supported still through player.sendMessage(Texts.legecy(message, "§e"));
but it will be removed from the API soon.
Better you use player.sendMessage(Texts.of(TextColors.Yellow + "Test Message"));

1 Like

Yeah, but my problem is, that i only get .fromlegacy and .tolegacy. i have sponge 2.0 in my external libs, is that the problem ? greets

Use Texts.legacy(char). Or even better, use the XML format to specify the formatting.

The answer to your second question depends on the configuration format you are using.

For HOCON you need to triple quotes as documented here:

"""This string
will have multiple
lines."""

For JSON you need to actually write the \n (depending on the parser you might need to add an additional escape sequenze \\n):

 "This string\n
 will have multiple\n
 lines."

For YAML, you need to use a pipe (alternatively you can use a >):

|
This string
will have multiple
lines.
1 Like

I don’t know if you have seen my answer, but my problem is, that my IDE does not ‘know’ a .legacy method :frowning:

Update your SpongeAPI library to the current version (as of writing, 2.1-SNAPSHOT).

Yep, thats an option. You can even overdo this and replace single parts of the text with ifs, but thats too much work ^^

Use better gradle then i can help you more :smiley:

1 Like

Ok, i importet it now, but now intellij says, .legacy is outdated ?!

Yes that what i said it will be removed soon.
So better stick with that player.sendMessage(Texts.of(TextColors.Yellow + "Test Message"));

…,but how can I load colors with &e … when i use this ? Can you give me an example please ? :smiley:

You can actually not, you need to do this what i said.
For example:

player.sendMessage(Texts.of(TextColors.Gray + "[" + TextColors.Blue + "Minigame" + TextColors.Gray + "] " + TextColors.Red + "You have been killed."));

i could go even crazier with bold and underlined and such but that is for now enough.

Ok, thats no problem, my main problem is, that i want to change the colors with a config :smile:
But thanks ^^

if(config.getNode("Color").getValue.equals("Red"){
      player.sendMessage(Texts.of(TextColors.Red + "You have been killed."));
}

No, please do not do that. As far as I know, Texts.legacy(char) (and its overloads) are not likely to be removed soon. They are deprecated because Mojang is phasing out formatting characters, but still provided by Sponge because some existing (Bukkit) plugins still rely formatting characters and need a way to parse them.

For new plugins it is recommended to use the XML TextRepresentation that Sponge bundles: Texts.xml().from(String). As of writing, this way is not yet covered by the documentation. Still it should be pretty obvious from reading this commit. An official XSD file to validate the formatting XML is available here.

can u say me, why

player.sendMessage(Texts.legacy(sql.getMotd(), "§e"));

gives me “cannot resolve method…” ?

Adding to @RafVDL’s answer, you are also using Texts wrong. Try Texts.legacy().of(sql.getMotd()).

No, there’s a method that sends the given message to chat:

1 Like

How is this used? I ran a simple test and I got an error

src.sendMessage(Texts.xml().from("&4test"));

org.spongepowered.api.util.TextMessageException: Error parsing TextXML message '<span>&4test</span>'
...
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 8; The entity name must immediately follow the '&' in the entity reference.

I think that It only uses entity names from provided schema, not exact color codes from Minecraft.