Prevent config values from being overwritten on restart

Discussion in 'Plugin Development' started by Broeder_Mc_Henk, Sep 8, 2021.

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

    Broeder_Mc_Henk

    I have a plugin where a "cloud" of explosions follows the player, destroys him.

    The problem is that when the user has edited the config.yml and the server is restarted, al the config will be overwritten to the default values.

    I have tried assigning values using ".AddDeafult" and ".set", saving the config via saveConfig and saveDefaultConfig and looked around for simular problems.

    The code:
    Code:
        public void createDefaultConfig() {
            FileConfiguration config = this.plugin.getConfig();
    
            ConfigurationSection killerCloudConfig = config.createSection("killerCloud");
            killerCloudConfig.addDefault("enabled", true);
            killerCloudConfig.addDefault("setFire", true);
            killerCloudConfig.addDefault("breakBlocks", false);
            killerCloudConfig.addDefault("moveSpeed", 2);
            killerCloudConfig.addDefault("explosionRadius", 10);
            killerCloudConfig.addDefault("maxDistanceToPlayer", 50);
    
            config.options().copyDefaults(true);
         
            plugin.saveConfig();
        }
    Ideal situation:

    - servers starts: config.yml does not exist and is created with default values.
    - user edits the config.yml
    - server starts: config.yml is loaded with the values the user specified.
     
  2. Offline

    Strahan

    I have the result you label ideal, and I got it by just having this in my startup:
    Code:
    @Override
    public void onEnable() {
      // other crap here
    
      saveDefaultConfig();
    }
    and by having my populated config.yml embedded in the plugin. Works exactly as you laid out. I don't fiddle around with any other code, a simple call to saveDefaultConfig does it.
     
    Broeder_Mc_Henk likes this.
  3. Offline

    Broeder_Mc_Henk

    I did just that. Still overwrites the user specified values with the default values.
    Here is the complete (changed) code:
    Code:
    #Main class
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            // other crap here
            new PluginConfig(this).createDefaultConfig();
            saveDefaultConfig();
            }
    }
    
    #PluginConfig class
    
    public class PluginConfig {
        private Main plugin;
    
        public PluginConfig(Main plugin) {
            this.plugin = plugin;
        }
    
        public void createDefaultConfig() {
            FileConfiguration config = this.plugin.getConfig();
    
    
            ConfigurationSection killerCloudConfig = config.createSection("killerCloud");
            killerCloudConfig.set("enabled", true);
            killerCloudConfig.addDefault("setFire", true);
            killerCloudConfig.addDefault("breakBlocks", false);
            killerCloudConfig.addDefault("moveSpeed", 2);
            killerCloudConfig.addDefault("explosionRadius", 10);
            killerCloudConfig.addDefault("maxDistanceToPlayer", 50);
    
            config.options().copyDefaults(true);
       
            plugin.saveDefaultConfig();
        }
    
    
    }
    
    
     
    Last edited: Sep 8, 2021
  4. Offline

    Strahan

    When I said other crap here in my example, I meant basic command executors and listener registration. You snuck in configuration related code there, heh, so yea.. not the same thing.

    The ONLY configuration related thing I do is a call to saveDefaultConfig(). When I do, the server checks if config.yml exists. If it does not, it throws the plugin embedded one into the plugin folder. If it does exist, it leaves it alone, not overwriting anything. Granted, it means if some dumbass admin totally deletes a key it won't regenerate it, but then... I don't code to coddle idiots lol.
     
    Broeder_Mc_Henk likes this.
  5. Offline

    davidclue

    @Broeder_Mc_Henk The only things you need in your onEnable method are these
    Code:
    public void onEnable() {
        this.getConfig().options().copyDefaults(true);
        saveDefaultConfig();
    }
     
    Broeder_Mc_Henk likes this.
Thread Status:
Not open for further replies.

Share This Page