Util ConfigAPI

Discussion in 'Resources' started by OfficerDeveloper, Mar 21, 2015.

?

Like?

Poll closed Mar 21, 2015.
  1. Yes

    2 vote(s)
    40.0%
  2. No

    3 vote(s)
    60.0%
Thread Status:
Not open for further replies.
  1. This is for people who do not know how to create configuration files :p.

    Code:
    Code:
    package com.officeremerald.configurationapi;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    
    public class ConfigAPI {
    
        private static FileConfiguration config;
    
        public static FileConfiguration getConfig() {
            return config;
        }
    
        public static void setupConfiguration(String dataFolder, String[] string, String[] string2) {
    
            File configFile = new File("plugins/" + dataFolder + "", "config.yml");
    
            if (configFile.exists())
                return;
    
            for (String s : string) {
                config.set(s, string2);
            }
    
            try {
                config.save(configFile);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
    
            try {
                configFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        public static void saveConfig(File fileDir) {
            try {
                config.save(fileDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
       
        public static void setConfig(FileConfiguration config1){
           
            config = config1;
           
        }
    
    }
    
    

    Usage (In onenable):
    Code:
    package com.officeremerald.configurationapi;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
       
        FileConfiguration config;
        String[] s = {"Messages.joinMes", "Messages.leaveMes"};
        String[] s1 = {"Welcome to UHC!", "GoodBye!"};
       
       
        public void onEnable(){
           
            config = getConfig();
            ConfigAPI.setConfig(config);
            ConfigAPI.setupConfiguration("ConfigAPI", s , s1);
           
        }
    
    }
    
    Usage(In getting config):
    Code:
    ConfigAPI.getConfig().getString("Messages.lol");
    Note: For the first String, use the name you put in your plugin.yml file.
     
    Last edited: Mar 21, 2015
  2. Offline

    teej107

    @OfficerDeveloper What if I want multiple configurations? What do I do then?
    And have you tested your code? I see a NPE already. :p
     
  3. If you want multiple configurations you dont use my util. No need for the sass. And let me fix the NPE error, didnt see it there.

    Fixed the NPE.

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
    Last edited by a moderator: Mar 21, 2015
  4. Offline

    nverdier

    If you can only create one config why not use "saveDefaultConfig()" and "getConfig()"?
     
  5. Like I said, most people who use this will not know how to make configs. This is just a tool to help them, not make multiple configs.
     
  6. Offline

    nverdier

    @OfficerDeveloper Well just make a post telling people to use "saveDefaultConfig()" and "getConfig()"...
     
    ChipDev and KingFaris11 like this.
  7. *sighs* Noone knows how to be thankful...
     
  8. Offline

    mrCookieSlime Retired Staff

    Locked on Request.
     
Thread Status:
Not open for further replies.

Share This Page