Advertisement
Guest User

YMLFactory.java

a guest
Aug 1st, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. package DCNetwork.KitPVP.Util;
  2.  
  3. import org.bukkit.configuration.file.FileConfiguration;
  4. import org.bukkit.configuration.file.YamlConfiguration;
  5. import org.bukkit.plugin.java.JavaPlugin;
  6.  
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.logging.Level;
  11.  
  12. public class YMLFactory {
  13.  
  14.     public static YML buildYML(String name, JavaPlugin main) {
  15.         YML yml = new YML(name, main);
  16.         return yml;
  17.     }
  18.  
  19.     public static class YML {
  20.  
  21.         private FileConfiguration config;
  22.         private File file;
  23.  
  24.         private String name;
  25.         private JavaPlugin main;
  26.  
  27.         private String path;
  28.  
  29.         public YML(String name, JavaPlugin main) {
  30.             this.name = name;
  31.             this.main = main;
  32.  
  33.             this.path = this.name;
  34.  
  35.             this.file = new File(main.getDataFolder(), path);
  36.         }
  37.  
  38.         public FileConfiguration getConfig() {
  39.             if (config == null) {
  40.                 reloadConfig();
  41.             }
  42.             return config;
  43.         }
  44.  
  45.         public void saveConfig() {
  46.             if (config == null || file == null) {
  47.                 return;
  48.             }
  49.             try {
  50.                 getConfig().save(file);
  51.             } catch (IOException ex) {
  52.                 main.getLogger().log(Level.SEVERE, "Could not save " + name + " to file", ex);
  53.             }
  54.         }
  55.  
  56.         public void reloadConfig() {
  57.             if (file == null) {
  58.                 file = new File(main.getDataFolder(), path);
  59.             }
  60.             config = YamlConfiguration.loadConfiguration(file);
  61.             InputStream fileStream = main.getResource(path);
  62.             if (fileStream != null) {
  63.                 YamlConfiguration configStream = YamlConfiguration.loadConfiguration(fileStream);
  64.                 config.setDefaults(configStream);
  65.             }
  66.         }
  67.  
  68.         public void saveDefaultConfig() {
  69.             if (file == null) {
  70.                 file = new File(main.getDataFolder(), path);
  71.             }
  72.             if (!file.exists()) {
  73.                 main.saveResource(path, false);
  74.             }
  75.         }
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement