Whats the easiest way to make config file?

Discussion in 'Spigot Plugin Development' started by Januxor, Jul 5, 2018.

  1. Making configs by scratch seems a bit hard.
    Anyone knows any custom class that I can use for configs.

    Something like this in this tutorial but there download links don't work.


    Also, I'm pretty new to spigot and to this forum.
     
  2. Should probably use the search feature of this site. Or Google. This question has been asked (and answered) at least 5 times this week on this site. Kinda tired of repeating things, so just use the search ;)
     
    • Like Like x 1
  3. I mean like if you want to use the built in config thing from the spigot api...

    creating a config.yml manually in the project folder then do saveDefaultConfig(); - Easiest for beginners
    getConfig().get(string);
    getConfig().set(string, value);
    Code (Text):
    public class ExtraConfigs extends YamlConfiguration {
     
        private File file;
        private String defaults;
        private JavaPlugin plugin;
       
        public ExtraConfigs(JavaPlugin plugin, String fileName) {
            this(plugin, fileName, null);
        }
        public ExtraConfigs(JavaPlugin plugin, String fileName, String defaultsName) {
            this.plugin = plugin;
            this.defaults = defaultsName;
            this.file = new File(plugin.getDataFolder(), fileName);
            reload();
        }
     
        /**
         * Reload configuration
         */
        public void reload() {
         
            if (!file.exists()) {
             
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                 
                } catch (IOException exception) {
                    exception.printStackTrace();
                    plugin.getLogger().severe("Error while creating file " + file.getName());
                }
             
            }
         
            try {
                load(file);
             
                if (defaults != null) {
                    InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
                    FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);    
                 
                    setDefaults(defaultsConfig);
                    options().copyDefaults(true);
                 
                    reader.close();
                    save();
                }
         
            } catch (IOException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());
             
            } catch (InvalidConfigurationException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());
             
            }
         
        }
     
        /**
         * Save configuration
         */
        public void save() {
         
            try {
                options().indent(2);
                save(file);
             
            } catch (IOException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while saving file " + file.getName());
            }
         
        }
     
    }
     
     

  4. You can use this class
    https://github.com/Songoda/Arconix/.../songoda/arconix/api/utils/ConfigWrapper.java

    Here is an example how I use it in my plugin (I initialiaze it and create getters for it within the main class)
    https://github.com/kiranhart/Monthl...in/java/com/shadebyte/monthlycrates/Core.java
     
    • Agree Agree x 2
  5. I have written for myself the following class:
    https://gist.github.com/hyWse/cfe1410e9b6813267a9123ce273abcb7

    Example:
    Code (Java):
    // Creates a new config file under the path "plugins/MyPlugin/test.yml" if the file does not yet exist
    WseConfig config = new WseConfig("plugins//MyPlugin", "test.yml");
    // Adds the word "World!" under the key "Hello" to the config
    config.getConfig().set("Hello", "World!");
    // Saves the config
    config.saveConfig();

    // Returns the word "World!" just saved under the key "Hello". If the key "Hello" does not exist, a default value "World!" is returned.
    String hello = config.getConfig().getString("Hello", "World!");
    // Prints "Hello World!" into the console.
    System.out.println("Hello " + hello);
     
  6. Strahan

    Benefactor

    What's wrong with the built in config handler?
     
    • Agree Agree x 1
  7. I personally thought @OP was wanting to make custom configs like coins.yml rather then using thr normal getConfig()
     
  8. Strahan

    Benefactor

    Ahh, well that makes sense then.
     
  9. I don't know I am new to bukkit/spigot.
    In this tutorial, I posted he didn't use the built-in config handler. I guess customs are easier.

    Edit:
    Actually ill try built-in.