mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-03-28 22:46:02 +01:00
Add meta-value-selection config setting (#2167)
This commit is contained in:
parent
cc80fe5105
commit
4d7a6cb658
api/src/main/java/net/luckperms/api/query/meta
bukkit/src/main/resources
bungee/src/main/resources
common/src/main/java/me/lucko/luckperms/common
nukkit/src/main/resources
sponge/src/main/resources
velocity/src/main/resources
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.luckperms.api.query.meta;
|
||||
|
||||
import net.luckperms.api.node.types.MetaNode;
|
||||
import net.luckperms.api.query.OptionKey;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A function that selects the {@link MetaNode#getMetaValue() meta value} to be used in queries
|
||||
* against a given {@link MetaNode#getMetaKey() meta key}.
|
||||
*
|
||||
* @since 5.1
|
||||
*/
|
||||
public interface MetaValueSelector {
|
||||
|
||||
/**
|
||||
* The {@link OptionKey} for {@link MetaValueSelector}.
|
||||
*/
|
||||
OptionKey<MetaValueSelector> KEY = OptionKey.of("metavalueselector", MetaValueSelector.class);
|
||||
|
||||
/**
|
||||
* Selects the meta value to map to the given key.
|
||||
*
|
||||
* <p>The {@code values} list is guaranteed to contain at least 1 element.</p>
|
||||
*
|
||||
* @param key the key
|
||||
* @param values the values, in the order in which they were accumulated.
|
||||
* @return the selected value
|
||||
*/
|
||||
@NonNull String selectValue(@NonNull String key, @NonNull List<String> values);
|
||||
|
||||
}
|
@ -407,6 +407,22 @@ apply-global-groups: true
|
||||
# If users on this server should have global (non-world specific) groups applied
|
||||
apply-global-world-groups: true
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Meta lookup settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
||||
# Defines how meta values should be selected.
|
||||
#
|
||||
# - Possible options:
|
||||
# => inheritance Selects the meta value that was inherited first
|
||||
# => highest-number Selects the highest numerical meta value
|
||||
# => lowest-number Selects the lowest numerical meta value
|
||||
meta-value-selection-default: inheritance
|
||||
|
||||
# Defines how meta values should be selected per key.
|
||||
meta-value-selection:
|
||||
# max-homes: highest-number
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Inheritance settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
@ -415,6 +415,22 @@ apply-global-groups: true
|
||||
# If users on this server should have global (non-world specific) groups applied
|
||||
apply-global-world-groups: true
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Meta lookup settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
||||
# Defines how meta values should be selected.
|
||||
#
|
||||
# - Possible options:
|
||||
# => inheritance Selects the meta value that was inherited first
|
||||
# => highest-number Selects the highest numerical meta value
|
||||
# => lowest-number Selects the lowest numerical meta value
|
||||
meta-value-selection-default: inheritance
|
||||
|
||||
# Defines how meta values should be selected per key.
|
||||
meta-value-selection:
|
||||
# max-homes: highest-number
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Inheritance settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
@ -57,7 +57,7 @@ public class MetaCache extends SimpleMetaCache implements CachedMetaData {
|
||||
private final String verboseCheckTarget;
|
||||
|
||||
public MetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions, CacheMetadata metadata) {
|
||||
super(queryOptions);
|
||||
super(plugin, queryOptions);
|
||||
this.plugin = plugin;
|
||||
this.metadata = metadata;
|
||||
|
||||
|
@ -30,12 +30,15 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
|
||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||
import me.lucko.luckperms.common.metastacking.MetaStack;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.verbose.event.MetaCheckEvent;
|
||||
|
||||
import net.luckperms.api.cacheddata.CachedMetaData;
|
||||
import net.luckperms.api.metastacking.MetaStackDefinition;
|
||||
import net.luckperms.api.query.QueryOptions;
|
||||
import net.luckperms.api.query.meta.MetaValueSelector;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@ -50,6 +53,8 @@ import java.util.SortedMap;
|
||||
*/
|
||||
public class SimpleMetaCache implements CachedMetaData {
|
||||
|
||||
private final LuckPermsPlugin plugin;
|
||||
|
||||
/** The query options this container is holding data for */
|
||||
private final QueryOptions queryOptions;
|
||||
|
||||
@ -62,21 +67,29 @@ public class SimpleMetaCache implements CachedMetaData {
|
||||
protected MetaStack prefixStack = null;
|
||||
protected MetaStack suffixStack = null;
|
||||
|
||||
public SimpleMetaCache(QueryOptions queryOptions) {
|
||||
public SimpleMetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions) {
|
||||
this.plugin = plugin;
|
||||
this.queryOptions = queryOptions;
|
||||
}
|
||||
|
||||
public void loadMeta(MetaAccumulator meta) {
|
||||
this.meta = Multimaps.asMap(ImmutableListMultimap.copyOf(meta.getMeta()));
|
||||
|
||||
final MetaValueSelector metaValueSelector = this.queryOptions.option(MetaValueSelector.KEY)
|
||||
.orElseGet(() -> this.plugin.getConfiguration().get(ConfigKeys.META_VALUE_SELECTOR));
|
||||
|
||||
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
|
||||
for (Map.Entry<String, List<String>> e : this.meta.entrySet()) {
|
||||
if (e.getValue().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// take the value which was accumulated first
|
||||
builder.put(e.getKey(), e.getValue().get(0));
|
||||
final String selected = metaValueSelector.selectValue(e.getKey(), e.getValue());
|
||||
if (selected == null) {
|
||||
throw new NullPointerException(metaValueSelector + " returned null");
|
||||
}
|
||||
|
||||
builder.put(e.getKey(), selected);
|
||||
}
|
||||
this.flattenedMeta = builder.build();
|
||||
|
||||
|
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.cacheddata.type;
|
||||
|
||||
import net.luckperms.api.query.meta.MetaValueSelector;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SimpleMetaValueSelector implements MetaValueSelector {
|
||||
private final Map<String, Strategy> strategies;
|
||||
private final Strategy defaultStrategy;
|
||||
|
||||
public SimpleMetaValueSelector(Map<String, Strategy> strategies, Strategy defaultStrategy) {
|
||||
this.strategies = strategies;
|
||||
this.defaultStrategy = defaultStrategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String selectValue(@NonNull String key, @NonNull List<String> values) {
|
||||
return this.strategies.getOrDefault(key, this.defaultStrategy).select(values);
|
||||
}
|
||||
|
||||
public enum Strategy {
|
||||
INHERITANCE {
|
||||
@Override
|
||||
public String select(List<String> values) {
|
||||
return values.get(0);
|
||||
}
|
||||
},
|
||||
HIGHEST_NUMBER {
|
||||
private final DoubleSelectionPredicate selection = (value, current) -> value > current;
|
||||
|
||||
@Override
|
||||
public String select(List<String> values) {
|
||||
return selectNumber(values, this.selection);
|
||||
}
|
||||
},
|
||||
LOWEST_NUMBER {
|
||||
private final DoubleSelectionPredicate selection = (value, current) -> value < current;
|
||||
|
||||
@Override
|
||||
public String select(List<String> values) {
|
||||
return selectNumber(values, this.selection);
|
||||
}
|
||||
};
|
||||
|
||||
public abstract String select(List<String> values);
|
||||
|
||||
public static Strategy parse(String s) {
|
||||
try {
|
||||
return Strategy.valueOf(s.replace('-', '_').toUpperCase());
|
||||
} catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface DoubleSelectionPredicate {
|
||||
boolean shouldSelect(double value, double current);
|
||||
}
|
||||
|
||||
private static String selectNumber(List<String> values, DoubleSelectionPredicate selection) {
|
||||
double current = 0;
|
||||
String selected = null;
|
||||
|
||||
for (String value : values) {
|
||||
try {
|
||||
double parse = Double.parseDouble(value);
|
||||
if (selected == null || selection.shouldSelect(parse, current)) {
|
||||
selected = value;
|
||||
current = parse;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
return selected != null ? selected : Strategy.INHERITANCE.select(values);
|
||||
}
|
||||
}
|
@ -26,7 +26,9 @@
|
||||
package me.lucko.luckperms.common.config;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import me.lucko.luckperms.common.cacheddata.type.SimpleMetaValueSelector;
|
||||
import me.lucko.luckperms.common.command.utils.ArgumentParser;
|
||||
import me.lucko.luckperms.common.graph.TraversalAlgorithm;
|
||||
import me.lucko.luckperms.common.metastacking.SimpleMetaStackDefinition;
|
||||
@ -45,6 +47,7 @@ import net.luckperms.api.model.data.TemporaryNodeMergeStrategy;
|
||||
import net.luckperms.api.query.Flag;
|
||||
import net.luckperms.api.query.QueryMode;
|
||||
import net.luckperms.api.query.QueryOptions;
|
||||
import net.luckperms.api.query.meta.MetaValueSelector;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
@ -53,6 +56,7 @@ import java.util.EnumMap;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
@ -271,6 +275,22 @@ public final class ConfigKeys {
|
||||
*/
|
||||
public static final ConfigKey<Boolean> POST_TRAVERSAL_INHERITANCE_SORT = booleanKey("post-traversal-inheritance-sort", false);
|
||||
|
||||
/**
|
||||
* The meta value selector
|
||||
*/
|
||||
public static final ConfigKey<MetaValueSelector> META_VALUE_SELECTOR = customKey(c -> {
|
||||
SimpleMetaValueSelector.Strategy defaultStrategy = SimpleMetaValueSelector.Strategy.parse(c.getString("meta-value-selection-default", "inheritance"));
|
||||
Map<String, SimpleMetaValueSelector.Strategy> strategies = c.getStringMap("meta-value-selection", ImmutableMap.of()).entrySet().stream()
|
||||
.map(e -> {
|
||||
SimpleMetaValueSelector.Strategy parse = SimpleMetaValueSelector.Strategy.parse(e.getValue());
|
||||
return parse == null ? null : Maps.immutableEntry(e.getKey(), parse);
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(ImmutableCollectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
|
||||
return new SimpleMetaValueSelector(strategies, defaultStrategy);
|
||||
});
|
||||
|
||||
/**
|
||||
* The configured group weightings
|
||||
*/
|
||||
|
@ -402,6 +402,22 @@ apply-global-groups: true
|
||||
# If users on this server should have global (non-world specific) groups applied
|
||||
apply-global-world-groups: true
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Meta lookup settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
||||
# Defines how meta values should be selected.
|
||||
#
|
||||
# - Possible options:
|
||||
# => inheritance Selects the meta value that was inherited first
|
||||
# => highest-number Selects the highest numerical meta value
|
||||
# => lowest-number Selects the lowest numerical meta value
|
||||
meta-value-selection-default: inheritance
|
||||
|
||||
# Defines how meta values should be selected per key.
|
||||
meta-value-selection:
|
||||
# max-homes: highest-number
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Inheritance settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
@ -416,6 +416,23 @@ apply-global-groups = true
|
||||
# If users on this server should have global (non-world specific) groups applied
|
||||
apply-global-world-groups = true
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Meta lookup settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
||||
# Defines how meta values should be selected.
|
||||
#
|
||||
# - Possible options:
|
||||
# => inheritance Selects the meta value that was inherited first
|
||||
# => highest-number Selects the highest numerical meta value
|
||||
# => lowest-number Selects the lowest numerical meta value
|
||||
meta-value-selection-default = "inheritance"
|
||||
|
||||
# Defines how meta values should be selected per key.
|
||||
meta-value-selection {
|
||||
#max-homes = "highest-number"
|
||||
}
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Inheritance settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
@ -406,6 +406,22 @@ apply-global-groups: true
|
||||
# If users on this server should have global (non-world specific) groups applied
|
||||
apply-global-world-groups: true
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Meta lookup settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
||||
# Defines how meta values should be selected.
|
||||
#
|
||||
# - Possible options:
|
||||
# => inheritance Selects the meta value that was inherited first
|
||||
# => highest-number Selects the highest numerical meta value
|
||||
# => lowest-number Selects the lowest numerical meta value
|
||||
meta-value-selection-default: inheritance
|
||||
|
||||
# Defines how meta values should be selected per key.
|
||||
meta-value-selection:
|
||||
# max-homes: highest-number
|
||||
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
# | Inheritance settings | #
|
||||
# +----------------------------------------------------------------------------------------------+ #
|
||||
|
Loading…
Reference in New Issue
Block a user