Search in sources :

Example 11 with CommentedConfiguration

use of com.palmergames.bukkit.config.CommentedConfiguration in project TownyCultures by TownyAdvanced.

the class Settings method setDefaults.

/**
 * Builds a new config reading old config data.
 */
private static void setDefaults(String version, File file) {
    newConfig = new CommentedConfiguration(file.toPath());
    newConfig.load();
    for (ConfigNodes root : ConfigNodes.values()) {
        if (root.getComments().length > 0)
            addComment(root.getRoot(), root.getComments());
        if (root.getRoot() == ConfigNodes.VERSION.getRoot())
            setNewProperty(root.getRoot(), version);
        else if (root.getRoot() == ConfigNodes.LAST_RUN_VERSION.getRoot())
            setNewProperty(root.getRoot(), getLastRunVersion(version));
        else
            setNewProperty(root.getRoot(), (config.get(root.getRoot().toLowerCase()) != null) ? config.get(root.getRoot().toLowerCase()) : root.getDefault());
    }
    config = newConfig;
    newConfig = null;
}
Also used : CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration)

Example 12 with CommentedConfiguration

use of com.palmergames.bukkit.config.CommentedConfiguration in project TownyCultures by TownyAdvanced.

the class Settings method loadConfig.

public static void loadConfig(String filepath, String version) throws IOException {
    if (FileMgmt.checkOrCreateFile(filepath)) {
        File file = new File(filepath);
        // read the config.yml into memory
        config = new CommentedConfiguration(file.toPath());
        if (!config.load())
            TownyCultures.severe("Failed to load Config!");
        setDefaults(version, file);
        config.save();
    }
}
Also used : CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration) File(java.io.File)

Example 13 with CommentedConfiguration

use of com.palmergames.bukkit.config.CommentedConfiguration in project TownyCultures by TownyAdvanced.

the class Translation method loadLanguage.

// This will read the language entry in the config.yml to attempt to load
// custom languages
// if the file is not found it will load the default from resource
public static void loadLanguage(String filepath, String defaultRes) throws IOException {
    String res = Settings.getString(ConfigNodes.LANGUAGE.getRoot(), defaultRes);
    String fullPath = filepath + File.separator + res;
    File file = FileMgmt.unpackResourceFile(fullPath, res, defaultRes);
    // read the (language).yml into memory
    language = new CommentedConfiguration(file.toPath());
    language.load();
    HelpMenu.loadMenus();
    CommentedConfiguration newLanguage = new CommentedConfiguration(file.toPath());
    try {
        newLanguage.loadFromString(FileMgmt.convertStreamToString("/" + res));
    } catch (IOException e) {
        TownyCultures.info("Lang: Custom language file detected, not updating.");
        TownyCultures.info("Lang: " + res + " v" + Translation.of("version") + " loaded.");
        return;
    } catch (InvalidConfigurationException e) {
        TownyCultures.severe("Invalid Configuration in language file detected.");
    }
    String resVersion = newLanguage.getString("version");
    String langVersion = Translation.of("version");
    if (!langVersion.equalsIgnoreCase(resVersion)) {
        language = newLanguage;
        TownyCultures.info("Lang: Language file replaced with updated version.");
        FileMgmt.stringToFile(FileMgmt.convertStreamToString("/" + res), file);
    }
    TownyCultures.info("Lang: " + res + " v" + Translation.of("version") + " loaded.");
}
Also used : CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration) IOException(java.io.IOException) File(java.io.File) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 14 with CommentedConfiguration

use of com.palmergames.bukkit.config.CommentedConfiguration in project SiegeWar by TownyAdvanced.

the class MigrationUtil method readInConfigFileMigrationFields.

/**
 * Read in the config fields to migrate
 */
public static boolean readInConfigFileMigrationFields() {
    try {
        String configFilePath = SiegeWar.getSiegeWar().getDataFolder().getPath() + File.separator + "config.yml";
        if (FileMgmt.checkOrCreateFile(configFilePath)) {
            File file = new File(configFilePath);
            // read the config.yml into memory
            CommentedConfiguration config = new CommentedConfiguration(file.toPath());
            if (!config.load()) {
                throw new TownyException("Failed to load existing config file.");
            }
            // Read in migration fields
            for (ConfigFileMigrationField migrationField : migrationFields) {
                // Read if old field exists AND is not dummy
                if (config.contains(migrationField.oldKey) && config.getComments(migrationField.oldKey).size() > 0) {
                    migrationField.value = config.get(migrationField.oldKey);
                }
            }
        }
        SiegeWar.info("Successfully read existing config file.");
        return true;
    } catch (Exception e) {
        SiegeWar.severe(e.getMessage());
        e.printStackTrace();
        return false;
    }
}
Also used : CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration) File(java.io.File) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 15 with CommentedConfiguration

use of com.palmergames.bukkit.config.CommentedConfiguration in project SiegeWar by TownyAdvanced.

the class MigrationUtil method migrateConfigFileFields.

/**
 * Migrate config fields
 */
public static boolean migrateConfigFileFields() {
    try {
        int numMigratedFields = 0;
        CommentedConfiguration config = Settings.getConfig();
        // migrate fields
        for (ConfigFileMigrationField migrationField : migrationFields) {
            if (migrationField.value != null) {
                config.set(migrationField.newKey, migrationField.value);
                numMigratedFields++;
            }
        }
        if (numMigratedFields > 0) {
            // Save to disk
            config.save();
            SiegeWar.info("Successfully migrated " + numMigratedFields + " fields.");
        }
        return true;
    } catch (Exception e) {
        SiegeWar.severe(e.getMessage());
        e.printStackTrace();
        return false;
    }
}
Also used : CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

CommentedConfiguration (com.palmergames.bukkit.config.CommentedConfiguration)16 File (java.io.File)7 IOException (java.io.IOException)3 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)2 Path (java.nio.file.Path)2 ConfigNodes (com.palmergames.bukkit.config.ConfigNodes)1 ConfigMigrator (com.palmergames.bukkit.config.migration.ConfigMigrator)1 TownyInitException (com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException)1 InputStream (java.io.InputStream)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 ArrayList (java.util.ArrayList)1 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)1