Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.19 KB | None | 0 0
  1. package me.messageofdeath.PaidRanks.Utils.zRequired.Database;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.ArrayList;
  8.  
  9. import org.bukkit.Material;
  10. import org.bukkit.configuration.ConfigurationSection;
  11. import org.bukkit.configuration.file.FileConfiguration;
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14.  
  15. /**
  16.  * @author messageofDEATH
  17.  */
  18.  
  19. public class YamlDatabase {
  20.    
  21.     public JavaPlugin plugin = null;
  22.     public String fileName = null, fileExtension = null, fileLocation = null;
  23.     public File file = null;
  24.     public FileConfiguration fileConfig = null;
  25.     public boolean createdFile = false, saveOnSet = true;
  26.    
  27.     /**
  28.      * Creates a new instance of YamlDatabase with the default fileLocation.
  29.      * @param plugin
  30.      * @param fileName
  31.      * @return new YamlDatabase
  32.      */
  33.    
  34.     public YamlDatabase(JavaPlugin plugin) {
  35.         this.plugin = plugin;
  36.         this.fileExtension = ".yml";
  37.         this.fileConfig = new YamlConfiguration();
  38.     }
  39.    
  40.     public YamlDatabase(JavaPlugin plugin, String fileName) {
  41.         this.plugin = plugin;
  42.         this.fileName = fileName;
  43.         this.fileExtension = ".yml";
  44.         this.fileConfig = new YamlConfiguration();
  45.     }
  46.    
  47.     /**
  48.      * Creates a new instance of YamlDatabase with a set fileLocation.
  49.      * @param plugin
  50.      * @param fileName
  51.      * @param fileLocation
  52.      * @return new YamlDatabase
  53.      */
  54.    
  55.     public YamlDatabase(JavaPlugin plugin, String fileName, String fileLocation) {
  56.         this.plugin = plugin;
  57.         this.fileName = fileName;
  58.         this.fileExtension = ".yml";
  59.         this.fileConfig = new YamlConfiguration();
  60.         this.fileLocation = fileLocation;
  61.     }
  62.    
  63.     /**
  64.      * Checks if file exists, if not creates one and puts default.
  65.      */
  66.     public void changeFile(String fileName) {
  67.         this.changeFile(fileName, this.plugin.getDataFolder().getPath());
  68.     }
  69.    
  70.     public void changeFile(String fileName, String fileLocation) {
  71.         this.fileName = fileName;
  72.         this.fileLocation = fileLocation;
  73.         this.onStartUp();
  74.     }
  75.    
  76.     public void onStartUp() {
  77.         if(this.fileLocation == null)
  78.             this.file = new File(this.plugin.getDataFolder(), this.fileName + this.fileExtension);
  79.         else
  80.             this.file = new File(this.fileLocation, this.fileName + this.fileExtension);
  81.         try{
  82.             // *** Config ***
  83.             this.fileConfig = YamlConfiguration.loadConfiguration(this.file);
  84.             if(!this.file.exists()){
  85.                 this. file.getParentFile().mkdirs();
  86.                 this.file.createNewFile();
  87.                 if(this.plugin.getResource(this.fileName + this.fileExtension) != null)
  88.                     copy(this.plugin.getResource(this.fileName + this.fileExtension), this.file);
  89.                 this.createdFile = true;
  90.             }
  91.             //this.fileConfig.load(this.file);
  92.         }catch (Exception e){e.getCause();}
  93.     }
  94.    
  95.     /**
  96.      * Saves the file.
  97.      */
  98.    
  99.     public void onShutDown() {
  100.         this.save();
  101.     }
  102.    
  103.     private void copy(InputStream in, File file) {
  104.         try{
  105.             OutputStream out = new FileOutputStream(file);
  106.             byte[] buf = new byte[1024];
  107.             int len;
  108.             while ((len = in.read(buf)) > 0){
  109.                 out.write(buf, 0, len);
  110.             }
  111.             out.close();
  112.             in.close();
  113.         }catch (Exception e){
  114.             e.printStackTrace();
  115.         }
  116.     }
  117.        
  118.      /**
  119.      * Reloads the file
  120.      */
  121.    
  122.     public void reload() {
  123.         try{
  124.             this.fileConfig.load(this.file);
  125.         }catch (Exception e){
  126.                    
  127.         }
  128.     }
  129.    
  130.     /**
  131.      * Saves the file
  132.      */
  133.    
  134.     public void save() {
  135.         try{
  136.             this.fileConfig.save(this.file);
  137.         }catch(Exception e) {
  138.            
  139.         }
  140.     }
  141.    
  142.      /**
  143.      * Gets a ConfigurationSection value from the file.
  144.      * @param key
  145.      * @param fallback
  146.      * @return the ConfigurationSection for the key, if exists.
  147.      * @return fallback when the key doesn't exist.
  148.      */
  149.        
  150.     public ConfigurationSection getConfigurationSection(String key, ConfigurationSection fallback) {
  151.         if(this.fileConfig.contains(key)) {
  152.             return this.fileConfig.getConfigurationSection(key);
  153.         }else{
  154.             return fallback;
  155.         }
  156.     }
  157.        
  158.      /**
  159.      * Gets the ConfigurationSection in a List<String> value from the file.
  160.      * @param key
  161.      * @return the List<String> for the key, if exists.
  162.      * @return fallback when the key doesn't exist.
  163.      */
  164.    
  165.     public ArrayList<String> getSection(String key, ArrayList<String> fallback) {
  166.         if(this.fileConfig.contains(key)) {
  167.             ArrayList<String> section = new ArrayList<String>();
  168.             for(Object str : getConfigurationSection(key, null).getKeys(false).toArray()) {
  169.                 section.add(String.valueOf(str));
  170.             }
  171.             return section;
  172.         }else{
  173.             return fallback;
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * Gets an integer value from the file.
  179.      * @param key
  180.      * @param fallback
  181.      * @return the integer for the key, if exists.
  182.      * @return fallback when the key doesn't exist.
  183.      */
  184.     public int getInteger(String key, int fallback){
  185.         if(this.fileConfig.contains(key)) {
  186.             return this.fileConfig.getInt(key);
  187.         }else{
  188.             return fallback;
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Gets an string value from the file.
  194.      * @param key
  195.      * @param fallback
  196.      * @return the string for the key, if exists.
  197.      * @return fallback when the key doesn't exist.
  198.      */
  199.     public String getString(String key, String fallback){
  200.         if(this.fileConfig.contains(key)) {
  201.             return this.fileConfig.getString(key);
  202.         }else{
  203.             return fallback;
  204.         }
  205.     }
  206.        
  207.         /**
  208.      * Gets an float value from the file.
  209.      * @param key
  210.      * @return true for the key, if exists.
  211.      * @return false when the key doesn't exist.
  212.      */
  213.    
  214.     public boolean contains(String key) {
  215.         return this.fileConfig.contains(key);
  216.     }
  217.  
  218.     /**
  219.      * Gets an boolean value from the file. It will accept "true" and "false".
  220.      * @param key
  221.      * @param fallback
  222.      * @return the boolean for the key, if exists.
  223.      * @return fallback when the key doesn't exist.
  224.      */
  225.     public boolean getBoolean(String key, boolean fallback){
  226.         if(this.fileConfig.contains(key)) {
  227.             return this.fileConfig.getBoolean(key);
  228.         }else{
  229.             return fallback;
  230.         }
  231.     }
  232.    
  233.         /**
  234.      * Gets a List<String> value from the file.
  235.      * @param key
  236.      * @param fallback
  237.      * @return the List<String> for the key, if exists.
  238.      * @return fallback when the key doesn't exist.
  239.      */
  240.        
  241.     public ArrayList<String> getStringArray(String key, ArrayList<String> fallback) {
  242.         if(this.fileConfig.contains(key)) {
  243.             return (ArrayList<String>)this.fileConfig.getStringList(key);
  244.         }else{
  245.             return fallback;
  246.         }
  247.     }
  248.  
  249.     /**
  250.      * Gets an double value from the file.
  251.      * @param key
  252.      * @param fallback
  253.      * @return the double for the key, if exists.
  254.      * @return fallback when the key doesn't exist.
  255.      */
  256.     public double getDouble(String key, double fallback){
  257.         if(this.fileConfig.contains(key)) {
  258.             return this.fileConfig.getDouble(key);
  259.         }else{
  260.             return fallback;
  261.         }
  262.     }
  263.  
  264.         /**
  265.      * Gets an Object value from the file.
  266.      * @param key
  267.      * @param fallback
  268.      * @return the Object for the key, if exists.
  269.      * @return fallback when the key doesn't exist.
  270.      */
  271.        
  272.     public Object getObject(String key, Object fallback) {
  273.         if(this.fileConfig.contains(key)) {
  274.             return this.fileConfig.get(key);
  275.         }else{
  276.             return fallback;
  277.         }
  278.     }
  279.     /**
  280.      * Gets an float value from the file.
  281.      * @param key
  282.      * @param fallback
  283.      * @return the float for the key, if exists.
  284.      * @return fallback when the key doesn't exist.
  285.      */
  286.     public float getFloat(String key, float fallback){
  287.         if(this.fileConfig.contains(key)) {
  288.             return (float) this.fileConfig.getDouble(key);
  289.         }else{
  290.             return fallback;
  291.         }
  292.     }
  293.  
  294.     /**
  295.      * Gets an material value from the file. It parses material-ids as well as Bukkit-Material names.
  296.      * @param key
  297.      * @param fallback
  298.      * @return the material for the key, if exists.
  299.      * @return fallback when the key doesn't exist.
  300.      */
  301.     public Material getMaterial(String key, Material fallback){
  302.         if(this.fileConfig.contains(key)) {
  303.             return this.fileConfig.getItemStack(key).getType();
  304.         }else{
  305.             return fallback;
  306.         }
  307.     }
  308.  
  309.     /**
  310.      * When one key exists multiple times, use this method to get all values as strings in a list.
  311.      * @param key the key to search
  312.      * @return all values for that key.
  313.      */
  314.  
  315.     /**
  316.      * Writes the keySet to the file
  317.      */
  318.    
  319.     public void set(String key, Object set) {
  320.         this.fileConfig.set(key, set);
  321.         if(this.saveOnSet) {
  322.             this.save();
  323.         }
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement