Advertisement
Guest User

Config Class

a guest
Feb 7th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package ;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import org.bukkit.configuration.file.YamlConfiguration;
  9.  
  10. public class Config {
  11.     <MAINCLASS> plugin;
  12.     public Config(<MAINCLASS> plugin) {
  13.         this.plugin = plugin;
  14.     }
  15.    
  16.     public FileConfiguration customConfig;
  17.     public File configFile;
  18.    
  19.     public void createConfig() {
  20.         configFile = new File(plugin.getDataFolder(), "config.yml");
  21.         if (!configFile.exists()) {
  22.             try {
  23.                 configFile.createNewFile();
  24.                 generateConfig();
  25.             } catch (IOException e) {
  26.                 e.printStackTrace();
  27.             }
  28.         }
  29.     }
  30.    
  31.     public void generateConfig() {
  32.         try {
  33.             File customConfigFile = new File(plugin.getDataFolder(), "config.yml");
  34.             FileWriter w = new FileWriter(customConfigFile);
  35.             w(w, "# Wow, a comment!");
  36.             w(w, "hi-world: true");
  37.             w(w, "");
  38.             w(w, "# OMG! Another comment!");
  39.             w(w, "whatever: 'hai der'");
  40.             w.close();
  41.             reloadConfig();
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.    
  47.     private void w(FileWriter writer, String string) throws IOException {
  48.         writer.write(string + "\n");
  49.     }
  50.    
  51.     public void reloadConfig() {
  52.         if (!configFile.exists()) {
  53.             configFile = new File(plugin.getDataFolder(), "config.yml");
  54.         }
  55.         customConfig = YamlConfiguration.loadConfiguration(configFile);
  56.        
  57.         InputStream defConfigStream = plugin.getResource("config.yml");
  58.         if (defConfigStream != null) {
  59.             YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  60.             customConfig.setDefaults(defConfig);
  61.         }
  62.     }
  63.    
  64.     public FileConfiguration getConfig() {
  65.         if (customConfig == null) {
  66.             reloadConfig();
  67.         }
  68.         return customConfig;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement