Configuration Format

SpongeAPI offers support to serialize text directly to a Configurate configuration file through the use of the TypeToken class. Text objects are saved using the same node structure as the Text’s JSON 格式, but in a more flexible format called Human-Optimized Config Object Notation (HOCON).

小訣竅

For information on how to use Configurate to create configuration files for your plugin, please refer to 設定插件.

For example, the text "Hello World!", formatted with the color red and an underline, would have the following HOCON representation:

{
    underlined=true
    color=red
    text="Hello World!"
}

To save a Text object simply set the value of your desired node using the following code:

import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.format.TextStyles;

ConfigurationNode node = loader.load();
Text text = Text.of(TextColors.RED, TextStyles.UNDERLINE, "Hello World!");
node.getNode("mytext").setValue(TypeToken.of(Text.class), text);
loader.save(node);

You can then load a Text object using the following code:

Text text = node.getNode("mytext").getValue(TypeToken.of(Text.class));

備註

This strategy is not limited to HoconConfigurationLoader; any ConfigurationLoader will suffice.