Config overwrites itself

Discussion in 'Plugin Development' started by jokie666, May 8, 2013.

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

    jokie666

    Hi everybody,

    I am almost ready with my plugin, I have only 1 problem.
    I made some configs and stuff but everytime I reload the server the whole config gets overwritten with the default.
    Here is my code:
    Code:
       
     
    public void loadConfiguration(){
              if (getConfig().options().copyDefaults() == false){
                  getConfig().options().copyDefaults(true);
                  saveConfig();
                  log(Level.INFO,"copyconfig");
              }
              if (messageConfigFile == null){
                  messageConfigFile = new File(getDataFolder(), "Messages.yml");
                  log(Level.INFO,"copy1");
              }
         
              messageConfig = YamlConfiguration.loadConfiguration(messageConfigFile);
           
                if(!messageConfig.isString("ConsolemessageOnlyRunByPlayer")) {
                    messageConfig.set("ConsolemessageOnlyRunByPlayer", "This command can only be run by a player.");
                    messageConfig.set("notAllowedToDoThisMessage", "You don't have the permission to do this!");
                    messageConfig.set("howToUseItMessage", "usage /nick {player} {nickname}");
                }
           
                  runByPlayer = messageConfig.getString("ConsolemessageOnlyRunByPlayer");
                  notAllowed = messageConfig.getString("notAllowedToDoThisMessage");
                  usage = messageConfig.getString("howToUseItMessage");
               
                try{
                    messageConfig.save(messageConfigFile);
                } catch(Exception e) {
                    e.printStackTrace();
                }
             
     
               
                  if (tntConfigFile == null){
                      tntConfigFile = new File(getDataFolder(), "Tnt.yml");
                      log(Level.INFO,"tntcopy");
                  }
             
                  tntConfig = YamlConfiguration.loadConfiguration(tntConfigFile);
               
                  if(!tntConfig.isString("enabled")) {
                      tntConfig.set("enabled", true);
                  }
                  try{
                      tntConfig.save(tntConfigFile);
                  } catch(Exception e) {
                      e.printStackTrace();
                  }
               
                  if (lavaConfigFile == null){
                      lavaConfigFile = new File(getDataFolder(), "Lava.yml");
                      log(Level.INFO,"lavacopy");
                  }
             
                  lavaConfig = YamlConfiguration.loadConfiguration(lavaConfigFile);
               
                  if(!lavaConfig.isString("enabled")) {
                      lavaConfig.set("enabled", true);
                  }
                  try{
                      lavaConfig.save(lavaConfigFile);
                  } catch(Exception e) {
                      e.printStackTrace();
                  }
               
                  if (waterConfigFile == null){
                      waterConfigFile = new File(getDataFolder(), "Water.yml");
                      log(Level.INFO,"watercopy");
                  }
             
                  waterConfig = YamlConfiguration.loadConfiguration(waterConfigFile);
               
                  if(!waterConfig.isString("enabled")) {
                      waterConfig.set("enabled", true);
                  }
                  try{
                      waterConfig.save(waterConfigFile);
                  } catch(Exception e) {
                      e.printStackTrace();
                  }
               
                  if (igniterConfigFile == null){
                      igniterConfigFile = new File(getDataFolder(), "Igniter.yml");
                      log(Level.INFO,"ignitercopy");
                  }
             
                  igniterConfig = YamlConfiguration.loadConfiguration(igniterConfigFile);
               
                  if(!igniterConfig.isString("enabled")) {
                      igniterConfig.set("enabled", true);
                  }
                  try{
                      igniterConfig.save(igniterConfigFile);
                  } catch(Exception e) {
                      e.printStackTrace();
                  }
                  if (spawnerConfigFile == null){
                      spawnerConfigFile = new File(getDataFolder(), "Igniter.yml");
                      log(Level.INFO,"spawnercopy");
                  }
             
                  spawnerConfig = YamlConfiguration.loadConfiguration(spawnerConfigFile);
               
                  if(!spawnerConfig.isString("enabled")) {
                      spawnerConfig.set("enabled", true);
                  }
                  try{
                      spawnerConfig.save(spawnerConfigFile);
                  } catch(Exception e) {
                      e.printStackTrace();
                  }
                  if (bedrockConfigFile == null){
                      bedrockConfigFile = new File(getDataFolder(), "Bedrock.yml");
                      log(Level.INFO,"copy1");
                  }
             
                  bedrockConfig = YamlConfiguration.loadConfiguration(bedrockConfigFile);
               
                  if(!bedrockConfig.isString("enabled")) {
                      bedrockConfig.set("enabled", true);
                  }
                  try{
                      bedrockConfig.save(bedrockConfigFile);
                  } catch(Exception e) {
                      e.printStackTrace();
                  }
                  if (redstoneConfigFile == null){
                      redstoneConfigFile = new File(getDataFolder(), "Redstone.yml");
                      log(Level.INFO,"copy1");
                  }
             
                  redstoneConfig = YamlConfiguration.loadConfiguration(redstoneConfigFile);
               
                  if(!redstoneConfig.isString("enabled")) {
                      redstoneConfig.set("enabled", true);
                  }
                  try{
                      redstoneConfig.save(redstoneConfigFile);
                  } catch(Exception e) {
                      e.printStackTrace();
                  }
          }
    Can somebody see what I am doing wrong?
     
  2. Offline

    Adriani6

    I am not familiar with JAVA but...

    getConfig().options().copyDefaults(true);

    Explain actions of this ? :D
     
  3. Offline

    kreashenz

    saveResource("name.yml", false); That works, I think.
     
  4. Offline

    nggmc

    Code:
    	    public void createConfig() {
    	    	  File file = new File(getDataFolder() + File.separator + "config.yml");
    	    	  if (!file.exists()) {
    	    	   getConfig().options().copyDefaults(true);
    	    	   saveConfig();
    	    	  }
    	    }
    
     
  5. Offline

    jokie666

    it creates a default config,

    This works for creating a default config but I can't get it rewritten that it creates custom configs like "messages.yml"

    off-topic: Sorry for the late reaction, yesterday I was the whole day out with friends and today I had a soccer tournament.
     
  6. Offline

    Barinade

    File f = new File(pathToConfig);
    if (!f.exists()) {
    //create
    }
     
Thread Status:
Not open for further replies.

Share This Page