Slightly optimize weight and display name lookups

This commit is contained in:
Luck 2020-12-11 10:38:57 +00:00
parent 45188c6334
commit 97d1deec9c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 17 additions and 28 deletions

View File

@ -72,11 +72,7 @@ public class MetaInfo extends GenericChildCommand {
Set<MetaNode> meta = new LinkedHashSet<>(); Set<MetaNode> meta = new LinkedHashSet<>();
// Collect data // Collect data
for (Node node : target.resolveInheritedNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) { for (Node node : target.resolveInheritedNodes(NodeType.META_OR_CHAT_META, QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) {
if (!NodeType.META_OR_CHAT_META.matches(node)) {
continue;
}
if (node instanceof PrefixNode) { if (node instanceof PrefixNode) {
PrefixNode pn = (PrefixNode) node; PrefixNode pn = (PrefixNode) node;
prefixes.add(Maps.immutableEntry(pn.getPriority(), pn)); prefixes.add(Maps.immutableEntry(pn.getPriority(), pn));

View File

@ -34,7 +34,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; 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.node.types.DisplayNameNode;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.query.QueryOptions;
@ -137,11 +137,8 @@ public class Group extends PermissionHolder {
public Optional<String> calculateDisplayName(QueryOptions queryOptions) { public Optional<String> calculateDisplayName(QueryOptions queryOptions) {
// query for a displayname node // query for a displayname node
for (Node n : getOwnNodes(queryOptions)) { for (DisplayNameNode n : getOwnNodes(NodeType.DISPLAY_NAME, queryOptions)) {
if (n instanceof DisplayNameNode) { return Optional.of(n.getDisplayName());
DisplayNameNode displayNameNode = (DisplayNameNode) n;
return Optional.of(displayNameNode.getDisplayName());
}
} }
// fallback to config // fallback to config

View File

@ -29,7 +29,7 @@ import me.lucko.luckperms.common.cache.Cache;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.query.QueryOptionsImpl; 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 net.luckperms.api.node.types.WeightNode;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -50,29 +50,25 @@ public class WeightCache extends Cache<OptionalInt> {
@Override @Override
protected @NonNull OptionalInt supply() { protected @NonNull OptionalInt supply() {
boolean seen = false; boolean seen = false;
int best = 0; int weight = 0;
for (Node n : this.group.getOwnNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) {
if (n instanceof WeightNode) {
WeightNode weightNode = (WeightNode) n;
int value = weightNode.getWeight();
if (!seen || value > best) { for (WeightNode n : this.group.getOwnNodes(NodeType.WEIGHT, QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL)) {
int value = n.getWeight();
if (!seen || value > weight) {
seen = true; seen = true;
best = value; weight = value;
}
} }
} }
OptionalInt weight = seen ? OptionalInt.of(best) : OptionalInt.empty(); if (!seen) {
if (!weight.isPresent()) {
Map<String, Integer> configWeights = this.group.getPlugin().getConfiguration().get(ConfigKeys.GROUP_WEIGHTS); Map<String, Integer> configWeights = this.group.getPlugin().getConfiguration().get(ConfigKeys.GROUP_WEIGHTS);
Integer w = configWeights.get(this.group.getObjectName().toLowerCase()); Integer value = configWeights.get(this.group.getObjectName().toLowerCase());
if (w != null) { if (value != null) {
weight = OptionalInt.of(w); seen = true;
weight = value;
} }
} }
return weight; return seen ? OptionalInt.of(weight) : OptionalInt.empty();
} }
} }