Config not reloading

Discussion in 'Plugin Development' started by Synapz, May 11, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Synapz

    I am trying to add an in game command (/pb admin reload) to be able to reload the configuration without having to type /reload and reload all the plugins. So far this is what I have:

    Code:
    public class Settings {
    
    private static Settings settings = new Settings();
    
    private Paintball pb;
    private FileConfiguration config, arena;
    private File aFile;
    
    private String prefix, version; // etc...
    
    private Settings() {}
    
    public static Settings getSettings() {
    return settings;
    }
    
    public void init(Paintball pb) {
        if (!pb.getDataFolder().exists()) {
            pb.getDataFolder().mkdir();
    }
    
        this.pb = pb;
        pb.saveDefaultConfig();
        config = pb.getConfig();
    
        aFile = new File(pb.getDataFolder(), "arenas.yml");
    
        if (!aFile.exists()) {
            try {
                aFile.createNewFile();
           } catch (IOException e) { 
                Message.getMessenger().msg(Bukkit.getConsoleSender(), ChatColor.RED, "", "Could not create                             arenas.yml.      Stack trace: ");
                e.printStackTrace();
           }
        }
    
        arena = YamlConfiguration.loadConfiguration(aFile);
        loadValues();
    }
    
    public void reloadConfig() {
        arena = YamlConfiguration.loadConfiguration(aFile);
        pb.reloadConfig();
        config = pb.getConfig();
        loadValues();
    }
    
    
    private void loadValues() {
        prefix = ChatColor.translateAlternateColorCodes('&', config.getString("prefix"));
        version = pb.getDescription().getVersion();
        // other values...
    }
    

    I have the reloadConfig() hooked up to my Reload Config command, however when I edit values in config.yml then type /pb admin reload it says 'All Settings Reloaded Successfully' but, none of the settings get updated. If I type /reload then it works. Anyone know what I am doing wrong?

    P.S - sorry for the indentation, it got screwed up when i pasted it so I had to manually fix it....
     
  2. Offline

    nbrandwine

    1. public void reloadCustomConfig() {
    2. if (customConfigFile == null) {
    3. customConfigFile = new File(getDataFolder(), "customConfig.yml");
    4. }
    5. customConfig = YamlConfiguration.loadConfiguration(customConfigFile);

    6. // Look for defaults in the jar
    7. InputStream defConfigStream = this.getResource("customConfig.yml");
    8. if (defConfigStream != null) {
    9. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    10. customConfig.setDefaults(defConfig);
    11. }
    12. }
    set defaults, maybe
     
  3. Offline

    Synapz

    @nbrandwine
    Tried it, didn't work. I belive plugin.saveDefaultConfig() is the same as that except that's for a custom config. I am not using a custom config, just the regular config.yml. However nothing I try works, pb.reloadConfig(), pb.saveConfig(), pb.getConfig() etc....
     
  4. Offline

    caderape

    @Synapz
    SaveDefautConfig(); on your enable for create the file.
    SaveConfig(); for save changement
    ReloadConfig(); for reload the file
    (Only accessible by your main class)
     
  5. Offline

    Synapz

    @caderape
    All those are not only accessible by your main class. If in your onEnable() you pass your main class as a parameter you can access those methods throughout different classes. Which is what I did. In my onEnable, Settings.getSettings().init(this). Unless I understand this wrong...
     
  6. Offline

    caderape

    @Synapz i think you are.
    Just create a constructor in your other class.

    MainClass:
    public Settings settings;
    public void onEnable() { settings = new Settings(this); } (this is your main class)

    And on your created class,

    PintBall plugin;
    public Settings(Paintball plugin) {
    this.plugin = plugin;}
     
  7. Offline

    Synapz

    @caderape
    But I already did that... Instead of using a constructor though I used the init(Paintball pb) method (in Settings class) because I had to private Settings-- so no one can create more than Settings instance otherwise there would be a bunch of different instances everywhere :O.

    But that is my bad because I didn't show you my main class, sorry. Here it is now,
    Code:
    public class Paintball extends JavaPlugin {
    
        @Override
        public void onEnable() {
            Settings.getSettings().init(this);
            ArenaManager.getArenaManager().setup();
            CommandManager commandManager = new CommandManager();
            commandManager.init();
    
            getCommand("paintball").setExecutor(commandManager);
        }
    
        @Override
        public void onDisable() {
        }
    
    }
    Still, the reloadConfig() in my Settings class (see top post for the code in it) has pb.reloadConfig() and it still hasn't reloaded the config's values on /pb admin reload. And I have done basically everything you said in my main post just in a different format (without constructors)...
     
    Last edited: May 11, 2015
  8. Offline

    caderape

    @Synapz You don't need a method getsettings and init
    Just put on your onEnable 'new Settings(this)'

    private Settings() {}

    change this for :
    pubic Settings(PaintBall plugin) {
    pb = plugin;
    saveDefautConfig(); (this will create the file, but that not override existing file, so no need to check if the folder exist)
    config = pb.getconfig();
    }

    A constructor must be public, i think.
    no need to load or something, it's already loaded. SaveDefautConfig do all the job.

    Now for custom file,
    Code:
    public File arenaf;
    public Fileconfiguration arena;
    
    public void loadArena() {
    areanf = new File(getDatafolder(), "arena.yml")
    if (!arenaf.exists() {
    arenaf.getparentFile().mkdirs();
    try arenaf.createnewfile();
    }
    areana = yamlconfiguration.loadconfiguration(arenaf)
    }
    For save data and get it back, you need a save method ( try arena.save(areanaf) )
    And call the method loadarena() for reload;
     
  9. Offline

    Synapz

    @caderape
    I need getSettings() as a static method to access the one Settings instance I created. Using the init() in my onEnable() will do the same thing as if I posted the code from the init() into the onEnable().

    Private constructors = ensuring there is only one instance of the class lying around. If you notice the
    Code:
    private static Settings settings = new Settings() 
    I am already creating one instance, which goes into getSettings(), in the onEnable(), that calls the init() which calls the this.pb = pb, pb.saveDefaultConfig() etc.. I see what you are saying but everything your telling me to do I already have done (In a different way) and isn't really solving my problem...
     
  10. Offline

    caderape

    @Synapz

    private static Settings settings = new Settings();

    I'm not sure this can work. instance the class in the class. If you see what i mean.

    You create the class settings on your enable, but everytime you will call your getSettings, it will recreate again, i think.
     
  11. Offline

    Synapz

    @caderape
    It works :p. Reloading config is what isn't working
     
  12. Offline

    caderape

    @Synapz
    This is how i would do it. You might find a solution with it.

    Code:
    public class TestMain  extends JavaPlugin {
    
        public void onEnable()
        {
            saveDefaultConfig();
            new Settings(this);
        }    
    Code:
    public class Settings {
    
        private TestMain plugin;
        private static Settings settings;
        private FileConfiguration arena, config;
        private File arenaf;
      
      
        public Settings(TestMain plugin)
        {
            settings = this;
            this.plugin = plugin;
            this.config = plugin.getConfig();
            LoadArena();
        }
    
      
      
        public void LoadArena() {
            arenaf = new File(plugin.getDataFolder(), "arena.yml");
            if (!arenaf.exists()) {
                arenaf.getParentFile().mkdirs();
                try {arenaf.createNewFile();} catch (IOException e) {e.printStackTrace();}
            }
            arena = YamlConfiguration.loadConfiguration(arenaf);
        }
      
      
      
      
      
      
        public void ReloadeArenas()
        {
            try {arena.save(arenaf);} catch (IOException e) {e.printStackTrace();}
            LoadArena();
        }
      
      
        public void ReloadConfig()
        {
            plugin.saveConfig();
            plugin.reloadConfig();
        }
    
    
    
      
        public static Settings getSettings() {
            return settings;
        }
    
      
        public FileConfiguration getConfig() {
            return this.config;
        }
      
        public FileConfiguration getArena() {
            return this.arena;
        }
      
      
        public File getArenaFile() {
            return this.arenaf;
        }
    }
     
    Last edited: May 11, 2015
    nbrandwine likes this.
  13. Offline

    Synapz

    @caderape
    Thanks! Just the thing is, even if I use that way, my reload config and your reload config are pretty much the same, just in different structure (i.e. you have constructors & different method's + names). So I don't see what changing the structure of the class would help with reloading the config values. It's like saying if you put everything on one line maybe it will work, when it will work the same way no matter how you indent. The only real difference I see would be before the plugin.reloadConfig() you added plugin.saveConfig(). Not sure if that could be the simple mistake I made, I'll try it tomorrow though to make sure it isn't. But I will defiantly change my code around to make it look similar with those extra methods you added. Thanks a lot! Although still not 100% sure that this will work.
     
Thread Status:
Not open for further replies.

Share This Page