[Solved] UTF-8 in config.yml

Discussion in 'Spigot Plugin Development' started by AgentLV, Mar 30, 2015.

Thread Status:
Not open for further replies.
  1. How can I use UTF-8 symbols in my config,yml?
  2. Are you asking how to decode it, or how to convert the file to utf-8?

    If you would like to convert it:
    Code (Text):

    public void convertFile(File f, File newFile){
    try {
        FileInputStream fis = new FileInputStream(f);
        byte[] contents = new byte[fis.available()];
        fis.read(contents, 0, contents.length);
        String asString = new String(contents, “ISO8859_1″);
        byte[] newBytes = asString.getBytes(“UTF8″);
        FileOutputStream fos = new FileOutputStream(newFile);
    } catch(Exception e) {
    This is untested but should work. I will test it then update.
  3. I think decode. I want that users of my plugin can write UTF-8 symbols into the config. I will test the code.
  4. My above code simply converts an ANSI file to UTF-8. If that is ran then UTF-8 characters are placed in it, it should work fine. (Don't quote me on this, never had a reason to learn about file encoding in java)
  5. The problem is that if I have a config saved in UTF-8, I get a warning and this happens:[​IMG]
  6. Can you show me the warning?
  7. Code (Text):
    [17:23:40] [Server thread/WARN]: [NameManager] Default system encoding may have misread config.yml from plugin jar
  8. Do you have the § symbol in the config.yml? If so, replace it with & and turn the display string to ChatColor.translateAlternateColorCodes('&', configString);
  9. No and I already use ChatColor.translateAlternateColorCodes.
  10. Hmm, I'm only having this problem under windows.
  11. What's the encoding of the file? Also, try using \uXXXX instead of raw Unicode.
  12. Maybe you can use this yaml configuration class: (Didn't test it yet.)
    Code (Text):
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import org.apache.commons.lang.Validate;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    import com.google.common.base.Charsets;
    import com.google.common.io.Files;

    public class Utf8YamlConfiguration extends YamlConfiguration {

        public void save(File file) throws IOException {
            Validate.notNull(file, "File cannot be null");
            String data = this.saveToString();
            Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
            try {
            } finally {

        public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
            Validate.notNull(file, "File cannot be null");
            this.load(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8));

        public void load(InputStream stream) throws IOException, InvalidConfigurationException {
            Validate.notNull(stream, "Stream cannot be null");
            this.load(new InputStreamReader(stream, Charsets.UTF_8));
    • Like Like x 4
  13. The file which contains a UTF-8 symbol is of course UTF-8.
    My current code, could it be built in somewhere there?
    Code (Text):

    public static FileConfiguration getFileConfiguration(String fileName) {
            File file = new File("plugins/NameManager/" + fileName + ".yml");
            FileConfiguration fileConfiguration = new YamlConfiguration();
            try {
                if (fileName == "config") {
                   String version = fileConfiguration.getString("version");
                   if (version != null && version.equals(configVersion)) {
                       return fileConfiguration;
                   if (version == null) {
                       version = "backup";
                   if (file.renameTo(new File(plugin.getDataFolder(), "old-" + fileName + "-" + version + ".yml"))) {
                      plugin.getLogger().info("Found outdated config, creating backup...");
                       plugin.getLogger().info("Created a backup for: " + fileName + ".yml");
                } else {
                    return fileConfiguration;
            } catch (IOException|InvalidConfigurationException e) {
                plugin.getLogger().info("Generating fresh configuration file: " + fileName + ".yml");

            try {
                if (!file.exists()) {
                    InputStream in = plugin.getResource(fileName + ".yml");
                    OutputStream out = new FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
            } catch(IOException|InvalidConfigurationException ex) {
                plugin.getLogger().severe("Plugin unable to write configuration file " + fileName + ".yml!");
            return fileConfiguration;
  14. I ran into this problem some time ago with using special characters in a config.yml. @DarkSeraphim explained the idea that I used. In a string in the config, replace the symbol with its corresponding code (\uXXXX). Normally, the codes would be translated automatically to text when you parse them, but I think since you're using Bukkit API and not retrieving the string from the yml file yourself then you need to translate the codes yourself. The Apache Commons has a good method for translating these easily:

    Code (Text):
    String translated = StringEscapeUtils.unescapeJava(String totranslate);
    So essentially, just retrieve the string from the config and plug it into this method and out comes the translated text with the symbol.
    • Winner Winner x 1
  15. You can just replace this line:
    Code (Text):
    FileConfiguration fileConfiguration = new YamlConfiguration();
    Code (Text):
    FileConfiguration fileConfiguration = new Utf8YamlConfiguration();
    since the Utf8YamlConfiguration the default class extends.
    • Like Like x 1
  16. Code (Text):
    String translated = StringEscapeUtils.unescapeJava(getConfig().getString("HealthFormat"));
                objective.setDisplayName(ChatColor.translateAlternateColorCodes('&', translated));
    Didn't work for me :/
  17. I get: Utf8YamlConfiguration cannot be resolved to a type
  18. @AgentLV The class I posted is not available in the bukkit api. You will need to add it.
  19. @Cybermaxke Did it exactly as you told me, still doesn't work :/
Thread Status:
Not open for further replies.