Change behaviour of resolveInheritanceTree when 'resolve inheritance' flag is false

This commit is contained in:
Luck 2020-06-12 20:34:57 +01:00
parent 0bef0902c0
commit 2bb1aa7511
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 17 additions and 19 deletions

View File

@ -33,6 +33,7 @@ import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User; import net.luckperms.api.model.user.User;
import net.luckperms.api.node.Node; import net.luckperms.api.node.Node;
import net.luckperms.api.node.NodeType; import net.luckperms.api.node.NodeType;
import net.luckperms.api.query.Flag;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.query.QueryOptions;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -277,10 +278,14 @@ public interface PermissionHolder {
@NonNull SortedSet<Node> resolveDistinctInheritedNodes(@NonNull QueryOptions queryOptions); @NonNull SortedSet<Node> resolveDistinctInheritedNodes(@NonNull QueryOptions queryOptions);
/** /**
* Gets a collection of the {@link Group}s this holder inherits nodes from, both directly * Gets a collection of the {@link Group}s this holder inherits nodes from.
* and indirectly (through directly inherited groups).
* *
* <p>It effectively resolves the whole "inheritance tree".</p> * <p>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".</p>
*
* <p>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.</p>
* *
* <p>The collection will be ordered according to the platforms inheritance rules. The groups * <p>The collection will be ordered according to the platforms inheritance rules. The groups
* which are inherited from first will appear earlier in the list.</p> * which are inherited from first will appear earlier in the list.</p>

View File

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