[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);
        fos.write(newBytes);
        fos.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
    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 {

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

        @Override
        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));
        }

        @Override
        @Deprecated
        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 {
                fileConfiguration.load(file);
               
                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()) {
                    file.getParentFile().mkdirs();
                    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);
                    out.close();
                    in.close();
                }
                fileConfiguration.load(file);
            } catch(IOException|InvalidConfigurationException ex) {
                plugin.getLogger().severe("Plugin unable to write configuration file " + fileName + ".yml!");
                plugin.getLogger().severe("Disabling...");
                plugin.getServer().getPluginManager().disablePlugin(plugin);
                ex.printStackTrace();
            }
           
            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();
    with
    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.