Search in sources :

Example 1 with ConfigurationNode

use of com.bergerkiller.bukkit.common.config.ConfigurationNode in project BKCommonLib by bergerhealer.

the class ItemMaterialTest method generateVariantsConfig.

// Only works on MC 1.12.1 - generate an item variants yaml configuration
// This configuration is used on MC < 1.12.1
@Ignore
@Test
public void generateVariantsConfig() {
    FileConfiguration config = new FileConfiguration("test.yml");
    for (Material m : getAllMaterials()) {
        List<ItemStack> items = ItemUtil.getItemVariants(m);
        if (items.size() == 0) {
            // Material has no variants at all
            continue;
        }
        if (items.size() != 1 || !items.get(0).equals(new ItemStack(m))) {
            int dur_start = items.get(0).getDurability();
            int dur_end = items.get(items.size() - 1).getDurability();
            boolean validRange = true;
            Iterator<ItemStack> it = items.iterator();
            for (int dur = dur_start; dur <= dur_end; dur++) {
                ItemStack t = new ItemStack(m, 1, (short) dur);
                if (!it.hasNext() || !it.next().equals(t)) {
                    validRange = false;
                    break;
                }
            }
            if (validRange) {
                System.out.println("registerRange(\"" + m.name() + "\", " + dur_start + ", " + dur_end + ");");
            } else {
                // 'Weird'
                ConfigurationNode node = config.getNode(m.toString());
                for (int i = 0; i < items.size(); i++) {
                    node.set("item" + i, items.get(i));
                }
            // System.out.println(node.toString());
            }
        }
    }
    config.save();
}
Also used : FileConfiguration(com.bergerkiller.bukkit.common.config.FileConfiguration) ConfigurationNode(com.bergerkiller.bukkit.common.config.ConfigurationNode) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ConfigurationNode

use of com.bergerkiller.bukkit.common.config.ConfigurationNode 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)");
}
Also used : IOException(java.io.IOException) 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) Permission(org.bukkit.permissions.Permission) Plugin(org.bukkit.plugin.Plugin) CommonPlugin(com.bergerkiller.bukkit.common.internal.CommonPlugin) JavaPlugin(org.bukkit.plugin.java.JavaPlugin)

Example 3 with ConfigurationNode

use of com.bergerkiller.bukkit.common.config.ConfigurationNode in project BKCommonLib by bergerhealer.

the class PluginBase method getCommandDescription.

/**
 * Gets the localized description for a command
 *
 * @param command name (case insensitive)
 * @return command description
 */
public String getCommandDescription(String command) {
    ConfigurationNode node = getCommandNode(command);
    final String defValue = "No description specified";
    if (node == null) {
        return defValue;
    } else {
        return node.get("description", defValue);
    }
}
Also used : ConfigurationNode(com.bergerkiller.bukkit.common.config.ConfigurationNode)

Example 4 with ConfigurationNode

use of com.bergerkiller.bukkit.common.config.ConfigurationNode in project BKCommonLib by bergerhealer.

the class PluginBase method setPermissions.

private static void setPermissions(ConfigurationNode node) {
    for (ConfigurationNode subNode : node.getNodes()) {
        setPermissions(subNode);
    }
    PermissionDefault def = ParseUtil.convert(getNodeStringValue(node, "default"), PermissionDefault.class);
    String desc = getNodeStringValue(node, "description");
    if (def != null || desc != null) {
        Permission permission = getPermission(node.getPath().toLowerCase(Locale.ENGLISH));
        if (def != null) {
            permission.setDefault(def);
        }
        if (desc != null) {
            permission.setDescription(desc);
        }
    }
}
Also used : IPermissionDefault(com.bergerkiller.bukkit.common.permissions.IPermissionDefault) PermissionDefault(org.bukkit.permissions.PermissionDefault) ConfigurationNode(com.bergerkiller.bukkit.common.config.ConfigurationNode) Permission(org.bukkit.permissions.Permission)

Example 5 with ConfigurationNode

use of com.bergerkiller.bukkit.common.config.ConfigurationNode in project BKCommonLib by bergerhealer.

the class PluginBase method getCommandUsage.

/**
 * Gets the localized usage for a command
 *
 * @param command name (case insensitive)
 * @return command usage
 */
public String getCommandUsage(String command) {
    ConfigurationNode node = getCommandNode(command);
    final String defValue = "/" + command;
    if (node == null) {
        return defValue;
    } else {
        return node.get("usage", defValue);
    }
}
Also used : ConfigurationNode(com.bergerkiller.bukkit.common.config.ConfigurationNode)

Aggregations

ConfigurationNode (com.bergerkiller.bukkit.common.config.ConfigurationNode)6 FileConfiguration (com.bergerkiller.bukkit.common.config.FileConfiguration)3 ItemStack (org.bukkit.inventory.ItemStack)2 Permission (org.bukkit.permissions.Permission)2 Test (org.junit.Test)2 CommonPlugin (com.bergerkiller.bukkit.common.internal.CommonPlugin)1 Metrics (com.bergerkiller.bukkit.common.metrics.Metrics)1 IPermissionDefault (com.bergerkiller.bukkit.common.permissions.IPermissionDefault)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 GameMode (org.bukkit.GameMode)1 Material (org.bukkit.Material)1 PermissionDefault (org.bukkit.permissions.PermissionDefault)1 Plugin (org.bukkit.plugin.Plugin)1 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)1 Ignore (org.junit.Ignore)1