diff --git a/api/src/main/java/net/luckperms/api/model/PermissionHolder.java b/api/src/main/java/net/luckperms/api/model/PermissionHolder.java index 076dfe766..34710a140 100644 --- a/api/src/main/java/net/luckperms/api/model/PermissionHolder.java +++ b/api/src/main/java/net/luckperms/api/model/PermissionHolder.java @@ -33,6 +33,7 @@ import net.luckperms.api.model.group.Group; import net.luckperms.api.model.user.User; import net.luckperms.api.node.Node; import net.luckperms.api.node.NodeType; +import net.luckperms.api.query.Flag; import net.luckperms.api.query.QueryOptions; import org.checkerframework.checker.nullness.qual.NonNull; @@ -277,10 +278,14 @@ public interface PermissionHolder { @NonNull SortedSet resolveDistinctInheritedNodes(@NonNull QueryOptions queryOptions); /** - * Gets a collection of the {@link Group}s this holder inherits nodes from, both directly - * and indirectly (through directly inherited groups). + * Gets a collection of the {@link Group}s this holder inherits nodes from. * - *

It effectively resolves the whole "inheritance tree".

+ *

If {@link Flag#RESOLVE_INHERITANCE} is set, this will include holders inherited from both + * directly and indirectly (through directly inherited groups). It will effectively resolve the + * whole "inheritance tree".

+ * + *

If {@link Flag#RESOLVE_INHERITANCE} is not set, then the traversal will only go one + * level up the inheritance tree, and return only directly inherited groups.

* *

The collection will be ordered according to the platforms inheritance rules. The groups * which are inherited from first will appear earlier in the list.

diff --git a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java index 79c807b3f..6b9990578 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java @@ -60,7 +60,6 @@ import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; @@ -351,26 +350,20 @@ public abstract class PermissionHolder { @SuppressWarnings({"unchecked", "rawtypes"}) public List resolveInheritanceTree(QueryOptions queryOptions) { - if (!queryOptions.flag(Flag.RESOLVE_INHERITANCE)) { - return Collections.emptyList(); - } - InheritanceGraph graph = this.plugin.getInheritanceGraphFactory().getGraph(queryOptions); - // perform a full traversal of the inheritance tree - List traversal = new ArrayList<>(); - Iterables.addAll(traversal, graph.traverse(this)); + List inheritanceTree = new ArrayList<>(); - // remove 'this' (the start node) - will usually be at traversal[0], - // but not always due to the possibility of post-traversal sorts! - if (traversal.get(0) == this) { - traversal.remove(0); + if (queryOptions.flag(Flag.RESOLVE_INHERITANCE)) { + Iterables.addAll(inheritanceTree, graph.traverse(this)); + inheritanceTree.remove(this); } else { - traversal.remove(this); + // if RESOLVE_INHERITANCE is not set, only go up by one level + Iterables.addAll(inheritanceTree, graph.successors(this)); } - // ensure our traversal now only consists of groups - for (PermissionHolder permissionHolder : traversal) { + // ensure our tree now only consists of groups + for (PermissionHolder permissionHolder : inheritanceTree) { if (!(permissionHolder instanceof Group)) { throw new IllegalStateException("Non-group object in inheritance tree: " + permissionHolder); } @@ -378,7 +371,7 @@ public abstract class PermissionHolder { // cast List to List // this feels a bit dirty but it works & avoids needless copying! - return (List) traversal; + return (List) inheritanceTree; } public Map exportPermissions(QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) {