Brord icon

Easy configuration but the Bukkit API

Brord | PRO | 02/13/13 07:19:37 PM UTC | 0 ⭐ | 420 👁️ | Never ⏰ | []
Java 5 |

0 B

|

None

|

0 👍

/

0 👎

package net.castegaming.plugins.FPSCaste.config;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
 
import net.castegaming.plugins.FPSCaste.FPSCaste;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
 
public class Config {
        
        public FPSCaste plugin;
        
        public Config(){
            plugin = FPSCaste.getInstance();
        }
 
        /**
         * Creates the specified config (Doesnt save!)
         * @param name The config to create
         * @return The config which you just made
         */
        public FileConfiguration createConfig(String name) {
                if (!name.endsWith(".yml")) {
                        name = name + ".yml"; 
                }
                File file = new File(plugin.getDataFolder(),name);
                if (!file.exists()) {
                        plugin.getDataFolder().mkdir();
                        try {
                                file.createNewFile();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                return YamlConfiguration.loadConfiguration(file);// returns the newly created configuration object.
        }
 
        /**
         * Saves the specified config to the main plugin dir.<br/>
         * The string is the name of the config to save
         * @param name The name the config should have
         * @param config The config to  save
         * @return nothing
         */
        public void saveConfig(String name, FileConfiguration config) {
                if (!name.endsWith(".yml")) {
                        name = name + ".yml";
                }
                File file = new File(plugin.getDataFolder(),name);
                try {
                        config.save(file);
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
 
        /**
         * Gets the specified config from the main plugin dir
         * @param name The config to get
         * @return The config or null if it doesnt exist
         */
        public FileConfiguration getConfig(String name) {
            if (!name.endsWith(".yml")) {
                name = name + ".yml";
            }
            
            File file = new File(plugin.getDataFolder(), name);
            
            if (!file.exists()){
                return null;
            } else {
                return YamlConfiguration.loadConfiguration(file);
            }
        }
        
        /******************************Player configs***************************************/
        
        /**
         * Creates a player config, and saves it to disk
         * @param name name of the config/player
         */
        public void createPlayerConfig(String name) {
            if (!name.endsWith(".yml")) {
                name = name + ".yml";
            }
            
            File file = new File(plugin.getDataFolder() + File.separator + "players", name);
            if (!file.exists()) {
                try {
                     file.createNewFile();
                } catch (IOException e) {
                      e.printStackTrace();
                }
            }
            FileConfiguration playerfile = getPlayerConfig(name);
            FileConfiguration defaultplayer = getConfig("defaultplayer");
            
            for (String key : defaultplayer.getKeys(false)){
                playerfile.set(key, defaultplayer.get(key));
            }
            
            savePlayerConfig(name, playerfile);
        }
        
        /**
         * Saves the player config to disk
         * @param name the name of the player/config
         * @param config the Player his config to save
         */
        public void savePlayerConfig(String name, FileConfiguration config) {
            if (!name.endsWith(".yml")) {
                    name = name + ".yml";
            }
            File file = new File(plugin.getDataFolder() + File.separator + "players", name);
            try {
                    config.save(file);
            } catch (IOException e) {
                    e.printStackTrace();
            }
        }
        
        /**
         * Gets the player his configuration
         * @param name the name of the player/config
         * @return the config
         */
        public FileConfiguration getPlayerConfig(String name) {
            if (!name.endsWith(".yml")) {
               name = name + ".yml";
            }
            
            File file = new File(plugin.getDataFolder() + File.separator + "players", name);
            
            if (!file.exists()){
                return null;
            } else {
                return YamlConfiguration.loadConfiguration(file);
            }
        }
 
        
        /**
         * Copys values from a config inside the plugin jar
         * @param name the name of the config to copy
         * @return the freshly made config
         */
        public FileConfiguration copy(String name){
            if (!name.endsWith(".yml")) {
                   name = name + ".yml";
                }
            
            FileConfiguration config = getConfig(name);
            
            //Does this config exist?
            if (config != null){
                InputStream defaultStream = plugin.getResource(name);
                
                //does the raw config exist?
                if (defaultStream != null){
                    
                    //laod the stream into a config
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defaultStream);
                    
                    //add the defaults to the config
                    config.addDefaults(defConfig);
                    
                    //save the config to disk
                    saveConfig(name, defConfig);
                    
                    //return the filled config
                    return config;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎