How to write and read a .yml file?

Discussion in 'Plugin Development' started by Shinxs, Jan 21, 2013.

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

    Shinxs

    I'm working on a plugin right now but i need to write and read to a groups.yml file, i searched a long time but everything i tried didn't work.

    I really wanna go further with the plugin but i can't i first need the groups.yml file.
     
    Caldan112 likes this.
  2. Offline

    AyshineMatt

    If you're talking about developing a plugin, you're in the wrong section. The correct section where you would receive more attention is here.

    You posted in the section where users ask other users for tips and help on their Bukkit server.

    Thread has been alerted to the staff for a move.
     
  3. Offline

    TnT

    Moved to Plugin Development.
     
    xiMatt likes this.
  4. Offline

    Lolmewn

    Have you tried looking at the wonderful wiki we have?
    I guess not, since that would've answered your question.
     
  5. Offline

    Shinxs

    yes i have but i couldn't find it can you than send a link?
     
  6. Offline

    Lolmewn

  7. Offline

    Shinxs

  8. Offline

    Yasahiro

    Why don't you just use java file I/O operations?
     
  9. Offline

    Shinxs

    I already made it and it works the only thing is that when i reload it it's empty do you mabey know how to fix it?
     
  10. Offline

    InflamedSebi

    u need to save the file like u do for the config with saveConfig()

    i created a copy of the default Config.java and edit this to work with custom yml files ... maybe its helpfull for u ...

    Show Spoiler
    Code:java
    1. package com.github.inflamedsebi.DefaultPlugin;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.logging.Level;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9.  
    10. public class ConfigFileAccessor {
    11.  
    12. private final DefaultPlugin plugin;
    13. private SpecialLogger sl;
    14.  
    15. /**
    16.   * Enables multiple yaml configuration files.
    17.   *
    18.   * @param plugin
    19.   */
    20. public ConfigFileAccessor(DefaultPlugin instance) {
    21. plugin = instance;
    22. sl = plugin.sl;
    23. }
    24.  
    25. /**
    26.   * Does the same as getConfig() but for the given config file.
    27.   *
    28.   * @param configFile
    29.   * @return FileConfiguration from configFile
    30.   */
    31. public FileConfiguration loadConfig(File configFile) {
    32. FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
    33.  
    34. // Look for defaults in the jar
    35. InputStream defConfigStream = plugin.getResource(configFile.getName());
    36. if (defConfigStream != null) {
    37. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    38. fileConfiguration.setDefaults(defConfig);
    39. }
    40. return fileConfiguration;
    41. }
    42.  
    43. /**
    44.   * Will save the given FileConfiguration to the given File.
    45.   *
    46.   * @param configFile
    47.   * @param fileConfiguration
    48.   */
    49. public void saveConfig(File configFile, FileConfiguration fileConfiguration) {
    50. if (fileConfiguration == null || configFile == null) {
    51. return;
    52. } else {
    53. try {
    54. fileConfiguration.save(configFile);
    55. } catch (IOException ex) {
    56. sl.log("Could not save config to " + configFile, Level.SEVERE);
    57. }
    58. }
    59. }
    60.  
    61. /**
    62.   * Does the same as saveDefaultConfig() but for the given config file.
    63.   *
    64.   * @param configFile
    65.   */
    66. public void saveDefaultConfig(File configFile) {
    67. if (!configFile.exists()) {
    68. this.plugin.saveResource(configFile.getName(), false);
    69. }
    70. }
    71.  
    72. }
    73.  

    this is part of my DefaultPlugin if u want to see more: FULL SOURCE
     
  11. Offline

    Shinxs

    yes i know i have to save it and i do that but after the reload they are empty so if i delete the files and then reload they are not empty anymore but thats to much work for one test
     
Thread Status:
Not open for further replies.

Share This Page