net.bitjump.launchme.utils.FileConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.bitjump.launchme.utils.FileConfig.java

Source

/*
 * Copyright (C) 2016 AlternaCraft
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.bitjump.launchme.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import net.bitjump.launchme.LaunchMe;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

/**
 * Custom class for working better with the main config file. These are the
 * capabilities:
 * <ul>
 *  <li>Saving config with comments between lines</li>
 *  <li>Checking config version with internal version for checking changes
 *      <ul>
 *          <li>Setting params from previous config into the new one</li>
 *      </ul>
 *  </li>
 * </ul>
 *
 * @see FileConfiguration
 */
public class FileConfig {

    private LaunchMe plugin = null;

    private FileConfiguration configFile = null;
    private File backupFile = null;

    private String before = "";

    public FileConfig() {
        plugin = LaunchMe.instance;

        File cfile = new File(new StringBuilder().append(plugin.getDataFolder()).append(File.separator)
                .append("config.yml").toString());

        if (!cfile.exists() || mismatchVersion(cfile)) {
            plugin.saveDefaultConfig();
            if (backupFile != null) {
                copyParams(cfile);
                backupFile = null;
            }
        }

        plugin.reloadConfig();
        configFile = plugin.getConfig();
    }

    private boolean mismatchVersion(File cFile) {
        File bFile = new File(new StringBuilder().append(plugin.getDataFolder()).append(File.separator)
                .append("config.backup.yml").toString());

        YamlConfiguration configV = YamlConfiguration.loadConfiguration(cFile);

        if (!configV.contains("version")
                || !configV.getString("version").equals(plugin.getConfig().getDefaults().getString("version"))) {

            if (bFile.exists()) {
                bFile.delete();
            }

            cFile.renameTo(bFile);
            MessageUtils.showMessage(ChatColor.RED + "Mismatch config version, a new one has been created.");

            backupFile = bFile;

            return true;
        }

        return false;
    }

    private void copyParams(File outFile) {
        YamlConfiguration newFile = YamlConfiguration.loadConfiguration(outFile);
        YamlConfiguration oldFile = YamlConfiguration.loadConfiguration(backupFile);

        File temp = new File(new StringBuilder().append(plugin.getDataFolder()).append(File.separator)
                .append("config_temp.yml").toString());

        try (BufferedReader br = new BufferedReader(new FileReader(outFile));
                FileWriter fw = new FileWriter(temp)) {

            String linea;
            while ((linea = br.readLine()) != null) {
                if (linea.matches("\\s*-\\s?.+") && !linea.contains("#")) {
                    continue;
                }
                String nline = replace(linea, newFile, oldFile);
                fw.write(nline);
            }
        } catch (IOException ex) {
            OMBLogger.log(Level.SEVERE, ex.getMessage());
        }

        if (outFile.exists()) {
            outFile.delete();
        }

        temp.renameTo(outFile);
        MessageUtils.showMessage(
                ChatColor.GREEN + "Previous file settings have been established " + "into the new one.");
        MessageUtils.showMessage(ChatColor.GREEN + "Just in case, check the result.");
    }

    private String replace(String line, YamlConfiguration newFile, YamlConfiguration oldFile) {
        String resul = line;

        for (String value : newFile.getKeys(true)) {
            if (value.equals("version")) // Este parametro no se toca
            {
                continue;
            }

            String cValue = value;
            String spaces = ""; // Estilo

            if (value.contains(".")) {
                String[] vals = value.split("\\.");
                cValue = vals[vals.length - 1];

                int i = 0;
                while (i < StringUtils.countMatches(value, ".")) {
                    spaces += "    "; // Style format
                    i++;
                }
            }

            if (line.contains(cValue + ":")) {
                Object v = null;
                // Previous structure
                if (oldFile.contains(before + "." + cValue)) {
                    v = oldFile.get(before + "." + cValue);
                } else if (newFile.contains(before + "." + cValue)) {
                    v = newFile.get(before + "." + cValue);
                    // New structure
                } else if (oldFile.contains(value)) {
                    v = oldFile.get(value);
                } else {
                    v = newFile.get(value);
                }

                resul = spaces + cValue + ":";

                if (v instanceof List) {
                    List<Object> vs = (List<Object>) v;
                    for (Object v1 : vs) {
                        String val = getFilteredString(v1.toString());
                        resul += System.lineSeparator() + spaces + "- " + val;
                    }
                } else if (!(v instanceof MemorySection)) {
                    resul += " " + getFilteredString(v.toString());
                } else if (v instanceof MemorySection) {
                    before = value;
                }

                resul += System.lineSeparator();
                break;
            }
        }

        return (resul.equals(line) ? resul + System.lineSeparator() : resul);
    }

    public FileConfiguration getConfig() {
        return this.configFile;
    }

    private String getFilteredString(String str) {
        List<Character> special = Arrays.asList(':', '{', '}', '[', ']', ',', '&', '*', '#', '?', '|', '<', '>',
                '=', '!', '%', '@', '\\');

        for (Character character : special) {
            if (str.contains(String.valueOf(character))) {
                return "\"" + str + "\"";
            }
        }

        return str;
    }
}