Allow meta/prefix/suffix nodes to be negated to cancel inheritance (#3020)

This commit is contained in:
Luck 2021-10-18 22:10:20 +01:00
parent b2c76aca7d
commit 9c5a43b487
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 16 additions and 1 deletions

View File

@ -40,8 +40,10 @@ import net.luckperms.api.node.types.PrefixNode;
import net.luckperms.api.node.types.SuffixNode;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicReference;
@ -79,6 +81,8 @@ public class MetaAccumulator {
private int weight = 0;
private String primaryGroup;
private Set<String> seenNodeKeys = new HashSet<>();
private final MetaStackDefinition prefixDefinition;
private final MetaStackDefinition suffixDefinition;
private final MetaStackAccumulator prefixAccumulator;
@ -120,6 +124,7 @@ public class MetaAccumulator {
if (this.primaryGroup != null && !this.meta.containsKey("primarygroup")) {
this.meta.put("primarygroup", this.primaryGroup);
}
this.seenNodeKeys = null; // free up for GC
this.state.set(State.COMPLETE);
}
@ -129,6 +134,16 @@ public class MetaAccumulator {
public void accumulateNode(Node n) {
ensureState(State.ACCUMULATING);
// only process distinct nodes once, allows inheritance to be
// "cancelled out" by assigning a false copy.
if (!this.seenNodeKeys.add(n.getKey())) {
return;
}
if (!n.getValue()) {
return;
}
if (n instanceof MetaNode) {
MetaNode mn = (MetaNode) n;
this.meta.put(mn.getMetaKey(), mn.getMetaValue());

View File

@ -394,7 +394,7 @@ public abstract class PermissionHolder {
// accumulate nodes
for (DataType dataType : holder.queryOrder(queryOptions)) {
holder.getData(dataType).forEach(queryOptions, node -> {
if (node.getValue() && NodeType.META_OR_CHAT_META.matches(node)) {
if (NodeType.META_OR_CHAT_META.matches(node)) {
accumulator.accumulateNode(node);
}
});