Solved yml file encoding ? (Unsolved)

Discussion in 'Plugin Development' started by InflamedSebi, May 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    InflamedSebi

    Hay, i have a problem with my yml files ... somtimes all '§' will be replaced with '�' on other pc's. This should be an encoding problem, but i don't know how to solve this.

    my default encoding is: Cp1252 (windows default)

    and i can switch to:
    ISO-8859-1
    US-ASCII
    UTF-16
    UTF-16BE
    UTF-16LE
    UTF-8

    btw I'm using eclipse as ide.

    ty4help
     
  2. Offline

    Shevchik

    you need to switch to UTF-8
     
  3. Offline

    InflamedSebi

    UTF-8 is still not right ... if i use "§4" is the config, its not a right color code ... ... its adds strange Ä's or T's in front of the code ...

    so the "§4" is a "§4" after i used config.getString(path);
    and the console is printing the "Â" as a small version of "T"
    [​IMG]
    the inputs from the config look like:
    consoleTest: [§6SpecialTools§r] §fB: [§6%c§fx§6%n§f]
    chatTest: §fB: [§6%c§fx§6%n§f]
    ... so no Â's and T's ... but each § creates such an odd char ...
     
  4. Offline

    TheE

    This is one of the major flaws of bukkit's configuration API: It always uses the default encoding of the platform the server is running on and there is absolutely now way to specify what encoding you want to use.

    There might be issues when reading the string out of the config (in Cp1252) and converting it to java's inbuild UTF-16 and back to the server's logic (UTF-8 ?), but I have absolutely no idea how to solve this, except from writing your own configuration-handler...
     
  5. Offline

    psycowithespn

  6. Offline

    InflamedSebi

    Y U NO ...

    i messed around with the encodings ...:
    US-ASCII; UTF-16; UTF-16BE; UTF-16LE
    are not usable ... so only ISO-8859-1 and UTF-8 are left ....
    Due to UTF-8 is causing the ecoding bug i switched over to ISO-8859-1 ... seems like it worked ... but wasn't able to test it on some non european nor non windows systems ...

    looks good ... i have my own cfa (ConfigFileAccessor) so this would be a good addition ...
    will try it later ^^

    ----- EDIT:

    I adjusted my cfa to fit

    fileConfiguration.loadFromString(Files.toString(file, Charset.forName("UTF-8")));

    but same result ...

    Code:java
    1. package com.blog.inflamedsebi.SpecialTools.ressources;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.nio.charset.Charset;
    7. import java.util.logging.Level;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import com.blog.inflamedsebi.SpecialTools.Core;
    12. import com.google.common.io.Files;
    13.  
    14. /**
    15.  * Basic implementation for multiple config files.
    16.  *
    17.  * @author Inflamedsebi
    18.  *
    19.  */
    20. public class ConfigFileAccessor {
    21.  
    22. private final Core plugin;
    23. private SpecialLogger sl;
    24.  
    25. /**
    26.   * Enables multiple yaml configuration files.
    27.   *
    28.   * @param plugin
    29.   */
    30. public ConfigFileAccessor(Core instance) {
    31. plugin = instance;
    32. sl = plugin.sl;
    33. }
    34.  
    35. /**
    36.   * Does the same as getConfig() but for the given config file.
    37.   *
    38.   * @param configFile
    39.   * @return FileConfiguration from configFile
    40.   */
    41. public FileConfiguration loadConfigDefault(String fileName) {
    42. File file = new File(plugin.getDataFolder(), fileName);
    43. FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file);
    44.  
    45. // Look for defaults in the jar
    46. InputStream defConfigStream = plugin.getResource(fileName);
    47. if (defConfigStream != null) {
    48. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    49. fileConfiguration.setDefaults(defConfig);
    50. }
    51. return fileConfiguration;
    52. }
    53.  
    54. /**
    55.   * Does the same as getConfig() but for the given config file.
    56.   *
    57.   * @param configFile
    58.   * @return FileConfiguration from configFile
    59.   */
    60. public FileConfiguration loadConfig(String fileName) {
    61. File file = new File(plugin.getDataFolder(), fileName);
    62. FileConfiguration fileConfiguration = new YamlConfiguration();
    63. try {
    64. file.createNewFile();
    65. fileConfiguration.loadFromString(Files.toString(file, Charset.forName("UTF-8")));
    66. } catch (Exception e) {
    67. sl.log(ChatColor.RED+"Could not load data from " + file, Level.SEVERE);
    68. }
    69.  
    70. return fileConfiguration;
    71. }
    72.  
    73. /**
    74.   * Will save the given FileConfiguration to the given File.
    75.   *
    76.   * @param configFile
    77.   * @param fileConfiguration
    78.   */
    79. public void saveConfig(String fileName, FileConfiguration fileConfiguration) {
    80. File file = new File(plugin.getDataFolder(), fileName);
    81. if (fileConfiguration == null || file == null) {
    82. return;
    83. } else {
    84. try {
    85. fileConfiguration.save(file);
    86. } catch (IOException ex) {
    87. sl.log(ChatColor.RED+"Could not save data to " + file, Level.SEVERE);
    88. }
    89. }
    90. }
    91.  
    92. /**
    93.   * Does the same as saveDefaultConfig() but for the given config file.
    94.   *
    95.   * @param configFile
    96.   */
    97. public void saveDefaultConfig(String fileName) {
    98. File file = new File(plugin.getDataFolder(), fileName);
    99. if (!file.exists()) {
    100. this.plugin.saveResource(fileName, false);
    101. }
    102. }
    103.  
    104. }
    105.  
    106.  
     
  7. Offline

    Codisimus

    Why doesn't Bukkit fix this flaw? The § symbol is very common these days especially when saving ItemStacks that have custom lore.

    Also the title of this thread says Solved and Unsolved...
     
Thread Status:
Not open for further replies.

Share This Page