Search in sources :

Example 1 with SQLiteBackend

use of au.com.mineauz.minigames.backend.sqlite.SQLiteBackend in project Minigames by AddstarMC.

the class BackendManager method initialize.

/**
	 * Initializes the backend.
	 * @param config The configuration to load settings from
	 */
public boolean initialize(ConfigurationSection config) {
    ConfigurationSection backendSection = config.getConfigurationSection("backend");
    if (config.isSet("use-sql")) {
        // Load in values from previous system
        if (config.getBoolean("use-sql")) {
            backendSection.set("type", "mysql");
        } else {
            backendSection.set("type", "sqlite");
        }
        backendSection.set("database", config.getString("sql-database"));
        backendSection.set("host", config.getString("sql-host") + ":" + config.getInt("sql-port"));
        backendSection.set("username", config.getString("sql-username"));
        backendSection.set("password", config.getString("sql-password"));
        backendSection.set("convert", true);
        // Clear the existing value
        config.set("use-sql", null);
        config.set("sql-database", null);
        config.set("sql-port", null);
        config.set("sql-host", null);
        config.set("sql-username", null);
        config.set("sql-password", null);
    }
    // Create the backend
    String type = backendSection.getString("type", "sqlite").toLowerCase();
    backend = makeBackend(type);
    if (backend == null) {
        // Default to this
        logger.warning("Invalid backend type " + type + ". Falling back to SQLite");
        backend = new SQLiteBackend(logger);
    }
    // Init
    if (!backend.initialize(backendSection)) {
        return false;
    }
    // Handle conversion
    if (backendSection.getBoolean("convert", false)) {
        ExportNotifier notifier = new ExportNotifier() {

            @Override
            public void onProgress(String state, int count) {
                logger.info("Conversion: " + state + " " + count);
            }

            @Override
            public void onError(Throwable e, String state, int count) {
                logger.log(Level.SEVERE, "Conversion error: " + state + " " + count, e);
            }

            @Override
            public void onComplete() {
                logger.info("Conversion complete");
            }
        };
        if (backend.doConversion(notifier)) {
            backendSection.set("convert", false);
        } else {
            return false;
        }
    }
    // Start the cleaning task to remove old connections
    Bukkit.getScheduler().runTaskTimer(Minigames.plugin, new Runnable() {

        @Override
        public void run() {
            backend.clean();
        }
    }, 300, 300);
    return true;
}
Also used : SQLiteBackend(au.com.mineauz.minigames.backend.sqlite.SQLiteBackend) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

SQLiteBackend (au.com.mineauz.minigames.backend.sqlite.SQLiteBackend)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1