Solved How to reload a custom config?

Discussion in 'Spigot Plugin Development' started by parpar8090, Feb 20, 2020.

  1. So I use this to reload all my configs after I do /cmd-reload:
    Code (Text):
    public void reloadConfigs() {
            mc.reloadThisConfig();
            udc.reloadThisConfig();
            msg.reloadThisConfig();
            pc.reloadThisConfig();
            gc.reloadThisConfig();
        }
    The command works fine, but the config doesn't output the updated values. for example I set:
    Code (Text):
    Message: "Hey"
    and then I do:
    Code (Text):
    Message: "Hello!"
    then I do /cmd-reload and it still outputs "Hey" instead of "Hello!".

    Can you help me?

    Here's my reloadConfig code:
    Code (Text):
    public void reloadConfig(File configFile, FileConfiguration config) {
            config = YamlConfiguration.loadConfiguration(configFile);
            if (!configFile.exists()) {
              createConfig(configFile, config);
            }
        }
     
  2. @parpar8090 You should first check if the file does exists before loading it in a YamlConfiguration, otherwise you could load a non-existing file and cause some weird issues.

    You have never saved the new config variable so the reloaded values are destroyed.
    Return the new config and save that returned config into a variable.

    Hope this helps
     
  3. Yes, but can I please get some code? I just don't understand the reloading method. Please.
     
  4. That is not how we do it here. You can ask questions about code, but it is still your job to figure it out yourself. We only give tips to help you figuring it out.

    But I'm in good mood so here you go:

    Code (Java):
     
    public static YamlConfiguration config;

    public void reloadConfig(File configFile, YamlConfiguration config){
        if(!configFile.exists()) {
            createConfig(configFile, config);
        }

        config = YamlConfiguration.loadConfiguration(configFile);
    }

    public static YamlConfiguration getConfig() {
        return config;
    }
     
    Use getConfig() to acces your new config file. Use this method also when you are initializing your config file for the first time.
     
  5. @parpar8090 If your issue is solved please mark this thread as solved. If you have any more questions, you're free to ask :)
     
    • Friendly Friendly x 1
  6. Oh, totally forgot!
    I use an abstract class called ConfigManager, and I extend it for every config file (class) (such as MainConfig.java, etc...).
    This is my code our there:
    Code (Java):
    protected abstract FileConfiguration getConfig();

        public static YamlConfiguration config; //Your code

        public void reloadConfig(File configFile, YamlConfiguration config){ //Your code
            if(!configFile.exists()) {
                createConfig(configFile, config);
            }

            config = YamlConfiguration.loadConfiguration(configFile);
        }
       
        public abstract void reloadThisConfig(); //Just makes a shortcut for reloadConfig(config, configFile).
       
        public void checkConfig(File configFile, FileConfiguration config) {
            if (!configFile.exists()) {
                  createConfig(configFile, config);
            }
        }
       
        public void saveConfig(File configFile, FileConfiguration config) {
            if (!configFile.exists()) {          
                 createConfig(configFile, config);
            }
            try {
                config.save(configFile);
            } catch (IOException e) {
                Bukkit.getConsoleSender().sendMessage("§cCould not save " + configFile.getName());
            }
        }
    So how your code should fit in this class? So that it can work for every extended config class
     
  7. Try this in your main class?
    Code (Text):
    this.reloadConfig();
     
  8. Save the configfile and the config as a variable in your subclass (eg MainConfig) and override the reloadThisConfig() and call the reloadConfig() with the configfile and the config in your subclass.

    eg in your subclass
    Code (Java):
    private YamlConfiguration config;
    private File configFile;

    @Override
    public void reloadThisConfig() {
       this.reloadConfig(configFile, config);
    }

    public void reloadConfig(File configFile, YamlConfiguration config)

    Also make your config variable in the subclass not static, it is irrelevant using a static context when you're using individual objects.

    A few tips:
    - If your only going to use YAML, I recommend to use YamlConfiguration.
    - Using '§' in strings is not a good idea. There is a reason Minecraft has '§' as illegal character. Spigot converts this character to a ChatColor before it touches minecraft code, but if spigot misses a character, the client or the server could crash. You should better use ChatColor.RED (or whatever) to use colors.
    -
    Code (Text):
     Bukkit.getLogger().log(Level.SEVRE, "Your message");
    logs your message as an error in console and automaticly add a red color to it. Same with Level.WARNING, but then in yellow
     
  9. this only works for config.yml. I have multiple configs
     
  10. My bad I didn't check the code properly.
     
  11. Ok, look, sorry if I didn't mention this, but I already did those things. I just need to know what to put in the reloadConfig(config, configFile) method. the others I can do by myself.

    Should I do:
    Code (Text):
    public void reloadConfig(File configFile, FileConfiguration config){
            if(!configFile.exists()) {
                createConfig(configFile, config);
            }

            config = YamlConfiguration.loadConfiguration(configFile);
            saveConfig(configFile, getConfig());
        }
    or what? WITHOUT using "config" variable in the abstract class, coz I use it in my subclasses. The ConfigManager is only for an "API" to all my configs.
     
  12. Put that code in a subclass of your ConfigManager (eg. MainConfig) and it should work.
     
  13. Try putting these
    Code (Text):
    File configFile = new File(Bukkit.getPluginManager().getPlugin("nameofplugin_casesensitive").getDataFolder(), "custom.yml");
    YamlConfiguration config = new YamlConfiguration();
    try {
        config.load(configFile);
    } catch (IOException | InvalidConfigurationException ex) {
        ex.printStackTrace();
    }
    Put in the name of your plugin, case sensitive and the name of the config file
     
  14. Your abstract class should handle the config variable (non-static). Subclasses should access it via a getter method. If you don't want other classes to access the config, use protected as the visibility of the getter.

    If, for whatever reason, you really want your subclasses to each have their own config variable, you have to duplicate your loading code. In this case, there is really nor reason for an abstract class.
     
  15. Hey, I noticed my subclasses' main method (the method that is called as the file) contains this:
    Code (Text):

    public MainConfig(PluginClass pl) {
            super(pl);
            configFile = new File(pl.getDataFolder(), "X.yml");
            checkConfig(configFile, config);
            config = YamlConfiguration.loadConfiguration(configFile);
    }
     
    Should I change it?
     
  16. Oh! Sorry, found the problem! In my reloadConfigs() method I didn't do setupConfigs(). As it basically does the same thing lol!
     
  17. That's not a main method, that's a constructor. It is used to create a new instance of your class. And as I wrote above, your subclasses should not have a "config" field, only the abstract superclass.
     
  18. This is from the spigot configuration tutorial:

    Using Custom Configurations
    First of all, you'll want to be able to make your File and FileConfiguration objects available to other classes in your plugin so that you can readily read and write to your different configuration files. How do you do this?

    In your main class, create field variables. These are variables that are not contained within a method so that they can be accessed externally.
    Code (Text):
    public class YourPlugin extends JavaPlugin {

        private File customConfigFile;
        private FileConfiguration customConfig;

        @Override
        public void onEnable(){
            createCustomConfig();
        }

        public FileConfiguration getCustomConfig() {
            return this.customConfig;
        }

        private void createCustomConfig() {
            customConfigFile = new File(getDataFolder(), "custom.yml");
            if (!customConfigFile.exists()) {
                customConfigFile.getParentFile().mkdirs();
                saveResource("custom.yml", false);
             }

            customConfig= new YamlConfiguration();
            try {
                customConfig.load(customConfigFile);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }
    }
    What does this do? It's a snapshot of how a basic plugin class would look like. What else it does is that it creates a configuration file, namely, "custom.yml". When the plugin is enabled, it calls the createCustomConfig() method. It checks if the File object of the custom config exists and if it doesn't it creates the parent directory and the file.

    What about the saveResource(String, boolean) part? You can actually store files inside your jar aside from just classes. To do this, depending on your IDE, you should create a new file in your 'src' folder (names may vary depending on editor). Now in your main class, you can call saveResource("name of file in jar here", replaceIfAlreadyExists). This will save the file stored in your jar to the file <data folder>/<name of file in jar> if it doesn't already exist (or if the boolean was true). Now, you've created the custom configuration files!

    We can do this:
    Code (Text):
    plugin.getCustomConfig().getString("some-path");
    Basically, you can access the configuration the same way as you would do getConfig() from Using Single Configuration Files. If you want to see more on how to manipulate and access configurations, read that section. As for saving, for custom configurations, you need to call FileConfiguration#save(File) (which saveConfig() does under the hood for config.yml) to write the data to the disk.
     
  19. If that is not clear enough, I don't know how else I can help you :(