How to reload a config

Discussion in 'Spigot Plugin Development' started by Vert3x, Feb 28, 2017.

  1. Hello, I'd like to know how to properly reload a config. I'm aware of the JavaPlugin#reloadConfig method but I think there's a little general misunderstanding: what I mean with "reload a config" is to get its newest saved option to work rather than leaving them just into the file and not effective. Every plugin (with a config) has got a reload command (or argument of a command) which acts like what I asked for, but reloadConfig method doesn't look to work as I expect, if it even works. So, basically, in order to do what I described above, what method(s) should I properly use?
     
  2. Im using the saveConfig() method ;)
     
  3. Do you use only that?
     
  4. Yes :) When i something set to a config, im Adding a saveConfig() right after that. No problem getting something right after
     
  5. I'm back. After many attempts, I'm assuming that perhaps my failing is being caused by some misunderstanding. What I actually mean with "reload" a config is to refresh its values when it has been MANUALLY wrote and saved through programs such as Notepad++: whatever I try I never get the new manually wrote value to work. In some cases the config gets updated (as Notepad++ notifies) and keeps the wrote value, but that's not got to work though and I often have to reload it with a Plugman command. I would like to end this problem once for all today because I'm pretty annoyed by missing this important essential thing.
     
  6. In my FileManager I use this method to reload my configuration files.

    Code (Text):
        public void reloadConfig() {
             //config is a FileConfiguration object
            //cfile is the File object
            config = YamlConfiguration.loadConfiguration(cfile);
        }
     
    • Agree Agree x 1
  7. Uhm.. Gotta try this after having tested with FileConfiguration#load method.
     
  8. I'm sorry, I was busy. So, both FileConfiguration#load and YamlConfiguration.loadConfiguration method did not work. I don't know what else to do.
     
  9. Get the config file directly from the plugin's data folder, load it as a normal YamlConfiguration, then re-save the YamlConfiguration as the config file you pulled from the plugin's data folder.

    Code (Text):
    File configFile = new File();

    YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);

    config.save(configFile);
    ( you're gonna have to edit that a bit for it to actually work )
     
  10. YamlConfiguration.loadConfiguration(File) works perfectly fine, you are just doing it wrong. Mind sharing your class?
     
    • Funny Funny x 1
  11. I don't use default config.yml file, so I could directly create a method into my custom yaml file class accessing both File and FileConfiguration objects.
    It doesn't work for what I need it for, I have to update the file values to the new manually changed ones once it has been saved.
    Code (Java):

    private static File file = null;
    private static FileConfiguration config = null;

    public static void reloadValues() {
        config = YamlConfiguration.loadConfiguration(file);
        saveConfig();
    }

    public static void saveConfig() {
        try {
            config.save(file);
        } catch (IOException e) {
            Main.getInstance().getLogger().log(Level.SEVERE, "Could not save config to " + file, e);
        }
    }
     
    I just use reloadValues method in the command I use to reload the configuration.
    (if you noticed private fields were not static, that is because I wrote them again manually and forgot about it, obviously they are static in my actual class. I've just changed them though, this is an edit).
     
  12. Usage reloadConfig() is a method native of Bukkit on Main Class
     
    • Agree Agree x 3
    • Useful Useful x 1
  13. That was the first method I tried when I had to do what I want and it was also the first one not to work.
     
  14. And I have to bump again.. Every plugin includes a command which allows what I'm asking for but none of ways to do it I get suggested work..
     
  15. I'll try to explain it one last time.
    If you use the default config (saveDefaultConfig(), getConfig()...), reloadConfig() will do exactly this:
    If you use a "custom" config (aka something which is NOT called "config.yml"), simply use:
    Code (Java):
    FileConfiguration config = YamlConfiguration.loadConfiguration(new File(PATH_HERE, FILE_HERE));
    Obviously, if you have a variable of type FileConfiguration in two different classes and only do the above for the first, the second one will still have the old values. Thus, make sure all your variables are "up to date".
     
    • Useful Useful x 1
  16. I always use something along these lines:
    Code (Java):
    fun reload(): Unit {
    configFile.reader().use {
        config = gson.fromJson(it, CustomConfig::class.java)
    }
    }
     
  17. You literally quoted @Lori00 . I've already shown my class and there's nothing really important over there.
    Mh.. Will see this soon. These days around I've stopped coding a bit to relax.
     
  18. The problem is where you use the data. From a recent plugin I wrote:

    Code (Text):
        private void loadConfig() {
            try {
                config = new ConfigHandlerSI(this);
            } catch(IOException e) {
                Bukkit.getPluginManager().disablePlugin(this);
                throw new RuntimeException("Failed to generate default cofnig", e);
            }
            item = new ItemHandlerSI(config);
            event = new EventHandlerSI(config, item);
        }
    As you can see, I not only replace my config handler but also everything that uses it.
     
  19. Ok, I'm back.
    I don't use more than one class, I'm currently registering the reloadValues method into the same class I developed the custom yaml file handling in.
    In the code I've shown some days ago into this thread, "config" and "file" variables aren't just got by creating new Objects like you were doing directly in a method:
    Code (Java):
    new File(PATH_HERE, FILE_HERE);
    Also,
    if this stuff above was true, then I would just be using my reloadConfig method created into the class instead of getting into threads, the method I created into my custom class for my custom yaml file I attempted to use is:
    Code (Java):
    public static void reloadConfig() {
        config = YamlConfiguration.loadConfiguration(file);
        InputStream defaultConfigStream = Main.getInstance().getResource("Configuration.yml");
        if (defaultConfigStream != null) {
            config.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defaultConfigStream, Charsets.UTF_8)));
        }
    }
    , the one into JavaPlugin class is this:
    Code (Java):
    public void reloadConfig() {
        this.newConfig = YamlConfiguration.loadConfiguration(this.configFile);
        InputStream defConfigStream = this.getResource("config.yml");
        if(defConfigStream != null) {
            this.newConfig.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8)));
        }
    }
    As mine is static, I'm not using this to refer to my class (it would be useless even if I was, actually), but the code is the same or almost it. If the method purpose was what I need, then my method would have worked fine, and did not though.
    I don't understand this.
    What do you mean to say by "everything that uses it"? I don't really have any class containing my config handler into the constructor and I just access its methods because they're all static (as I guess you could see).
     
  20. Code (Text):
    reloadConfig();
    Add it in the reload command and its so easy as it could be..