[COMPLETELY RE-DONE] The Ultimate Way Of Managing Your Configs

Discussion in 'Resources' started by BungeeTheCookie, Dec 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    BungeeTheCookie

    ConfigurationAPI

    Introduction: WhenI was driving home from school today and I became bored; an idea popped up in my head. Wouldn't it be great to easily reload, make, get, save, and save default configuration files, YAMLConfiguration files, and even just plain text documents, with one string and not have to write up a bunch of code? In about 12 minutes, I made an easy to use awesome Configuration Library! It's called ConfigurationAPI.

    Features:
    • Create files, YamlConfiguration files, Configuration files, and text documents in a second!
    • Reload your files with ease!
    • Get booleans, integers, and strings from your config or text file easier than ever!
    • Never worry about losing your information, this API has saved you!
    • No settings lines, strings, booleans, integers; this API has come with a save default config option!
    Usage:
    There are two requirements in all of the methods that come with ConfigurationAPI; a JavaPlugin, and a String.
    Code:java
    1. public class Test extends JavaPlugin{
    2.  
    3. public void onEnable(){
    4. Bukkit.getServer().shutdown();
    5. }
    6. //Creating or Reloading The Config
    7. ConfigurationAPI.reloadConfig(this, "config.yml");
    8.  
    9. //Getting the Config
    10. ConfigurationAPI.getConfig(this, "config.yml");
    11.  
    12. //Saving the Config
    13. ConfigurationAPI.saveConfig(this, "config.yml");
    14.  
    15. //Saving the Default Config
    16. ConfigurationAPI.saveDefaultConfig(this, "config.yml");
    17.  
    18. }

    Closing: In closing, I wanted to say that anyone could contribute code, methods, or fields to this library, and I will update it whenever I can. If there are any errors, you can tag me using BungeeTheCookie. To end this awesome resource, I will post the ConfigurationAPI code. You are free to use this resource as long as you give credit to me.
    Code:
    PHP:
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.logging.Level;
     
    public class 
    ConfigurationAPI {
     
    private static 
    JavaPlugin plugin;
    private static 
    HashMap<StringFileConfiguration>configs = new HashMap<>();
    private static 
    HashMap<StringFile>files = new HashMap<>();
    private 
    File configFile;
    private 
    FileConfiguration config;
    private 
    String configName;
     
        public 
    ConfigurationAPI(JavaPlugin pluginString fileNameFile fileLocation){
            
    this.plugin plugin;
            
    this.configName fileName;
            if (
    configFile == null) {
                
    configFile = new File(fileLocationfileName);
            }
            if(
    config == null){
                
    config YamlConfiguration.loadConfiguration(configFile);
            }
            if(!
    configs.containsKey(fileName) && !files.containsKey(fileName)){
                
    configs.put(fileNameconfig);
                
    files.put(fileNameconfigFile);
            }
        }
     
     
        public 
    String getName(){
            return 
    configName;
        }
     
        public 
    File getFile() {
            return 
    configFile;
        }
     
        public static 
    File getFile(JavaPlugin pluginString fileNamethrows IOException {
            
    loadAllConfigs(plugin);
            return 
    files.get(fileName);
        }
     
        public 
    void reloadConfig(){
            
    config YamlConfiguration.loadConfiguration(getFile());
     
            
    InputStream defConfigStream plugin.getResource(getName());
            if (
    defConfigStream != null) {
                
    YamlConfiguration defConfig YamlConfiguration.loadConfiguration(defConfigStream);
                
    config.setDefaults(defConfig);
            }
        }
     
        public static 
    void reloadConfig(JavaPlugin pluginString fileNamethrows IOException {
            
    FileConfiguration config YamlConfiguration.loadConfiguration(getFile(pluginfileName));
     
            
    InputStream defConfigStream plugin.getResource(fileName);
            if (
    defConfigStream != null) {
                
    YamlConfiguration defConfig YamlConfiguration.loadConfiguration(defConfigStream);
                
    config.setDefaults(defConfig);
            }
        }
     
        public static 
    HashMap<StringFileConfigurationgetConfigs(JavaPlugin plugin){
            
    loadAllConfigs(plugin);
            return 
    configs;
        }
     
        public 
    FileConfiguration getConfig(){
            return 
    config;
        }
     
        public static 
    FileConfiguration getConfig(JavaPlugin pluginString fileNamethrows IOException{
            return 
    getConfigs(plugin).get(fileName);
        }
     
        public 
    void saveConfig(){
            try {
                
    getConfig().save(configFile);
            } catch (
    IOException ex) {
                
    plugin.getLogger().log(Level.SEVERE"ERROR: Could not save the config file " getName());
                
    ex.printStackTrace();
            }
        }
     
        public static 
    void saveConfig(JavaPlugin pluginString fileNamethrows IOException{
            try{
                
    loadAllConfigs(plugin);
                
    getConfig(pluginfileName).save(getFile(pluginfileName));
            }catch(
    IOException ex){
                
    plugin.getLogger().log(Level.SEVERE"ERROR: Could not save the config file " fileName);
                
    ex.printStackTrace();
            }
        }
     
        public static 
    void saveDefaultConfig(JavaPlugin pluginFile fileLocationString fileName){
            
    File configFile = new File(fileLocationfileName);
            if (!
    configFile.exists()) {
                
    plugin.saveResource(fileNamefalse);
            }
        }
     
        public static 
    void loadAllConfigs(JavaPlugin plugin){
            try{
                for(
    File file plugin.getDataFolder().listFiles()){
                    
    FileConfiguration config YamlConfiguration.loadConfiguration(file);
                    if(!
    configs.containsKey(file.getName()) && !files.containsKey(file.getName())){
                        
    configs.put(file.getName(), config);
                        
    files.put(file.getName(), file);
                    }
                }
            }catch(
    Exception e){
                
    Bukkit.getServer().getLogger().severe("Oh oh! Could not load the configuration files for Plugin " plugin.getName() + "!");
                
    e.printStackTrace();
            }
        }
    }
    I hope you enjoy it, and happy coding!

    Reserved.

    Any and all contributions are appreciated! Thank you!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    Mahtaran and gabrielhowat like this.
  2. Offline

    jimbo8

    BungeeTheCookie
    Nice one!
    Now i don't have to go through the whole process when creating a new config file, you made my life a whole lot easier!

    Testing it out now.
    [EDIT]
    Why did you add so that when the server starts up, it automatically shuts down?

    You should remove that one before any bukkit-staff see's it :)
     
  3. Offline

    BungeeTheCookie

    Thanks! Glad you liked it :)
     
  4. Offline

    jimbo8

    BungeeTheCookie

    I've been testing this out for a week or so now, and i love it! I really recommend this. I've found a few bugs, though.

    Let's say that you have a MOTD command and a home command. When the home config is missing, if you set a home, it will save in the MOTD-config instead. I don't know why this is happening, as i i'm completely sure that everything is correct, i've checked many times.
     
  5. Offline

    BungeeTheCookie

    Thanks! And I'll look into the bug aswell. Also, it was a troll. Apparently you are never going to put Bukkit.getServer().shutdown() in your onEnable :p
     
    jimbo8 likes this.
  6. Offline

    Goblom

  7. Offline

    BungeeTheCookie

    Oh. Sorry. I never saw your post before (I guess it was on the third or fourth page) and I made this one. Atleast there is two ;)

    The Gaming Grunts
    I did get this off of the Configuration API Reference. I made this because instead of always copying and pasting code, I made this utility, so you only have to write the method once, not copying and pasting for every new file.
     
  8. Offline

    MineStein

    This is cool, I am interested to work with this in the future! Nice one Bungee!
     
  9. Offline

    BungeeTheCookie

    Thanks Einstein!
     
  10. Offline

    MineStein

  11. Offline

    BungeeTheCookie

    jimbo8 MineStein Goblom The Gaming Grunts
    Sorry it took me for ever, I forgot that I ever coded this thing until MineStein bumped it. I have completely redone the entire thing, and now it will work properly and better than ever! I also added a ton of new methods.
     
    jimbo8 likes this.
Thread Status:
Not open for further replies.

Share This Page