Dudemister1999

eCrud ConfigManager

Oct 26th, 2014
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.util.HashMap;
  3.  
  4. public class ConfigManager
  5. {
  6.     private static final HashMap<String, Config> configFiles = new HashMap<>();
  7.    
  8.     /**
  9.      * Create a configuration file. Creates the file, adds it to the registry, and returns the new instance
  10.      * @param folder Folder to store the config file in
  11.      * @param name Name of file (NOTE: Include file extension!)
  12.      * @return The new Config instance.
  13.      */
  14.     public static Config createConfig(File folder, String name)
  15.     {
  16.         Config cfg = new Config(folder, name);
  17.        
  18.         if(!configFiles.containsKey(name))
  19.         {
  20.             if(cfg.create())
  21.             {
  22.                 configFiles.put(name, cfg);
  23.                 return cfg;
  24.             }
  25.             else
  26.             {
  27.                 System.err.println("Error: Could not create file.");
  28.                 return null;
  29.             }
  30.         }
  31.         else
  32.         {
  33.             return getConfig(name);
  34.         }
  35.     }
  36.    
  37.     /**
  38.      * Gets the config from the registry.
  39.      * @param name Name of config (Checks through config database, throws an InvalidConfigException if the inputted config isn't registered)
  40.      * @return The config selected from the registry.
  41.      */
  42.     public static Config getConfig(String name)
  43.     {
  44.         if(configFiles.containsKey(name))
  45.         {
  46.             return configFiles.get(name);
  47.         }
  48.         else
  49.         {
  50.             throw new InvalidConfigException("Could not locate file " + name + "! (Was not registered)");
  51.         }
  52.     }
  53.    
  54.     /**
  55.      * Deletes config's file and registry.
  56.      * @param name Name of config (Checks through config database, throws an InvalidConfigException if the inputted config isn't registered)
  57.      * @return True if deleted successfully, false if something went wrong.
  58.      */
  59.     public static boolean deleteConfig(String name)
  60.     {
  61.         if(configFiles.containsKey(name))
  62.         {
  63.             Config cfg = configFiles.get(name);
  64.            
  65.             if(cfg.delete())
  66.             {
  67.                 configFiles.remove(name);
  68.                 return true;
  69.             }
  70.             else
  71.             {
  72.                 return false;
  73.             }
  74.         }
  75.         else
  76.         {
  77.             throw new InvalidConfigException("Could not delete file " + name + "! (Was not registered)");
  78.         }
  79.     }
  80.    
  81.     /**
  82.      * Recreate a config (In the event of data corruption, updated cfgs, etc.)
  83.      * @param name Name of config (Checks through config database, throws an InvalidConfigException if the inputted config isn't registered)
  84.      * @return True if recreated, false if something went wrong.
  85.      */
  86.     public static boolean recreateConfig(String name)
  87.     {
  88.         if(configFiles.containsKey(name))
  89.         {
  90.             Config cfg = configFiles.get(name);
  91.            
  92.             return cfg.recreate();
  93.         }
  94.         else
  95.         {
  96.             throw new InvalidConfigException("Could not find file " + name + "! (Was not registered)");
  97.         }
  98.     }
  99.    
  100.     /**
  101.      * Save all currently registered configurations.
  102.      */
  103.     public static void saveAllConfigs()
  104.     {
  105.         for(Config cfg : configFiles.values())
  106.         {
  107.             cfg.save();
  108.         }
  109.     }
  110.    
  111.     /**
  112.      * Use in your plugin's onDisable method. Just a precaution, to save all the files.
  113.      */
  114.     public static void onDisable()
  115.     {
  116.         for(Config cfg : configFiles.values())
  117.         {
  118.             cfg.save();
  119.         }
  120.         configFiles.clear();
  121.     }
  122. }
  123.  
  124. /**
  125.  * Exception that's thrown in the event an invalid configuration is used/found.
  126.  * @author Hydroxocobalamin
  127.  */
  128. class InvalidConfigException extends RuntimeException
  129. {
  130.     public InvalidConfigException(String ex)
  131.     {
  132.         super(ex);
  133.     }
  134. }
RAW Paste Data