Advertisement
Dudemister1999

eCrud Config

Oct 26th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import org.bukkit.configuration.file.YamlConfiguration;
  4.  
  5. /**
  6.  * @author Hydroxocobalamin. All rights reserved.
  7.  * License: BSD (Read at Bukkit Forums Post)
  8.  */
  9. public final class Config
  10. {
  11.     private File configFile;
  12.     private YamlConfiguration config;
  13.     private String fileName;
  14.    
  15.     /**
  16.      * Default constructor for configurations.
  17.      * @param folder Folder to store the config in
  18.      * @param name Name of the file (Specify extension!)
  19.      */
  20.     public Config(File folder, String name)
  21.     {
  22.         fileName = name;
  23.         configFile = new File(folder, name);
  24.     }
  25.    
  26.     /**
  27.      * Get the config file
  28.      * @return config file
  29.      */
  30.     public File getFile()
  31.     {
  32.         return configFile;
  33.     }
  34.    
  35.     /**
  36.      * Get the YamlConfiguration paired with the config
  37.      * @return The config
  38.      */
  39.     public YamlConfiguration getYaml()
  40.     {
  41.         return config;
  42.     }
  43.    
  44.     /**
  45.      * Get the name of the file
  46.      * @return File name
  47.      */
  48.     public String getFileName()
  49.     {
  50.         return fileName;
  51.     }
  52.    
  53.     /**
  54.      * Get the folder the config is stored in
  55.      * @return The config's folder
  56.      */
  57.     public File getFolder()
  58.     {
  59.         return configFile.getParentFile();
  60.     }
  61.    
  62.     /**
  63.      * Create a configuration section
  64.      * @param path Section to create
  65.      */
  66.     public void createSection(String path)
  67.     {
  68.         if(!config.contains(path))
  69.         {
  70.             config.createSection(path);
  71.             save();
  72.         }
  73.     }
  74.    
  75.     /**
  76.      * Set the value to the config file.
  77.      * @param path Path to store the variable
  78.      * @param value Value to store on the path
  79.      * @return The object specified
  80.      */
  81.     public Object setValue(String path, Object value)
  82.     {
  83.         if(hasValue(path, true))
  84.         {
  85.             config.set(path, value);
  86.         }
  87.         else
  88.         {
  89.             config.set(path, value);
  90.         }
  91.         save();
  92.         return value;
  93.     }
  94.    
  95.     /**
  96.      * Check the config for a value
  97.      * @param path Path to find the variable
  98.      * @return Does the config have the value?
  99.      */
  100.     public boolean hasValue(String path)
  101.     {
  102.         if(config.contains(path))
  103.         {
  104.             return true;
  105.         }
  106.         else
  107.         {
  108.             return false;
  109.         }
  110.     }
  111.    
  112.     /**
  113.      * Check the config for a value
  114.      * @param path Path to find the variable
  115.      * @param createIfNot If it can't find the path, create it?
  116.      * @return Does the config have the value?
  117.      */
  118.     public boolean hasValue(String path, boolean createIfNot)
  119.     {
  120.         if(config.contains(path))
  121.         {
  122.             return true;
  123.         }
  124.         else
  125.         {
  126.             if(createIfNot)
  127.             {
  128.                 config.createSection(path);
  129.                 save();
  130.             }
  131.             return false;
  132.         }
  133.     }
  134.    
  135.     /**
  136.      * Get an object from config, return null if it couldn't find the value
  137.      * @param path Path to find the value
  138.      * @return Config collected from file
  139.      */
  140.     public Object getValue(String path)
  141.     {
  142.         if(config.contains(path))
  143.         {
  144.             return config.get(path);
  145.         }
  146.         else
  147.         {
  148.             config.createSection(path);
  149.             save();
  150.         }
  151.         return null;
  152.     }
  153.    
  154.     /**
  155.      * Get an object from config, return default if it couldn't find the path
  156.      * @param path Path to find the value
  157.      * @param def Default value, if it couldn't find one in the config
  158.      * @return Config collected from file
  159.      */
  160.     public Object getValue(String path, Object def)
  161.     {
  162.         if(config.contains(path))
  163.         {
  164.             return config.get(path);
  165.         }
  166.         else
  167.         {
  168.             config.createSection(path);
  169.             config.set(path, def);
  170.             save();
  171.             return def;
  172.         }
  173.     }
  174.    
  175.     /**
  176.      * Create the config file, and folder.
  177.      * @return True if it was created, false if an exception was thrown.
  178.      */
  179.     public boolean create()
  180.     {
  181.         try
  182.         {
  183.             if(!getFolder().exists())
  184.             {
  185.                 getFolder().mkdir();
  186.             }
  187.            
  188.             configFile.createNewFile();
  189.             config = YamlConfiguration.loadConfiguration(configFile);
  190.             return true;
  191.         }
  192.         catch(IOException ex)
  193.         {
  194.             System.err.println("Error while creating file (IOException): ");
  195.             ex.printStackTrace();
  196.             return false;
  197.         }
  198.     }
  199.    
  200.     /**
  201.      * Delete the config, then re-create it.
  202.      * @return True if it worked, false if an exception was thrown.
  203.      */
  204.     public boolean recreate()
  205.     {
  206.         configFile.delete();
  207.         config = null;
  208.        
  209.         try
  210.         {
  211.             if(!getFolder().exists())
  212.             {
  213.                 getFolder().mkdir();
  214.             }
  215.            
  216.             configFile.createNewFile();
  217.             config = YamlConfiguration.loadConfiguration(configFile);
  218.             return true;
  219.         }
  220.         catch (IOException ex)
  221.         {
  222.             System.err.println("Error while creating file (IOException): ");
  223.             ex.printStackTrace();
  224.             return false;
  225.         }
  226.     }
  227.    
  228.     /**
  229.      * Delete the config file.
  230.      * @return True or false, depending on if it was deleted or not.
  231.      */
  232.     public boolean delete()
  233.     {
  234.         return configFile.delete();
  235.     }
  236.    
  237.     /**
  238.      * Delete the folder the config is stored in.
  239.      * @return True or false, depending on if it was deleted or not.
  240.      */
  241.     public boolean deleteFolder()
  242.     {
  243.         return getFolder().delete();
  244.     }
  245.    
  246.    
  247.     /**
  248.      * Save the config to disk.
  249.      * @return True if it saved, false if an exception was thrown
  250.      */
  251.     public boolean save()
  252.     {
  253.         try
  254.         {
  255.             config.save(configFile);
  256.             return true;
  257.         }
  258.         catch(IOException ex)
  259.         {
  260.             return false;
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement