Simple Configuration class - Easily create, edit and save .yml files!

Discussion in 'Resources' started by Dragonphase, Jul 19, 2014.

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

    Dragonphase

    Simple YamlConfiguration Extension: Easily create new files

    I have created a simple extenension class for YamlConfiguration which allows you to create new .yml files easily and quickly. It acts in the exact same way as any YamlConfiguration, but does not require you to go through a long tedious process when you want to create new configs.

    Code:

    Code:java
    1. import java.io.File;
    2.  
    3. import org.bukkit.configuration.file.YamlConfiguration;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Config extends YamlConfiguration{
    7.  
    8. private JavaPlugin plugin;
    9. private String fileName;
    10.  
    11. public Config(JavaPlugin plugin, String fileName){
    12. this.plugin = plugin;
    13. this.fileName = fileName + (fileName.endsWith(".yml") ? "" : ".yml");
    14.  
    15. createFile();
    16. }
    17.  
    18. private void createFile() {
    19. try {
    20. File file = new File(plugin.getDataFolder(), fileName);
    21. if (!file.exists()){
    22. if (plugin.getResource(fileName) != null){
    23. plugin.saveResource(fileName, false);
    24. }else{
    25. save(file);
    26. }
    27. }else{
    28. load(file);
    29. save(file);
    30. }
    31. } catch (Exception ex) {
    32. ex.printStackTrace();
    33. }
    34. }
    35.  
    36. public void save(){
    37. try {
    38. save(new File(plugin.getDataFolder(), fileName));
    39. } catch (Exception ex) {
    40. ex.printStackTrace();
    41. }
    42. }
    43. }
    44.  


    If the file already exists, it will load that file and use it. If the file does not exist on the disk but does exist as a resource within the plugin, it will use that resource and save it. Otherwise, it will create an entirely new file and use that.

    Remember, this class is an extension of YamlConfiguration. This means you can use all of YamlConfiguration's methods, like you would from your Plugin's default config. If you want to save any changes you make to a config, you must use the .save(); method.

    Usage:

    Code:java
    1. Config config = new Config(this, "config"); //Get the plugin's default config.yml file
    2. config.set("test", "Hello world!");
    3. config.save(); //Save and write changes made to the config.
    4.  
    5. String hello = config.getString("test"); //Retrieve the string stored at the path "test" in the config.
    6.  
    7. for (Player player : Bukkit.getOnlinePlayers()){
    8. Config playerConfig = new Config(this, "Players" + File.separator + player.getName());
    9. playerConfig.set("name", player.getName());
    10. playerConfig.save();
    11. }
    12. //Creates a collection of .yml files in a new Players folder, for all online players, and stores their location.
    13. //Note, this is an example, remember that Bukkit is now supporting UUIDs, so use player.getUniqueID() if you want to make any further use of this.


    If you notice any errors or issues with this code, please let me know. You may use this code freely as long as you give credit to me! :)
     
  2. Offline

    dsouzamatt

    Why are there two save methods and no set?
     
  3. Offline

    Dragonphase

    dsouzamatt
    I updated the main post so that it's a little less confusing.
     
  4. Offline

    dsouzamatt

    Dragonphase Ah, I see. Yeah, that makes sense now.
     
  5. Offline

    AoH_Ruthless

    Dragonphase
    Very nice resource. I may use this (with due credit, of course).
     
    Dragonphase likes this.
  6. Offline

    TheYajrab

    Dragonphase
    Great resource but is there any way to use this in a JoinListener class as well? I keep getting a load of errors when I try to reference to it.
     
  7. Offline

    Dragonphase

    TheYajrab
    Then you're referencing it wrong.
     
  8. Offline

    Phasesaber

    You might want to allow different files extensions, as YamlConfiguration can handle more than just .yml.
     
  9. Offline

    Dragonphase

    Phasesaber

    Anyone can alter the class to do this.
     
Thread Status:
Not open for further replies.

Share This Page