Java Code Examples for org.bukkit.configuration.MemorySection#getKeys()

The following examples show how to use org.bukkit.configuration.MemorySection#getKeys() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PermissionConsistencyTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Recursively visits every MemorySection and creates a {@link PermissionDefinition} when applicable.
 *
 * @param node the node to visit
 * @param collection the collection to add constructed permission definitions to
 */
private static void addChildren(MemorySection node, Map<String, PermissionDefinition> collection) {
    // A MemorySection may have a permission entry, as well as MemorySection children
    boolean hasPermissionEntry = false;
    for (String key : node.getKeys(false)) {
        if (node.get(key) instanceof MemorySection && !"children".equals(key)) {
            addChildren((MemorySection) node.get(key), collection);
        } else if (PERMISSION_FIELDS.contains(key)) {
            hasPermissionEntry = true;
        } else {
            throw new IllegalStateException("Unexpected key '" + key + "'");
        }
    }
    if (hasPermissionEntry) {
        PermissionDefinition permDef = new PermissionDefinition(node);
        collection.put(permDef.node, permDef);
    }
}
 
Example 2
Source File: HelpTranslationVerifier.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the leaf keys of the section at the given path of the file configuration.
 *
 * @param path the path whose leaf keys should be retrieved
 * @return leaf keys of the memory section,
 *         empty set if the configuration does not have a memory section at the given path
 */
private Set<String> getLeafKeys(String path) {
    if (!(configuration.get(path) instanceof MemorySection)) {
        return Collections.emptySet();
    }
    MemorySection memorySection = (MemorySection) configuration.get(path);

    // MemorySection#getKeys(true) returns all keys on all levels, e.g. if the configuration has
    // 'commands.authme.register' then it also has 'commands.authme' and 'commands'. We can traverse each node and
    // build its parents (e.g. for commands.authme.register.description: commands.authme.register, commands.authme,
    // and commands, which we can remove from the collection since we know they are not a leaf.
    Set<String> leafKeys = memorySection.getKeys(true);
    Set<String> allKeys = new HashSet<>(leafKeys);

    for (String key : allKeys) {
        List<String> pathParts = Arrays.asList(key.split("\\."));

        // We perform construction of parents & their removal in reverse order so we can build the lowest-level
        // parent of a node first. As soon as the parent doesn't exist in the set already, we know we can continue
        // with the next node since another node has already removed the concerned parents.
        for (int i = pathParts.size() - 1; i > 0; --i) {
            // e.g. for commands.authme.register -> i = {2, 1} => {commands.authme, commands}
            String parentPath = String.join(".", pathParts.subList(0, i));
            if (!leafKeys.remove(parentPath)) {
                break;
            }
        }
    }
    return leafKeys.stream().map(leaf -> path + "." + leaf).collect(Collectors.toSet());
}