From 97d1deec9c5f1cbf19fc7811d0c3b4567dea35ea Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 11 Dec 2020 10:38:57 +0000 Subject: [PATCH] Slightly optimize weight and display name lookups --- .../commands/generic/meta/MetaInfo.java | 6 +--- .../lucko/luckperms/common/model/Group.java | 9 ++---- .../luckperms/common/model/WeightCache.java | 30 ++++++++----------- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java index 97907d9fd..2712ae22e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java @@ -72,11 +72,7 @@ public class MetaInfo extends GenericChildCommand { Set meta = new LinkedHashSet<>(); // Collect data - for (Node node : target.resolveInheritedNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) { - if (!NodeType.META_OR_CHAT_META.matches(node)) { - continue; - } - + for (Node node : target.resolveInheritedNodes(NodeType.META_OR_CHAT_META, QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) { if (node instanceof PrefixNode) { PrefixNode pn = (PrefixNode) node; prefixes.add(Maps.immutableEntry(pn.getPriority(), pn)); diff --git a/common/src/main/java/me/lucko/luckperms/common/model/Group.java b/common/src/main/java/me/lucko/luckperms/common/model/Group.java index 7f18071b2..af1273472 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/Group.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/Group.java @@ -34,7 +34,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import net.luckperms.api.node.Node; +import net.luckperms.api.node.NodeType; import net.luckperms.api.node.types.DisplayNameNode; import net.luckperms.api.query.QueryOptions; @@ -137,11 +137,8 @@ public class Group extends PermissionHolder { public Optional calculateDisplayName(QueryOptions queryOptions) { // query for a displayname node - for (Node n : getOwnNodes(queryOptions)) { - if (n instanceof DisplayNameNode) { - DisplayNameNode displayNameNode = (DisplayNameNode) n; - return Optional.of(displayNameNode.getDisplayName()); - } + for (DisplayNameNode n : getOwnNodes(NodeType.DISPLAY_NAME, queryOptions)) { + return Optional.of(n.getDisplayName()); } // fallback to config diff --git a/common/src/main/java/me/lucko/luckperms/common/model/WeightCache.java b/common/src/main/java/me/lucko/luckperms/common/model/WeightCache.java index 420904b96..c3231afef 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/WeightCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/WeightCache.java @@ -29,7 +29,7 @@ import me.lucko.luckperms.common.cache.Cache; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.query.QueryOptionsImpl; -import net.luckperms.api.node.Node; +import net.luckperms.api.node.NodeType; import net.luckperms.api.node.types.WeightNode; import org.checkerframework.checker.nullness.qual.NonNull; @@ -50,29 +50,25 @@ public class WeightCache extends Cache { @Override protected @NonNull OptionalInt supply() { boolean seen = false; - int best = 0; - for (Node n : this.group.getOwnNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) { - if (n instanceof WeightNode) { - WeightNode weightNode = (WeightNode) n; - int value = weightNode.getWeight(); + int weight = 0; - if (!seen || value > best) { - seen = true; - best = value; - } + for (WeightNode n : this.group.getOwnNodes(NodeType.WEIGHT, QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) { + int value = n.getWeight(); + if (!seen || value > weight) { + seen = true; + weight = value; } } - OptionalInt weight = seen ? OptionalInt.of(best) : OptionalInt.empty(); - - if (!weight.isPresent()) { + if (!seen) { Map configWeights = this.group.getPlugin().getConfiguration().get(ConfigKeys.GROUP_WEIGHTS); - Integer w = configWeights.get(this.group.getObjectName().toLowerCase()); - if (w != null) { - weight = OptionalInt.of(w); + Integer value = configWeights.get(this.group.getObjectName().toLowerCase()); + if (value != null) { + seen = true; + weight = value; } } - return weight; + return seen ? OptionalInt.of(weight) : OptionalInt.empty(); } }