Solved Plugin does not recognize values from config unless I reload it.

Discussion in 'Plugin Development' started by Paul122, Mar 5, 2021.

  1. Offline

    Paul122

    Hello,

    As the title says, I'm trying to access values form the config, but even after a restart, some values are not recognized (or they appear as uninitialized) unless I run my config's reload command. My main class looks like this:

    Code:
        @Override
        public void onEnable() {
            plugin = this;
            loadConfig();
        }
    
        @Override
        public void onDisable() {
            saveConfig();
        }
    
        public void loadConfig() {
            this.getConfig().options().copyDefaults();
            saveDefaultConfig();
        }
    
    
    Any insight on why this might be the case would be greatly appreciated.
     
  2. Offline

    Kars

    saveDefaultConfig must be run first in onEnable. Your config won't exist until after you execute saveDefaultConfig.
     
  3. Offline

    Paul122

    I tried running saveDefaultConfig() before anything else in my onEnable() method, but it still has issues with getting data from the config until I reload it with a separate command.
     
  4. Offline

    Kars

    How do you read the config then?
     
  5. Offline

    Paul122

    I have a class titled "Config" with accessors. Here it is:
    Code:
    public class Config {
    
        public static HashMap<String, String> giveaways = new HashMap<>();
    
        public String getPrefix() {
            String prefix = Giveaways.plugin.getConfig().getString("prefix");
            return ChatColor.translateAlternateColorCodes('&', prefix);
        }
    
        public void loadGiveaways() {
            for (String type : Giveaways.plugin.getConfig().getConfigurationSection("types").getKeys(false)) {
                giveaways.put(type, Giveaways.plugin.getConfig().getConfigurationSection("types").getString(type + ".exec"));
            }
        }
    
        public String getGiveaway(String s) {
            return giveaways.get(s);
        }
    
    
    }
     

Share This Page