Advertisement
Guest User

YamlDatabase.java

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