Config Saving Problem

Discussion in 'Plugin Development' started by MnMaxon, Aug 16, 2012.

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

    MnMaxon

    I don't thing my config is saving, can someone please help me?
    It's my first time using a config that's the default config.yml

    My Main class:
    Code:
    public class Main extends JavaPlugin {
        public final CustomConfig CC = new CustomConfig(this);
        public static Forum plugin;
        public static File kNum = new File("plugins/4 Kingdoms/Kingdoms/1.yml");
     
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("Test")) {
                sender.sendMessage("Null :(");
                CC.getConfig(kNum).set("test", 1);
                CC.saveConfig(kNum);
            }
            return false;
        }
    }
    My CustomConfig class:
    Code:
    public class CCForum {
        private FileConfiguration customConfig;
        public static Main plugin;
     
        public  CustomConfig (Main instance) {
            CCForum.plugin = instance;
        }
     
        public FileConfiguration getConfig(File FILE) {
            customConfig = YamlConfiguration.loadConfiguration(FILE);
            return customConfig;
        }
     
        public void saveConfig(File customConfigFile) {
            customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
            if (customConfig == null || customConfigFile == null) {
                return;
            }
            try {
                customConfig.save(customConfigFile);
            } catch (IOException ex) {
                plugin.getLogger().log(Level.SEVERE,
                        "Could not save config to " + customConfigFile, ex);
            }
        }
    }
    There is no error. When I try to use the plugin to get the value of "test", it returns null, and when I go into the config file, "test" does not show up.
     
  2. First of all you shouldn't use
    Code:
    new File("plugins/4 Kingdoms/Kingdoms/1.yml");
    Use this instead
    Code:
    new File(getDataFolder() + "/1.yml");// you cannot use getDataFolder() in static members
    Beware that getDataFolder() can only be used on JavaPlugin objects.

    Second of all...
    Your saveConfig method loads the file, before it saves it. That doesn't make sense.

    For example...

    it would make more sense, if you changed this
    Code:
    public void saveConfig(File customConfigFile) {
    to this
    Code:
    public void saveConfig(FileConfiguration customConfig, File customConfigFile) {
    If you know what I mean :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
    MnMaxon likes this.
  3. Offline

    MnMaxon

    Thanks, I found the problem. I didn't know that I was loading the file, then directly saving it without doing anything.
     
Thread Status:
Not open for further replies.

Share This Page