Advertisement
Brord

Easy configuration but the Bukkit API

Feb 13th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.51 KB | None | 0 0
  1. package net.castegaming.plugins.FPSCaste.config;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. import net.castegaming.plugins.FPSCaste.FPSCaste;
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.configuration.file.YamlConfiguration;
  10.  
  11. public class Config {
  12.        
  13.         public FPSCaste plugin;
  14.        
  15.         public Config(){
  16.             plugin = FPSCaste.getInstance();
  17.         }
  18.  
  19.         /**
  20.          * Creates the specified config (Doesnt save!)
  21.          * @param name The config to create
  22.          * @return The config which you just made
  23.          */
  24.         public FileConfiguration createConfig(String name) {
  25.                 if (!name.endsWith(".yml")) {
  26.                         name = name + ".yml";
  27.                 }
  28.                 File file = new File(plugin.getDataFolder(),name);
  29.                 if (!file.exists()) {
  30.                         plugin.getDataFolder().mkdir();
  31.                         try {
  32.                                 file.createNewFile();
  33.                         } catch (IOException e) {
  34.                                 e.printStackTrace();
  35.                         }
  36.                 }
  37.                 return YamlConfiguration.loadConfiguration(file);// returns the newly created configuration object.
  38.         }
  39.  
  40.         /**
  41.          * Saves the specified config to the main plugin dir.<br/>
  42.          * The string is the name of the config to save
  43.          * @param name The name the config should have
  44.          * @param config The config to  save
  45.          * @return nothing
  46.          */
  47.         public void saveConfig(String name, FileConfiguration config) {
  48.                 if (!name.endsWith(".yml")) {
  49.                         name = name + ".yml";
  50.                 }
  51.                 File file = new File(plugin.getDataFolder(),name);
  52.                 try {
  53.                         config.save(file);
  54.                 } catch (IOException e) {
  55.                         e.printStackTrace();
  56.                 }
  57.         }
  58.  
  59.         /**
  60.          * Gets the specified config from the main plugin dir
  61.          * @param name The config to get
  62.          * @return The config or null if it doesnt exist
  63.          */
  64.         public FileConfiguration getConfig(String name) {
  65.             if (!name.endsWith(".yml")) {
  66.                 name = name + ".yml";
  67.             }
  68.            
  69.             File file = new File(plugin.getDataFolder(), name);
  70.            
  71.             if (!file.exists()){
  72.                 return null;
  73.             } else {
  74.                 return YamlConfiguration.loadConfiguration(file);
  75.             }
  76.         }
  77.        
  78.         /******************************Player configs***************************************/
  79.        
  80.         /**
  81.          * Creates a player config, and saves it to disk
  82.          * @param name name of the config/player
  83.          */
  84.         public void createPlayerConfig(String name) {
  85.             if (!name.endsWith(".yml")) {
  86.                 name = name + ".yml";
  87.             }
  88.            
  89.             File file = new File(plugin.getDataFolder() + File.separator + "players", name);
  90.             if (!file.exists()) {
  91.                 try {
  92.                      file.createNewFile();
  93.                 } catch (IOException e) {
  94.                       e.printStackTrace();
  95.                 }
  96.             }
  97.             FileConfiguration playerfile = getPlayerConfig(name);
  98.             FileConfiguration defaultplayer = getConfig("defaultplayer");
  99.            
  100.             for (String key : defaultplayer.getKeys(false)){
  101.                 playerfile.set(key, defaultplayer.get(key));
  102.             }
  103.            
  104.             savePlayerConfig(name, playerfile);
  105.         }
  106.        
  107.         /**
  108.          * Saves the player config to disk
  109.          * @param name the name of the player/config
  110.          * @param config the Player his config to save
  111.          */
  112.         public void savePlayerConfig(String name, FileConfiguration config) {
  113.             if (!name.endsWith(".yml")) {
  114.                     name = name + ".yml";
  115.             }
  116.             File file = new File(plugin.getDataFolder() + File.separator + "players", name);
  117.             try {
  118.                     config.save(file);
  119.             } catch (IOException e) {
  120.                     e.printStackTrace();
  121.             }
  122.         }
  123.        
  124.         /**
  125.          * Gets the player his configuration
  126.          * @param name the name of the player/config
  127.          * @return the config
  128.          */
  129.         public FileConfiguration getPlayerConfig(String name) {
  130.             if (!name.endsWith(".yml")) {
  131.                name = name + ".yml";
  132.             }
  133.            
  134.             File file = new File(plugin.getDataFolder() + File.separator + "players", name);
  135.            
  136.             if (!file.exists()){
  137.                 return null;
  138.             } else {
  139.                 return YamlConfiguration.loadConfiguration(file);
  140.             }
  141.         }
  142.  
  143.        
  144.         /**
  145.          * Copys values from a config inside the plugin jar
  146.          * @param name the name of the config to copy
  147.          * @return the freshly made config
  148.          */
  149.         public FileConfiguration copy(String name){
  150.             if (!name.endsWith(".yml")) {
  151.                    name = name + ".yml";
  152.                 }
  153.            
  154.             FileConfiguration config = getConfig(name);
  155.            
  156.             //Does this config exist?
  157.             if (config != null){
  158.                 InputStream defaultStream = plugin.getResource(name);
  159.                
  160.                 //does the raw config exist?
  161.                 if (defaultStream != null){
  162.                    
  163.                     //laod the stream into a config
  164.                     YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defaultStream);
  165.                    
  166.                     //add the defaults to the config
  167.                     config.addDefaults(defConfig);
  168.                    
  169.                     //save the config to disk
  170.                     saveConfig(name, defConfig);
  171.                    
  172.                     //return the filled config
  173.                     return config;
  174.                 } else {
  175.                     return null;
  176.                 }
  177.             } else {
  178.                 return null;
  179.             }
  180.         }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement