From 9c5a43b487dcb2f643a94354a9a7232195ddd581 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 18 Oct 2021 22:10:20 +0100 Subject: [PATCH] Allow meta/prefix/suffix nodes to be negated to cancel inheritance (#3020) --- .../common/cacheddata/type/MetaAccumulator.java | 15 +++++++++++++++ .../luckperms/common/model/PermissionHolder.java | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaAccumulator.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaAccumulator.java index e449ae0e4..fd6dfa8b9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaAccumulator.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/type/MetaAccumulator.java @@ -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 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()); 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 edd34ef30..4583261f0 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 @@ -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); } });