use of com.bergerkiller.bukkit.common.metrics.Metrics in project BKCommonLib by bergerhealer.
the class PluginBase method onEnable.
@Override
public final void onEnable() {
// Do this early so command perms work properly all the time
if (Bukkit.getPluginManager().getPermission(CommonDependencyStartupLogHandler.PERMISSION) == null) {
Permission permission = new Permission(CommonDependencyStartupLogHandler.PERMISSION, "Use the startuplog subcommand to view the startup log of plugins", PermissionDefault.OP);
Bukkit.getPluginManager().addPermission(permission);
}
// Check compatible with server
boolean compatible = false;
try {
compatible = Common.IS_COMPATIBLE;
} catch (Throwable t) {
getLogger().log(Level.SEVERE, "An unexpected BKCommonLib initialization error occurred", t);
if (this instanceof CommonPlugin) {
onCriticalStartupFailure("Critical initialization error (unsupported server?)");
return;
}
}
// The enable() will, after logging details, call onCriticalStartupFailure()
if (!compatible && this instanceof CommonPlugin) {
try {
this.enable();
} catch (Throwable t) {
getLogger().log(Level.SEVERE, "An unexpected BKCommonLib initialization error occurred", t);
if (!startupLogHandler.hasCriticalStartupFailure()) {
onCriticalStartupFailure("Critical initialization error (unsupported server?)");
}
}
return;
}
// If BKCommonLib is not compatible, don't bother enabling a dependency of it
if (!compatible) {
onCriticalStartupFailure("Installed BKCommonLib is not compatible with this server", Bukkit.getPluginManager().getPlugin("BKCommonLib"));
return;
}
if (!(this instanceof CommonPlugin) && !CommonPlugin.hasInstance()) {
onCriticalStartupFailure("BKCommonLib failed to enable, this plugin is disabled", Bukkit.getPluginManager().getPlugin("BKCommonLib"));
return;
}
// First of all, check that all dependencies are properly enabled
// Install a logger hook in all dependencies
{
// Load a full list of hard dependencies. These MUST be enabled to continue.
List<String> dependencies = LogicUtil.fixNull(getDescription().getDepend(), Collections.emptyList());
// Include all dependency's startup logs for the report of this plugin's logs
dependencies.stream().map(Bukkit.getPluginManager()::getPlugin).filter(Objects::nonNull).forEach(this.startupLogHandler::bindDependency);
// Fail enabling this plugin if a required dependency is not enabled
for (String dep : dependencies) {
if (!isPluginEnabledCheckPreloader(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);
onCriticalStartupFailure("Plugin dependency '" + dep + "' failed to enable", Bukkit.getPluginManager().getPlugin(dep));
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");
onCriticalStartupFailure("Plugin requires a newer version of BKCommonLib");
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("https://wiki.traincarts.net/p/BKCommonLib/PermissionDefaults");
// 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("https://wiki.traincarts.net/p/BKCommonLib/Localization");
// 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.shareWithMap(data, "description", "No description specified");
node.shareWithMap(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.getBoolean("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.hasCriticalStartupFailure() || this.wasDisableRequested) {
// Plugin was disabled again while enabling
return;
}
// Disable startup logging next tick. This makes sure that stuff logged by this plugin
// while other plugins (depending on it) enable is still included in the history.
startupLogHandler.setNotStartupNextTick();
// Start Metrics if enabled
if (metrics != null) {
metrics.start();
}
// Done, this plugin is enabled
this.enabled = true;
} catch (Throwable t) {
getLogger().log(Level.SEVERE, "An error occurred while enabling, the plugin will be disabled:", t);
onCriticalStartupFailure("An error occurred while enabling");
return;
}
// update dependencies
CommonPlugin.getInstance().plugins.add(this);
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.isEnabled()) {
this.updateDependency(plugin, plugin.getName(), true);
}
}
// Save
CommonPlugin.flushSaveOperations(this);
// Enable messages
if (this.enableMessage != null) {
log(Level.INFO, this.enableMessage);
}
log(Level.INFO, this.getName() + " version " + this.getDebugVersion() + " enabled! (" + MathUtil.round(0.001 * (System.currentTimeMillis() - startTime), 3) + "s)");
}
Aggregations