Solved Creating a config.yml

Discussion in 'Spigot Plugin Development' started by SirTak, Jun 10, 2016.

  1. How do I create the default config without explicitly using config.addDefault ? Or do I just have to do them all during the onEnable() ?
     
  2. You can put it in the same place as your plugin.yml and export it in the jar, then just fill it out like you want your config to look like (even with comments starting with #) and then in your onenable put
    saveDefaultConfig();
    It only saves it if the config.yml doesn't already exist in the target directory
     
  3. So I can just create a skeleton config.yml and pack it in with my jar with the export? As long as I save the default whenever the plugin runs.
     
  4. Yes. Well, except for that last part. You aren't actually saving the default every time it runs. Technically you could put it in an if statement if you want (if (!getDateFolder().exists)) but saveDefaultConfig() checks that for you.
     
    • Winner Winner x 1
  5. I create Configs like this:

    Code (Text):
    package me.multi.lobby.database.config;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;

    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class Config {

        public static File file = new File("plugins/Lobby", "hologram.yml");
        public static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
       
        public static void exist() {
            if(file.exists()) {
                try {
                    cfg.load(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InvalidConfigurationException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    cfg.save(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
     
     
  6. MiniDigger

    Supporter

    more static abuse pls.
     
  7. Use saveResource() to save the default config made in the same place as plugin.yml, then use YamlConfiguration.loadConfiguration() to (re)load, and fileconfiguration.save() to save.
     
  8. Minidigger this is a better way, as i must only get an instance for this. The exist() method i should change to a method without static, but the variables file and cfg are better with static.
     
  9. MiniDigger

    Supporter

    this is not a better way. you have an static instance that never gets unloaded. this could be a potential memory leaks on reloads. and on top of this, it is just a bad design decision to make it static and a bad practice.
     
    • Agree Agree x 1
  10. Than give me a reason why you use in RideThaMob also static methods. This method i used, work really good and i don't find some errors in console in the time i used this ;)
     
  11. MiniDigger

    Supporter

    RideThaMob is a perfect example of horrible bad design. You should take into account that I created it 3 years ago. I was still kinda new to the concepts of oop. But that is no reason to continue making my mistakes.
     
  12. Yeah but i think, if you think is a bad way, you should make it better.
    SupaDupaBroadcast is write in one class. WTF. You would like to tell me sth. about design?! This is from april...
     
  13. MiniDigger

    Supporter

    having fun stalking my github profile? ^^
    that plugin only contains ~150 lines of code, why would I want to split it up?
    While you are at it, take a look at my (wip) quest api here (https://github.com/Devintia/Quest/tree/api/src/main/java/net/devintia/quests) or my armorstand model importer here (http://git.devintia.net/Devintia/Co...n/java/net/devintia/commons/bukkit/armorstand)
     
  14. Making everything static is well-known as bad OOP practice.