Use Minecraft color codes in my plugin config

What are you trying to do? If you want to parse an XML formatted string, you need to supply a valid one (valid as in parses against this XSD).

If you want to parse legacy color codes, you need to use Texts.legacy().of("This string has §bcolors!")).

I think there might be confusion about this.

  • Using formatting codes (§) is OUTDATED, but if you want to use it in plugins, use Texts.legacy().from("§aHello §eWorld!"), or if you have a custom formatting character (like &), use Texts.legacy('&').from("§aHello §eWorld!"). Your IDE or compiler will give you a warning that the method is deprecated; this is expected, as you should not be doing this.
  • You can use XML-based formatting by using Texts.xml().from("<color name=green>Hello</color> <color name=yellow>World!</color>"). This is the recommended way to do formatting with new plugins. This does not work with formatting codes.

I’m aware how (§) works. I just didn’t understand the xml-based formatting.

src.sendMessage(Texts.xml().from("<color name=yellow>" + message + "</color>"));

Tried this and still got error.

org.spongepowered.api.util.TextMessageException: Error parsing TextXML message '<span><color name=yellow>test</color></span>'
...
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 19; Open quote is expected for attribute "name" associated with an  element type  "color".

I wasn’t sure whether the quotes were needed. Try Texts.xml().from("<color name=\"green\">Hello</color> <color name=\"yellow\">World!</color>"). If you’re reading the string from somewhere other than Java source, remove the backslashes (\ , NOT /).

Given that the value is going to be loaded from config, rather than relying on the character escape in the actual config, I’d do something like this:

Texts.xml().from(str.replaceAll("\"", "\""));

The first \" looks for quotations, then replaces it with an escaped quotation.

But, really - why are you using XML? It has its uses, but it seems like you’d be much better off foregoing that.

@JBYoshi
Ah its working. Thanks

@FerusGrim
Just weighing my options. I know it’ll be quite a while before the standard color codes are fazed out, but want to be prepared when they do.

Have you heard that this might be happening?

Well isn’t that why Texts.legacy() is deprecated because mojang is getting rid of the standard formatting system? From what I can tell, this method will no longer function after that, in which case we’ll need something to identify color in config’s and chat. Maybe I’m misunderstanding the situation.

I’m unfamiliar with XML so does anyone know how to use the onClick, onHover attributes in this:

https://github.com/SpongePowered/SpongeAPI/blob/master/extra/text_xml_schema.xsd#L32

The file you linked is a XML Schema that describes the elements that may be used in a valid XML file.

Based in the schema I generated a demonstration file filled with blank data using Intellij Idea. It should give you an idea on how to correctly use XMLs as the properties are named rather clearly.

The problem is I don’t know what to put in for a string. I get invalid click and hover handler errors.

<color onHover=???? name="RED">TEST</color>

If I’m right, this is an example.

Sorry, I did not recognize that those are not covered by the schema.

As of writing there does not appear to be any documentation available. However a corresponding docs issue links to this Bukkit issue witch explains how onHover etc. work (on Bukkit this change was never implemented but it was picked up by Sponge developers).

early i use Minecraft color codes manually.
thank you. now i love to use plugin.