package com.elmakers.mine.bukkit.data;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.bukkit.configuration.file.YamlConfiguration;

public class YamlDataFile extends YamlConfiguration {
    protected final File file;
    protected final Logger logger;
    protected final boolean checkBackupSize;

    public YamlDataFile(Logger logger, File file) {
        this(logger, file, true);
    }

    public YamlDataFile(Logger logger, File file, boolean checkBackupSize) {
        this.file = file;
        this.logger = logger;
        this.checkBackupSize = checkBackupSize;
    }

    public YamlDataFile(Logger logger, String filename) {
        file = new File(filename);
        this.logger = logger;
        checkBackupSize = true;
    }

    public void load() {
        try {
            load(file);
        } catch (Exception ex) {
            logger.warning(ex.getMessage());
            logger.warning("Error loading data file " + file.getName());
        }
    }

    public void save() {
        try {
            File tempFile = new File(file.getAbsolutePath() + ".tmp");
            if (tempFile.exists()) {
                logger.warning("Temp file for " + file.getName() + "exists, deleting");
                tempFile.delete();
            }
            File backupFile = new File(file.getAbsolutePath() + ".bak");
            save(tempFile);

            if (checkBackupSize && backupFile.exists() && backupFile.length() > tempFile.length()) {
                logger.info("Backup file " + backupFile.getName() + " is larger than " + tempFile.getName());
                int index = 1;
                File saveBackup = new File(backupFile.getAbsolutePath() + "." + index);
                while (saveBackup.exists()) {
                    index++;
                    saveBackup = new File(backupFile.getAbsolutePath() + "." + index);
                }
                logger.info("This may be normal, but just in case the backup file will be saved permanently as " + saveBackup.getName());
                logger.info("If data seems missing, please restore the backup file while the server is shut down");
                backupFile.renameTo(saveBackup);
            }

            if (backupFile.exists()) {
                backupFile.delete();
            }
            File targetFile = new File(file.getAbsolutePath());
            if (targetFile.exists()) {
                targetFile.renameTo(backupFile);
            }
            tempFile.renameTo(file);
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Error saving data file " + file.getName(), ex);
        }
    }

    public File getFile() {
        return file;
    }
}

@@ -54,16 +54,89 @@
 o.File;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Map;%0A
 import j
@@ -229,17 +229,8 @@
 ion.
-file.Yaml
 Conf
@@ -238,16 +238,140 @@
 guration
+Section;%0Aimport org.bukkit.configuration.file.YamlConfiguration;%0A%0Aimport com.elmakers.mine.bukkit.utility.ConfigurationUtils
 ;%0A%0Apubli
@@ -2902,11 +2902,1145 @@
 e;%0A    %7D
+%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    private Object convert(Object value) %7B%0A        if (value instanceof List) %7B%0A            List%3C?%3E list = (List%3C?%3E)value;%0A            if (!list.isEmpty()) %7B%0A                List%3CObject%3E newList = new ArrayList%3C%3E();%0A                for (Object o : list) %7B%0A                    newList.add(convert(o));%0A                %7D%0A                value = newList;%0A            %7D%0A        %7D else if (value instanceof ConfigurationSection) %7B%0A            value = ConfigurationUtils.toMap((ConfigurationSection)value);%0A        %7D%0A        if (value instanceof Map) %7B%0A            Map%3CString, Object%3E map = (Map%3CString, Object%3E)value;%0A            for (Map.Entry%3CString, Object%3E entry : map.entrySet()) %7B%0A                entry.setValue(convert(entry.getValue()));%0A            %7D%0A        %7D%0A        return value;%0A    %7D%0A%0A    public void set(String path, Object value) %7B%0A        // Stop screwing up the config code Spigot :(%0A        // Specifically lists no longer get serialized since the %22we need to save comments%22%0A        // refactor apocalypse in 1.18%0A        value = convert(value);%0A        super.set(path, value);%0A    %7D
 %0A%7D%0A

Work around Spigot list serialization bug