Search in sources :

Example 1 with MemorySection

use of org.bukkit.configuration.MemorySection in project TokenManager by RealizedMC.

the class AbstractConfiguration method convert.

protected FileConfiguration convert(final Converter converter) throws IOException {
    plugin.getLogger().info(String.format(CONVERT_START, name));
    final Map<String, Object> oldValues = new HashMap<>();
    for (final String key : configuration.getKeys(true)) {
        if (key.equals("config-version")) {
            continue;
        }
        final Object value = configuration.get(key);
        if (value instanceof MemorySection) {
            continue;
        }
        oldValues.put(key, value);
    }
    if (converter != null) {
        converter.renamedKeys().forEach((old, changed) -> {
            final Object previous = oldValues.get(old);
            if (previous != null) {
                oldValues.remove(old);
                oldValues.put(changed, previous);
            }
        });
    }
    final String newName = name.replace(".yml", "") + "-" + System.currentTimeMillis() + ".yml";
    final File copied = Files.copy(file.toPath(), new File(plugin.getDataFolder(), newName).toPath()).toFile();
    plugin.getLogger().info(String.format(CONVERT_SAVE, copied.getName()));
    plugin.saveResource(name, true);
    // Loads comments of the new configuration file
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        final Multimap<String, List<String>> comments = LinkedListMultimap.create();
        final List<String> currentComments = new ArrayList<>();
        String line;
        Matcher matcher;
        while ((line = reader.readLine()) != null) {
            if ((matcher = KEY_PATTERN.matcher(line)).find() && !COMMENT_PATTERN.matcher(line).matches()) {
                comments.put(matcher.group(2), Lists.newArrayList(currentComments));
                currentComments.clear();
            } else if (COMMENT_PATTERN.matcher(line).matches()) {
                currentComments.add(line);
            }
        }
        configuration = YamlConfiguration.loadConfiguration(file);
        configuration.options().header(null);
        // Transfer values from the old configuration
        for (Map.Entry<String, Object> entry : oldValues.entrySet()) {
            final Object previous;
            if ((previous = configuration.get(entry.getKey())) != null && entry.getValue().getClass().isInstance(previous)) {
                configuration.set(entry.getKey(), entry.getValue());
            }
        }
        final List<String> commentlessData = Lists.newArrayList(configuration.saveToString().split("\n"));
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (final String data : commentlessData) {
                matcher = KEY_PATTERN.matcher(data);
                if (matcher.find()) {
                    final String key = matcher.group(2);
                    final Collection<List<String>> result = comments.get(key);
                    if (result != null) {
                        final List<List<String>> commentData = Lists.newArrayList(result);
                        if (!commentData.isEmpty()) {
                            for (final String comment : commentData.get(0)) {
                                writer.write(comment);
                                writer.newLine();
                            }
                            commentData.remove(0);
                            comments.replaceValues(key, commentData);
                        }
                    }
                }
                writer.write(data);
                if (commentlessData.indexOf(data) + 1 < commentlessData.size()) {
                    writer.newLine();
                } else if (!currentComments.isEmpty()) {
                    writer.newLine();
                }
            }
            // Handles comments at the end of the file without any key
            for (final String comment : currentComments) {
                writer.write(comment);
                if (currentComments.indexOf(comment) + 1 < currentComments.size()) {
                    writer.newLine();
                }
            }
            writer.flush();
        }
        plugin.getLogger().info(CONVERT_DONE);
    }
    return configuration;
}
Also used : MemorySection(org.bukkit.configuration.MemorySection) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MemorySection

use of org.bukkit.configuration.MemorySection in project AuthMeReloaded by AuthMe.

the class PermissionConsistencyTest method getPermissionsFromPluginYmlFile.

/**
 * Returns all permission entries from the plugin.yml file.
 *
 * @return map with the permission entries by permission node
 */
private static Map<String, PermissionDefinition> getPermissionsFromPluginYmlFile() {
    FileConfiguration pluginFile = YamlConfiguration.loadConfiguration(getJarFile("/plugin.yml"));
    MemorySection permsList = (MemorySection) pluginFile.get("permissions");
    Map<String, PermissionDefinition> permissions = new HashMap<>();
    addChildren(permsList, permissions);
    return ImmutableMap.copyOf(permissions);
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) MemorySection(org.bukkit.configuration.MemorySection) HashMap(java.util.HashMap)

Example 3 with MemorySection

use of org.bukkit.configuration.MemorySection in project AuthMeReloaded by AuthMe.

the class CommandConsistencyTest method getLabelsFromPluginFile.

/**
 * Reads plugin.yml and returns the defined commands by main label and aliases.
 *
 * @return collection of all labels and their aliases
 */
@SuppressWarnings("unchecked")
private static Map<String, List<String>> getLabelsFromPluginFile() {
    FileConfiguration pluginFile = YamlConfiguration.loadConfiguration(getJarFile("/plugin.yml"));
    MemorySection commandList = (MemorySection) pluginFile.get("commands");
    Map<String, Object> commandDefinitions = commandList.getValues(false);
    Map<String, List<String>> commandLabels = new HashMap<>();
    for (Map.Entry<String, Object> commandDefinition : commandDefinitions.entrySet()) {
        MemorySection definition = (MemorySection) commandDefinition.getValue();
        List<String> alternativeLabels = definition.get("aliases") == null ? Collections.EMPTY_LIST : (List<String>) definition.get("aliases");
        commandLabels.put(commandDefinition.getKey(), alternativeLabels);
    }
    return commandLabels;
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) MemorySection(org.bukkit.configuration.MemorySection) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MemorySection

use of org.bukkit.configuration.MemorySection in project AuthMeReloaded by AuthMe.

the class HelpTranslationGeneratorIntegrationTest method checkDescription.

private static void checkDescription(Object memorySection, String description, String detailedDescription) {
    if (memorySection instanceof MemorySection) {
        MemorySection memSection = (MemorySection) memorySection;
        assertThat(memSection.getString("description"), equalTo(description));
        assertThat(memSection.getString("detailedDescription"), equalTo(detailedDescription));
    } else {
        fail("Expected MemorySection, got '" + memorySection + "'");
    }
}
Also used : MemorySection(org.bukkit.configuration.MemorySection)

Example 5 with MemorySection

use of org.bukkit.configuration.MemorySection in project Glowstone by GlowstoneMC.

the class GlowServer method enablePlugins.

/**
 * Enable all plugins of the given load order type.
 *
 * @param type The type of plugin to enable.
 */
private void enablePlugins(PluginLoadOrder type) {
    if (type == PluginLoadOrder.STARTUP) {
        helpMap.clear();
        helpMap.loadConfig(config.getConfigFile(Key.HELP_FILE));
    }
    // load all the plugins
    Plugin[] plugins = pluginManager.getPlugins();
    for (Plugin plugin : plugins) {
        if (!plugin.isEnabled() && plugin.getDescription().getLoad() == type) {
            List<Permission> perms = plugin.getDescription().getPermissions();
            for (Permission perm : perms) {
                try {
                    pluginManager.addPermission(perm);
                } catch (IllegalArgumentException ex) {
                    ConsoleMessages.Warn.Plugin.PERMISSION_DUPLICATE.log(ex, plugin.getDescription().getFullName(), perm.getName());
                }
            }
            try {
                pluginManager.enablePlugin(plugin);
            } catch (Throwable ex) {
                ConsoleMessages.Error.Plugin.LOADING.log(ex, plugin.getDescription().getFullName());
            }
        }
    }
    if (type == PluginLoadOrder.POSTWORLD) {
        commandMap.setFallbackCommands();
        commandMap.registerServerAliases();
        DefaultPermissions.registerCorePermissions();
        // Default permissions
        this.permissionRoot = DefaultPermissions.registerPermission("minecraft", "Gives the user the ability to use all " + "Minecraft utilities and commands");
        this.permissionRootCommand = DefaultPermissions.registerPermission("minecraft.command", "Gives the user the ability to use " + "all Minecraft commands", permissionRoot);
        DefaultPermissions.registerPermission("minecraft.command.tell", "Allows the user to send a " + "private message", PermissionDefault.TRUE, permissionRootCommand);
        permissionRootCommand.recalculatePermissibles();
        permissionRoot.recalculatePermissibles();
        helpMap.initializeCommands();
        helpMap.amendTopics(config.getConfigFile(Key.HELP_FILE));
        // load permissions.yml
        ConfigurationSection permConfig = config.getConfigFile(Key.PERMISSIONS_FILE);
        Map<String, Map<String, Object>> data = new HashMap<>();
        permConfig.getValues(false).forEach((key, value) -> data.put(key, ((MemorySection) value).getValues(false)));
        List<Permission> perms = Permission.loadPermissions(data, ConsoleMessages.Error.Permission.INVALID.get(), PermissionDefault.OP);
        for (Permission perm : perms) {
            try {
                pluginManager.addPermission(perm);
            } catch (IllegalArgumentException ex) {
                ConsoleMessages.Warn.Permission.DUPLICATE.log(ex, perm.getName());
            }
        }
    }
}
Also used : MemorySection(org.bukkit.configuration.MemorySection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Permission(org.bukkit.permissions.Permission) Map(java.util.Map) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) GlowHelpMap(net.glowstone.util.GlowHelpMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) EnumMap(java.util.EnumMap) HelpMap(org.bukkit.help.HelpMap) HashMap(java.util.HashMap) Plugin(org.bukkit.plugin.Plugin) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

MemorySection (org.bukkit.configuration.MemorySection)9 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)5 HashMap (java.util.HashMap)4 File (java.io.File)3 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Sets (com.google.common.collect.Sets)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 CommandDescription (fr.xephi.authme.command.CommandDescription)1 CommandInitializer (fr.xephi.authme.command.CommandInitializer)1 CommandUtils (fr.xephi.authme.command.CommandUtils)1 HelpMessage (fr.xephi.authme.command.help.HelpMessage)1 HelpSection (fr.xephi.authme.command.help.HelpSection)1 ApiManager (github.scarsz.discordsrv.api.ApiManager)1 DiscordGuildMessagePostBroadcastEvent (github.scarsz.discordsrv.api.events.DiscordGuildMessagePostBroadcastEvent)1 GameChatMessagePostProcessEvent (github.scarsz.discordsrv.api.events.GameChatMessagePostProcessEvent)1