Util Custom Configuration File Template

Discussion in 'Resources' started by x_Jake_s, Nov 23, 2016.

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

    x_Jake_s

    What it does-
    This code basically rewrites methods given for JavaPlugins getConfig() interface for use with other files

    How to insert-
    Place the code below in your main package and use the loadConfiguration() method in your onEnable()
    Code:
    //Initializes the file to create a reference
        private File basicFile = null;
        private FileConfiguration basic = null;
    
        public void reloadBasicConfig() {
            // BASIC FILE RELOAD
            if (basicFile == null) {
                basicFile = new File(pl.getDataFolder(), "basic.yml");
            }
            basic = YamlConfiguration.loadConfiguration(basicFile);
    
            // Look for defaults in the jar
            try {
                Reader defConfigStream = new InputStreamReader(pl.getResource("basic.yml"), "UTF8");
                if (defConfigStream != null) {
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    basic.setDefaults(defConfig);
                }
            } catch (Exception e) {
            }
        }
    
        public FileConfiguration getBasicConfig() {
            if (basic == null) {
                reloadBasicConfig();
            }
            return basic;
        }
    
        public void saveBasicConfig() {
            if (basic == null || basicFile == null) {
                return;
            }
            try {
                getBasicConfig().save(basicFile);
            } catch (IOException io) {
                pl.getLogger().log(Level.SEVERE, "Could not save config to " + basicFile, io);
            }
        }
    
        public void loadConfiguration() {
            // Set default values here
            this.getBasicConfig().addDefault("Which.Config.Is.This", "Basic Config!");
            // Creating the defaults
            this.getBasicConfig().options().copyDefaults(true);
            // Save the files (Always save after manipulation)
            this.saveBasicConfig();
        }
    
        //MUST READ
          /*
           * After implementing this simple copy and change the functions and value to that of a different name for
           * multiple files then just use (getBasicConfig) or whatever you named it to edit it like a default getConfig()
            /*
     
        //FURTHER NOTES (Setting Field Values)
        /*
         * If you wish to set a boolean value it would resemble the following:
         * this.getBasicConfig().set(path, true);
         *
         * If you wish to set a integer value it would resemble the following:
         * this.getBasicConfig().set(path, 5);
         *
         * If you wish to set a string value it would resemble the following:
         * this.getBasicConfig().set(path, "String Example");
         *
         * If you wish to set a list value it would resemble the following:
         * String[] list = {"Item1, Item2, Item3, Item4"}
         * this.getBasicConfig.set(path, Arrays.asList(list));
         */
     
        //FURTHER NOTES (Getting Field Values)
        /*
         * Getting a boolean value would resemble the following:
         * this.getBasicConfig().getBoolean(path);
         *
         * Getting a integer value would resemble the following:
         * this.getBasicConfig().getInt(path);
         *
         * Getting a string value would resemble the following:
         * this.getBasicConfig().getString(path);
         *
         * Getting a list value would resemble the following:
         * this.getBasicConfig().getList(path);
         */
     
        //FURTHER NOTES (Editing List Values)
        /*
         * Adding to a list is simple it resembles the following:
         * List<Type> list = (List<Type>)plugin.getBasicConfig().getList(path);
         * list.add(value);
         * plugin.getBasicConfig().set(path, list);
         *
         * Removing from a list is simple it resembles the following:
         * List<Type> list = (List<Type>)plugin.getBasicConfig().getList(path);
         * list.remove(value);
         * plugin.getBasicConfig().set(path, list);
         */
    
    }
     
    Last edited: Nov 23, 2016
  2. Offline

    Zombie_Striker

    @x_Jake_s
    1. Please add some sort of description of what this Util does. Don't just post the code without any explanation of A) What it does B) how to use it, and C) How to copy it to a project.
    2. You do not need to import the main class. If the class is in the same package, imports should not matter. Not only that, but you do not even need to keep those imports. Either use a command object the class extends (Such as JavaPlugin) or simply don't use those imports.
    3. Instead of making the user go through your whole class and change everything (such as the "which,config.it is " line, the name of the yml file, the location of the yml file, the name of the Main class, and the name of the class (Conf is ambiguous. It should alteast be called "Config"))
     
  3. Offline

    x_Jake_s

    1) thanks for pointing that out i have changed it to be more descriptive as to what it does and how to use it
    2) the class was in a different package which is the reason it was like that and i was currently using it for my minigame but i have edited it so only the parts that need to be transfered are there
    3) The class is meant to be customized as it is a template for any future file they make and every possible change they can make to the config file is at the bottom in quotes.
     
Thread Status:
Not open for further replies.

Share This Page