Search in sources :

Example 1 with CommonPlugin

use of com.bergerkiller.bukkit.common.internal.CommonPlugin in project BKCommonLib by bergerhealer.

the class PluginBase method onEnable.

@Override
@SuppressWarnings("unchecked")
public final void onEnable() {
    // Shortcut to avoid unneeded initialization: calling enable will result in BKCommonLib disabling
    if (!Common.IS_COMPATIBLE && this instanceof CommonPlugin) {
        this.enable();
        return;
    }
    // First of all, check that all dependencies are properly enabled
    for (String dep : LogicUtil.fixNull(getDescription().getDepend(), (List<String>) Collections.EMPTY_LIST)) {
        if (!Bukkit.getPluginManager().isPluginEnabled(dep)) {
            log(Level.SEVERE, "Could not enable '" + getName() + " v" + getVersion() + "' because dependency '" + dep + "' failed to enable!");
            log(Level.SEVERE, "Perhaps the dependency has to be updated? Please check the log for any errors related to " + dep);
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
    }
    long startTime = System.currentTimeMillis();
    if (this.getMinimumLibVersion() > Common.VERSION) {
        log(Level.SEVERE, "Requires a newer BKCommonLib version, please update BKCommonLib to the latest version!");
        log(Level.SEVERE, "Verify that there is only one BKCommonLib.jar in the plugins folder before retrying");
        Bukkit.getPluginManager().disablePlugin(this);
        return;
    }
    this.setDisableMessage(this.getName() + " disabled!");
    // Load permission configuration
    this.permissionconfig = new FileConfiguration(this, "PermissionDefaults.yml");
    // load
    if (this.permissionconfig.exists()) {
        this.loadPermissions();
    }
    // header
    this.permissionconfig.setHeader("Below are the default permissions set for plugin '" + this.getName() + "'.");
    this.permissionconfig.addHeader("These permissions are ignored if the permission is set for a group or player.");
    this.permissionconfig.addHeader("Use the defaults as a base to keep the permissions file small");
    this.permissionconfig.addHeader("Need help with this file? Please visit:");
    this.permissionconfig.addHeader("http://dev.bukkit.org/server-mods/bkcommonlib/pages/general/permission-defaults/");
    // Load localization configuration
    this.localizationconfig = new FileConfiguration(this, "Localization.yml");
    // load
    if (this.localizationconfig.exists()) {
        this.loadLocalization();
    }
    // header
    this.localizationconfig.setHeader("Below are the localization nodes set for plugin '" + this.getName() + "'.");
    this.localizationconfig.addHeader("For colors, use the & character followed up by 0 - F");
    this.localizationconfig.addHeader("Need help with this file? Please visit:");
    this.localizationconfig.addHeader("http://dev.bukkit.org/server-mods/bkcommonlib/pages/general/localization/");
    // Load plugin.yml configuration
    try {
        this.pluginYaml.loadFromStream(getResource("plugin.yml"));
    } catch (Exception ex) {
        Common.LOGGER.log(Level.SEVERE, "[Configuration] An error occured while loading plugin.yml resource for plugin " + getName() + ":");
    }
    // Load all the commands for this Plugin
    Map<String, Map<String, Object>> commands = this.getDescription().getCommands();
    if (commands != null && BPluginDescriptionFile.commands.isValid()) {
        // Prepare commands localization node
        ConfigurationNode commandsNode = getLocalizationNode("commands");
        // Create a new modifiable commands map to replace with
        commands = new HashMap<String, Map<String, Object>>(commands);
        for (Entry<String, Map<String, Object>> commandEntry : commands.entrySet()) {
            ConfigurationNode node = commandsNode.getNode(commandEntry.getKey());
            // Transfer description and usage
            Map<String, Object> data = new HashMap<String, Object>(commandEntry.getValue());
            node.shareWith(data, "description", "No description specified");
            node.shareWith(data, "usage", "/" + commandEntry.getKey());
            commandEntry.setValue(Collections.unmodifiableMap(data));
        }
        // Set the new commands map using reflection
        BPluginDescriptionFile.commands.set(this.getDescription(), Collections.unmodifiableMap(commands));
    }
    // ==== Permissions ====
    this.permissions();
    // Load all nodes from the permissions config
    setPermissions(this.permissionconfig);
    if (!this.permissionconfig.isEmpty()) {
        this.savePermissions();
    }
    // ==== Localization ====
    this.localization();
    if (!this.localizationconfig.isEmpty()) {
        this.saveLocalization();
    }
    // ==== Enabling ====
    try {
        // Metrics
        if (this.pluginYaml.get("metrics", false)) {
            // Send anonymous statistics to mcstats.org
            try {
                this.metrics = new Metrics(this);
            } catch (IOException ex) {
                log(Level.SEVERE, "Failed to initialize metrics for " + getName());
                CommonUtil.printFilteredStackTrace(ex);
            }
        }
        this.wasDisableRequested = false;
        this.enable();
        if (this.wasDisableRequested) {
            // Plugin was disabled again while enabling
            return;
        }
        // Start Metrics if enabled
        if (metrics != null) {
            metrics.start();
        }
        // Done, this plugin is enabled
        this.enabled = true;
    } catch (Throwable t) {
        log(Level.SEVERE, "An error occurred while enabling, the plugin will be disabled:");
        handle(t);
        Bukkit.getPluginManager().disablePlugin(this);
        return;
    }
    // update dependencies
    CommonPlugin.getInstance().plugins.add(this);
    for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
        if (plugin.isEnabled()) {
            this.updateDependency(plugin, plugin.getName(), true);
        }
    }
    // Enable messages
    if (this.enableMessage != null) {
        log(Level.INFO, this.enableMessage);
    }
    log(Level.INFO, this.getName() + " version " + this.getVersion() + " enabled! (" + MathUtil.round(0.001 * (System.currentTimeMillis() - startTime), 3) + "s)");
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) NoPermissionException(com.bergerkiller.bukkit.common.permissions.NoPermissionException) FileConfiguration(com.bergerkiller.bukkit.common.config.FileConfiguration) CommonPlugin(com.bergerkiller.bukkit.common.internal.CommonPlugin) Metrics(com.bergerkiller.bukkit.common.metrics.Metrics) ConfigurationNode(com.bergerkiller.bukkit.common.config.ConfigurationNode) Plugin(org.bukkit.plugin.Plugin) CommonPlugin(com.bergerkiller.bukkit.common.internal.CommonPlugin) JavaPlugin(org.bukkit.plugin.java.JavaPlugin)

Aggregations

ConfigurationNode (com.bergerkiller.bukkit.common.config.ConfigurationNode)1 FileConfiguration (com.bergerkiller.bukkit.common.config.FileConfiguration)1 CommonPlugin (com.bergerkiller.bukkit.common.internal.CommonPlugin)1 Metrics (com.bergerkiller.bukkit.common.metrics.Metrics)1 NoPermissionException (com.bergerkiller.bukkit.common.permissions.NoPermissionException)1 IOException (java.io.IOException)1 Plugin (org.bukkit.plugin.Plugin)1 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)1