[Resource] Custom YamlConfiguration Wrapper

Discussion in 'Resources' started by Dragonphase, Mar 31, 2014.

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

    Dragonphase

    I made a simple wrapper class which extends YamlConfiguration and lets you easily create custom YAML configuration files.

    The class:

    Code:java
    1. import java.io.File;
    2. import org.bukkit.configuration.file.YamlConfiguration;
    3.  
    4. import your.plugin.main.class.Plugin;
    5.  
    6. public class Config extends YamlConfiguration{
    7.  
    8. private Plugin plugin;
    9. private String fileName;
    10.  
    11. public Config(Plugin plugin, String fileName){
    12. this.plugin = plugin;
    13. this.fileName = fileName;
    14.  
    15. save();
    16. }
    17.  
    18. public void save() {
    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. }


    Usage:

    Code:java
    1. Config example = new Config(this, "example.yml");


    This will create a new YAML file if one does not exist. If a YAML file exists as a resource in your plugin, it will save that file. After this, the Config class can be used like any other YamlConfiguration, except for one difference:

    Code:java
    1. example.save();


    This will save the configuration file to disk and reload it.
     
Thread Status:
Not open for further replies.

Share This Page