I have been working for files for the longest time now, and this extremely weird bug has come up. 1. I copy the file from jar into the "Plugin Folder" 2. I load my FileConfiguration with YamlConfiguration 3. I check if anything is in it, It says there's nothing in it by using this.config.getSection("") 4. I checked the file (The entire configuration is there. 5. I set a value with set("Test", "test"); 6. I go back and check and the entire file was erased except the Test:test 7. I check if anything is in it, It says the only value now is what I have set. I have no idea what is going on at all. My YamlDatabase: http://pastebin.com/EmH8t87J My Configuration: http://pastebin.com/5bBMNfPZ Thanks in advance.
You may be overriding the current default values somehow. When you set the value, what do you use to save it? From what I understand you want to have a class manage on each new instance a new config file. I've got something similar. Code:java /****/package com.katsaroucraft.gopaintman; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import org.bukkit.configuration.file.FileConfiguration;import org.bukkit.configuration.file.YamlConfiguration;import org.bukkit.plugin.java.JavaPlugin; /*** @author Gopaintman**/public class AuxFileUtil { private JavaPlugin plugin; private FileConfiguration fileConfig; private File file; private String fileName; public AuxFileUtil(JavaPlugin instance, String fileName) { this.plugin = instance; this.fileName = fileName; file = new File(plugin.getDataFolder(), fileName.toLowerCase() + ".yml"); fileConfig = YamlConfiguration.loadConfiguration(file); if(!file.exists()){ try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void setValue(String path, Object value){ fileConfig.set(path, value); saveConfig(); } public void setValueNoSave(String path, Object value){ fileConfig.set(path, value); } public void createFile(){ if(file.exists()){ return; } try { file.createNewFile(); fileConfig.addDefaults(fileConfig); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void reloadConfig() { if (file == null) { file = new File(plugin.getDataFolder(), fileName.toLowerCase() + ".yml"); this.createFile(); } fileConfig = YamlConfiguration.loadConfiguration(file); try { InputStream defConfigStream = new FileInputStream(file); if (defConfigStream != null) { YamlConfiguration defConfig = YamlConfiguration .loadConfiguration(file); fileConfig.setDefaults(defConfig); defConfigStream.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void saveConfig() { if(fileConfig == null || file == null){ return; } try { fileConfig.save(file); } catch (IOException e) { plugin.getLogger().severe("Could not save file: " + fileName + ".yml"); e.printStackTrace(); } } /** * @return the fileConfig */ public FileConfiguration getConfig() { return fileConfig; } /** * @return the file */ public File getFile() { if(file.exists()){ return file; }else{ createFile(); return file; } } /** * @return the fileName currently held in RAM. May not be real filename */ public String getFileName() { return fileName; }}
Gopaintman Hey dude all I did was remove these two methods and it's works fine. But I don't get how it did because these methods were never called in the class or the instance of the class. /*public void changeFile(String fileName) { this.changeFile(fileName, this.plugin.getDataFolder().getPath()); } public void changeFile(String fileName, String fileLocation) { this.fileName = fileName; this.fileLocation = fileLocation; this.onStartUp(); }*/
So if you just remove them they work? Try adding them back, maybe you did something else and it fixed it.