Search in sources :

Example 1 with ActionRegistry

use of me.botsko.prism.actionlibs.ActionRegistry in project Prism-Bukkit by prism.

the class Prism method onEnable.

/**
 * Enables the plugin and activates our player listeners.
 */
@Override
public void onEnable() {
    debug = getConfig().getBoolean("prism.debug", false);
    prismLog = createPrismLogger();
    pluginName = this.getDescription().getName();
    pluginVersion = this.getDescription().getVersion();
    audiences = BukkitAudiences.create(this);
    messenger = new Messenger(pluginName, Prism.getAudiences());
    log("Initializing Prism " + pluginVersion + ". Originally by Viveleroi; maintained by the AddstarMC Network");
    // Load configuration, or install if new
    loadConfig();
    isPaper = PaperLib.isPaper();
    if (isPaper) {
        Prism.log("Optional Paper Events will be enabled.");
    } else {
        if (!getConfig().getBoolean("prism.suppress-paper-message", false)) {
            Prism.log("Paper not detected - Optional Paper Events will NOT be enabled.");
        }
    }
    checkPluginDependencies();
    if (getConfig().getBoolean("prism.paste.enable")) {
        pasteKey = Prism.config.getString("prism.paste.api-key", "API KEY");
        if (pasteKey != null && (pasteKey.startsWith("API key") || pasteKey.length() < 6)) {
            pasteKey = null;
        } else {
            Prism.log("PasteApi is configured and available");
        }
    } else {
        pasteKey = null;
    }
    final List<String> worldNames = getServer().getWorlds().stream().map(World::getName).collect(Collectors.toList());
    final String[] playerNames = Bukkit.getServer().getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new);
    // init db async then call back to complete enable.
    final BukkitTask updating = Bukkit.getScheduler().runTaskTimerAsynchronously(instance, () -> {
        if (!isEnabled()) {
            warn("Prism is loading and updating the database; logging is NOT enabled");
        }
    }, 100, 200);
    Bukkit.getScheduler().runTaskAsynchronously(instance, () -> {
        prismDataSource = PrismDatabaseFactory.createDataSource(config);
        Connection testConnection;
        if (prismDataSource != null) {
            testConnection = prismDataSource.getConnection();
            if (testConnection == null) {
                notifyDisabled();
                Bukkit.getScheduler().runTask(instance, () -> instance.enableFailedDatabase());
                updating.cancel();
                return;
            }
            try {
                testConnection.close();
            } catch (final SQLException e) {
                prismDataSource.handleDataSourceException(e);
            }
        } else {
            notifyDisabled();
            Bukkit.getScheduler().runTask(instance, () -> instance.enableFailedDatabase());
            updating.cancel();
            return;
        }
        // Info needed for setup, init these here
        handlerRegistry = new HandlerRegistry();
        actionRegistry = new ActionRegistry();
        // Setup databases
        prismDataSource.setupDatabase(actionRegistry);
        // Cache world IDs
        prismDataSource.cacheWorldPrimaryKeys(prismWorlds);
        SqlPlayerIdentificationHelper.cacheOnlinePlayerPrimaryKeys(playerNames);
        // ensure current worlds are added
        for (final String w : worldNames) {
            if (!Prism.prismWorlds.containsKey(w)) {
                prismDataSource.addWorldName(w);
            }
        }
        // Apply any updates
        final DatabaseUpdater up = new DatabaseUpdater(this);
        up.applyUpdates();
        Bukkit.getScheduler().runTask(instance, () -> instance.enabled());
        updating.cancel();
    });
}
Also used : HandlerRegistry(me.botsko.prism.actionlibs.HandlerRegistry) SQLException(java.sql.SQLException) BukkitTask(org.bukkit.scheduler.BukkitTask) Connection(java.sql.Connection) ActionRegistry(me.botsko.prism.actionlibs.ActionRegistry)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ActionRegistry (me.botsko.prism.actionlibs.ActionRegistry)1 HandlerRegistry (me.botsko.prism.actionlibs.HandlerRegistry)1 BukkitTask (org.bukkit.scheduler.BukkitTask)1