Search in sources :

Example 6 with CommentedConfiguration

use of com.palmergames.bukkit.config.CommentedConfiguration in project Towny by ElgarL.

the class TownySettings method loadConfig.

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

Example 7 with CommentedConfiguration

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

the class Towny method checkForLegacyDatabaseConfig.

/**
 * Converts the older config.yml's database settings into the database.yml file.
 * @return true if successful
 * @since 0.97.0.24
 */
private boolean checkForLegacyDatabaseConfig() {
    Path configYMLPath = getDataFolder().toPath().resolve("settings").resolve("config.yml");
    // Bail if the config doesn't exist at all yet.
    if (!Files.exists(configYMLPath))
        return true;
    CommentedConfiguration config = new CommentedConfiguration(configYMLPath);
    // return false if the config cannot be loaded.
    if (!config.load())
        return false;
    if (config.contains("plugin.database.database_load")) {
        /*
			 * Get old settings from config.
			 */
        String dbload = config.getString("plugin.database.database_load");
        String dbsave = config.getString("plugin.database.database_save");
        String hostname = config.getString("plugin.database.sql.hostname");
        String port = config.getString("plugin.database.sql.port");
        String dbname = config.getString("plugin.database.sql.dbname");
        String tableprefix = config.getString("plugin.database.sql.table_prefix");
        String username = config.getString("plugin.database.sql.username");
        String password = config.getString("plugin.database.sql.password");
        String flags = config.getString("plugin.database.sql.flags");
        String max_pool = config.getString("plugin.database.sql.pooling.max_pool_size");
        String max_lifetime = config.getString("plugin.database.sql.pooling.max_lifetime");
        String connection_timeout = config.getString("plugin.database.sql.pooling.connection_timeout");
        /*
			 * Create database.yml if it doesn't exist yet, with new settings.
			 */
        Path databaseYMLPath = getDataFolder().toPath().resolve("settings").resolve("database.yml");
        if (FileMgmt.checkOrCreateFile(databaseYMLPath.toString())) {
            CommentedConfiguration databaseConfig = new CommentedConfiguration(databaseYMLPath);
            databaseConfig.set("database.database_load", dbload);
            databaseConfig.set("database.database_save", dbsave);
            databaseConfig.set("database.sql.hostname", hostname);
            databaseConfig.set("database.sql.port", port);
            databaseConfig.set("database.sql.dbname", dbname);
            databaseConfig.set("database.sql.table_prefix", tableprefix);
            databaseConfig.set("database.sql.username", username);
            databaseConfig.set("database.sql.password", password);
            databaseConfig.set("database.sql.flags", flags);
            databaseConfig.set("database.sql.pooling.max_pool_size", max_pool);
            databaseConfig.set("database.sql.pooling.max_lifetime", max_lifetime);
            databaseConfig.set("database.sql.pooling.connection_timeout", connection_timeout);
            databaseConfig.save();
            getLogger().info("Database settings migrated to towny\\data\\settings\\database.yml");
        } else {
            getLogger().severe("Unable to migrate old database settings to towny\\data\\settings\\database.yml");
            return false;
        }
    }
    return true;
}
Also used : Path(java.nio.file.Path) CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration)

Example 8 with CommentedConfiguration

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

the class Towny method handleLegacyConfigs.

/**
 * Handle any legacy config settings before we load the config and database.
 */
private void handleLegacyConfigs() {
    Path configPath = Towny.getPlugin().getDataFolder().toPath().resolve("settings").resolve("config.yml");
    if (!Files.exists(configPath))
        return;
    CommentedConfiguration config = new CommentedConfiguration(configPath);
    if (!config.load() || config.getString(ConfigNodes.LAST_RUN_VERSION.getRoot(), "0.0.0.0").equals(getVersion()))
        return;
    // Old configs stored various TownBlock settings throughout the config.
    // This will migrate the old settings into the TownBlockType config section.
    // Since 0.97.5.4.
    TownBlockTypeHandler.Migrator.checkForLegacyOptions(config);
    ConfigMigrator earlyMigrator = new ConfigMigrator(config, "config-migration.json", true);
    earlyMigrator.migrate();
}
Also used : Path(java.nio.file.Path) ConfigMigrator(com.palmergames.bukkit.config.migration.ConfigMigrator) CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration)

Example 9 with CommentedConfiguration

use of com.palmergames.bukkit.config.CommentedConfiguration in project Towny by ElgarL.

the class TownyPerms method loadPerms.

/**
 * Load the townyperms.yml file.
 * If it doesn't exist create it from the resource file in the jar.
 *
 * @param filepath
 * @param defaultRes
 * @throws IOException
 */
public static void loadPerms(String filepath, String defaultRes) throws IOException {
    String fullPath = filepath + FileMgmt.fileSeparator() + defaultRes;
    File file = FileMgmt.unpackResourceFile(fullPath, defaultRes, defaultRes);
    if (file != null) {
        // read the (language).yml into memory
        perms = new CommentedConfiguration(file);
        perms.load();
    }
    /*
		 * Only do this once as we are really only interested in Towny perms.
		 */
    collectPermissions();
}
Also used : CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration) File(java.io.File)

Example 10 with CommentedConfiguration

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

the class TownyPerms method loadPerms.

/**
 * Load the townyperms.yml file.
 * If it doesn't exist create it from the resource file in the jar.
 *
 * @param permsYMLPath - Path to townyperms.yml in the data directory.
 * @throws TownyInitException - Thrown when if we fail to copy or load townyperms.yml.
 */
public static void loadPerms(@NotNull Path permsYMLPath) {
    try {
        InputStream resource = Towny.class.getResourceAsStream("/townyperms.yml");
        if (resource == null) {
            throw new TownyInitException("Could not find 'townyperms.yml' in the JAR.", TownyInitException.TownyError.PERMISSIONS);
        }
        Files.copy(resource, permsYMLPath);
    } catch (FileAlreadyExistsException ignored) {
    // Expected behaviour
    } catch (IOException e) {
        throw new TownyInitException("Could not copy townyperms.yml from JAR to '" + permsYMLPath + "'.", TownyInitException.TownyError.PERMISSIONS, e);
    }
    // read the townyperms.yml into memory
    perms = new CommentedConfiguration(permsYMLPath);
    if (!perms.load()) {
        throw new TownyInitException("Could not read townyperms.yml", TownyInitException.TownyError.PERMISSIONS);
    }
    groupPermsMap.clear();
    buildGroupPermsMap();
    checkForVitalGroups();
    buildComments();
    perms.save();
    /*
		 * Only do this once as we are really only interested in Towny perms.
		 */
    collectPermissions();
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InputStream(java.io.InputStream) CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration) IOException(java.io.IOException) TownyInitException(com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException)

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