下面列出了org.bukkit.configuration.MemorySection#getKeys ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
}
/**
* 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());
}