LuckPerms

LuckPerms

41.4k Downloads

[API] Best way to get all inherited parent groups

Sir-Will opened this issue ยท 2 comments

commented

hey,

what's the best way to get all inherited parent groups from a user performance wise?

I'm trying to check if a user has the group xyz in any way (temporary, parent of inherited group...).

I tried user.data().contains(node, NodeEqualityPredicate.IGNORE_EXPIRY_TIME) and collecting them all

Collection<String> groups = user.getNodes().stream()
                        .filter(NodeType.INHERITANCE::matches)
                        .map(NodeType.INHERITANCE::cast)
                        .map(InheritanceNode::getGroupName)
                        .collect(Collectors.toSet());

but I only get the groups which are directly assigned to the user.

I could loop over each group the user is assigned and then loop over that groups parent groups again but this is most likely not the best way to do it.

commented

Oh, I found what I was looking for. Using user.resolveInheritedNodes() instead of user.getNodes() works.

commented

Best way performance wise, assuming you just want to check one group, is to simply run a permission check for group.groupname, e.g. group.xyz, in the usual way.

e.g.

if (player.hasPermission("group.xyz") { ... }