Add config option to disable context calculators

This commit is contained in:
Luck 2021-11-27 09:49:01 +00:00
parent 9204848ffb
commit 594f50bee7
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
13 changed files with 105 additions and 37 deletions

View File

@ -551,6 +551,13 @@ apply-bukkit-attachment-permissions: true
# | Extra settings | #
# +----------------------------------------------------------------------------------------------+ #
# A list of context calculators which will be skipped when calculating contexts.
#
# - You can disable context calculators by either:
# => specifying the Java class name used by the calculator (e.g. com.example.ExampleCalculator)
# => specifying a sub-section of the Java package used by the calculator (e.g. com.example)
disabled-context-calculators: []
# Allows you to set "aliases" for the worlds sent forward for context calculation.
#
# - These aliases are provided in addition to the real world name. Applied recursively.

View File

@ -531,6 +531,13 @@ apply-bungee-config-permissions: false
# | Extra settings | #
# +----------------------------------------------------------------------------------------------+ #
# A list of context calculators which will be skipped when calculating contexts.
#
# - You can disable context calculators by either:
# => specifying the Java class name used by the calculator (e.g. com.example.ExampleCalculator)
# => specifying a sub-section of the Java package used by the calculator (e.g. com.example)
disabled-context-calculators: []
# Allows you to set "aliases" for the worlds sent forward for context calculation.
#
# - These aliases are provided in addition to the real world name. Applied recursively.

View File

@ -25,6 +25,8 @@
package me.lucko.luckperms.common.command.tabcomplete;
import me.lucko.luckperms.common.util.Predicates;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -46,7 +48,7 @@ public interface CompletionSupplier {
}
static CompletionSupplier startsWith(Supplier<Stream<String>> stringsSupplier) {
return partial -> stringsSupplier.get().filter(TabCompleter.startsWithIgnoreCase(partial)).collect(Collectors.toList());
return partial -> stringsSupplier.get().filter(Predicates.startsWithIgnoreCase(partial)).collect(Collectors.toList());
}
static CompletionSupplier contains(String... strings) {
@ -58,7 +60,7 @@ public interface CompletionSupplier {
}
static CompletionSupplier contains(Supplier<Stream<String>> stringsSupplier) {
return partial -> stringsSupplier.get().filter(TabCompleter.containsIgnoreCase(partial)).collect(Collectors.toList());
return partial -> stringsSupplier.get().filter(Predicates.containsIgnoreCase(partial)).collect(Collectors.toList());
}
List<String> supplyCompletions(String partial);

View File

@ -29,9 +29,7 @@ import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
/**
* Utility for computing tab completion results
@ -99,22 +97,4 @@ public class TabCompleter {
return this.suppliers.getOrDefault(position, CompletionSupplier.EMPTY).supplyCompletions(partial);
}
static Predicate<String> startsWithIgnoreCase(String prefix) {
return string -> {
if (string.length() < prefix.length()) {
return false;
}
return string.regionMatches(true, 0, prefix, 0, prefix.length());
};
}
static Predicate<String> containsIgnoreCase(String substring) {
return string -> {
if (string.length() < substring.length()) {
return false;
}
return string.toLowerCase(Locale.ROOT).contains(substring.toLowerCase(Locale.ROOT));
};
}
}

View File

@ -30,6 +30,7 @@ import com.google.common.base.Splitter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.treeview.PermissionRegistry;
import me.lucko.luckperms.common.treeview.TreeNode;
import me.lucko.luckperms.common.util.Predicates;
import net.luckperms.api.context.ImmutableContextSet;
@ -90,7 +91,7 @@ public final class TabCompletions {
}
return root.getChildren().get().keySet().stream()
.filter(TabCompleter.startsWithIgnoreCase(incomplete))
.filter(Predicates.startsWithIgnoreCase(incomplete))
.map(s -> String.join(".", parts) + "." + s)
.collect(Collectors.toList());
};
@ -112,7 +113,7 @@ public final class TabCompletions {
String value = partial.substring(index + 1).trim();
Set<String> potentialValues = potentialContexts.getValues(key);
return potentialValues.stream()
.filter(TabCompleter.startsWithIgnoreCase(value))
.filter(Predicates.startsWithIgnoreCase(value))
.map(s -> key + "=" + s)
.collect(Collectors.toList());
};

View File

@ -44,6 +44,7 @@ import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.storage.implementation.split.SplitStorageType;
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
import me.lucko.luckperms.common.util.ImmutableCollectors;
import me.lucko.luckperms.common.util.Predicates;
import net.luckperms.api.context.ContextSatisfyMode;
import net.luckperms.api.metastacking.DuplicateRemovalFunction;
@ -64,6 +65,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@ -515,6 +517,16 @@ public final class ConfigKeys {
*/
public static final ConfigKey<Boolean> FABRIC_INTEGRATED_SERVER_OWNER_BYPASSES_CHECKS = booleanKey("integrated-server-owner-bypasses-checks", true);
/**
* Disabled context calculators
*/
public static final ConfigKey<Set<Predicate<String>>> DISABLED_CONTEXT_CALCULATORS = key(c -> {
return c.getStringList("disabled-context-calculators", ImmutableList.of())
.stream()
.map(Predicates::startsWithIgnoreCase)
.collect(ImmutableCollectors.toSet());
});
/**
* The world rewrites map
*/

View File

@ -42,8 +42,10 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
/**
* Base implementation of {@link ContextManager} which caches content lookups.
@ -116,6 +118,16 @@ public abstract class ContextManager<S, P extends S> {
protected abstract void invalidateCache(S subject);
public void registerCalculator(ContextCalculator<? super S> calculator) {
String calculatorClass = calculator.getClass().getName();
Set<Predicate<String>> disabledCalculators = this.plugin.getConfiguration().get(ConfigKeys.DISABLED_CONTEXT_CALCULATORS);
for (Predicate<String> disabledPattern : disabledCalculators) {
if (disabledPattern.test(calculatorClass)) {
this.plugin.getLogger().info("Ignoring registration of disabled context calculator: " + calculatorClass);
return;
}
}
this.calculators.add(calculator);
}

View File

@ -29,6 +29,7 @@ import com.google.common.collect.Range;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Locale;
import java.util.function.Predicate;
/**
@ -86,4 +87,22 @@ public final class Predicates {
return t::equals;
}
public static Predicate<String> startsWithIgnoreCase(String prefix) {
return string -> {
if (string.length() < prefix.length()) {
return false;
}
return string.regionMatches(true, 0, prefix, 0, prefix.length());
};
}
public static Predicate<String> containsIgnoreCase(String substring) {
return string -> {
if (string.length() < substring.length()) {
return false;
}
return string.toLowerCase(Locale.ROOT).contains(substring.toLowerCase(Locale.ROOT));
};
}
}

View File

@ -114,18 +114,18 @@ public class ContextSetTest {
@Test
public void testImmutableContainsAll() {
ImmutableContextSetImpl set = (ImmutableContextSetImpl) new ImmutableContextSetImpl.BuilderImpl()
.add("test", "a")
.add("test", "b")
.add("test", "c")
.add("other", "a")
.add("other", "b")
.add("aaa", "a")
.add("aaa", "b")
.add("aaa", "c")
.add("bbb", "a")
.add("bbb", "b")
.build();
List<Consumer<ImmutableContextSet.Builder>> trueTests = ImmutableList.of(
builder -> builder.add("test", "a").add("other", "a"),
builder -> builder.add("test", "b").add("other", "a"),
builder -> builder.add("test", "c").add("other", "a"),
builder -> builder.add("test", "c").add("other", "b")
builder -> builder.add("aaa", "a").add("bbb", "a"),
builder -> builder.add("aaa", "b").add("bbb", "a"),
builder -> builder.add("aaa", "c").add("bbb", "a"),
builder -> builder.add("aaa", "c").add("bbb", "b")
);
for (Consumer<ImmutableContextSet.Builder> test : trueTests) {
@ -138,10 +138,10 @@ public class ContextSetTest {
}
List<Consumer<ImmutableContextSet.Builder>> falseTests = ImmutableList.of(
builder -> builder.add("test", "a").add("other", "z"),
builder -> builder.add("test", "b").add("other", "z"),
builder -> builder.add("test", "b"),
builder -> builder.add("test", "c"),
builder -> builder.add("aaa", "a").add("bbb", "z"),
builder -> builder.add("aaa", "b").add("bbb", "z"),
builder -> builder.add("aaa", "b"),
builder -> builder.add("aaa", "c"),
builder -> {}
);

View File

@ -537,6 +537,13 @@ integrated-server-owner-bypasses-checks = true
# | Extra settings | #
# +----------------------------------------------------------------------------------------------+ #
# A list of context calculators which will be skipped when calculating contexts.
#
# - You can disable context calculators by either:
# => specifying the Java class name used by the calculator (e.g. com.example.ExampleCalculator)
# => specifying a sub-section of the Java package used by the calculator (e.g. com.example)
disabled-context-calculators = []
# Allows you to set "aliases" for the worlds sent forward for context calculation.
#
# - These aliases are provided in addition to the real world name. Applied recursively.

View File

@ -546,6 +546,13 @@ apply-nukkit-attachment-permissions: true
# | Extra settings | #
# +----------------------------------------------------------------------------------------------+ #
# A list of context calculators which will be skipped when calculating contexts.
#
# - You can disable context calculators by either:
# => specifying the Java class name used by the calculator (e.g. com.example.ExampleCalculator)
# => specifying a sub-section of the Java package used by the calculator (e.g. com.example)
disabled-context-calculators: []
# Allows you to set "aliases" for the worlds sent forward for context calculation.
#
# - These aliases are provided in addition to the real world name. Applied recursively.

View File

@ -547,6 +547,13 @@ apply-sponge-default-subjects=true
# | Extra settings | #
# +----------------------------------------------------------------------------------------------+ #
# A list of context calculators which will be skipped when calculating contexts.
#
# - You can disable context calculators by either:
# => specifying the Java class name used by the calculator (e.g. com.example.ExampleCalculator)
# => specifying a sub-section of the Java package used by the calculator (e.g. com.example)
disabled-context-calculators = []
# Allows you to set "aliases" for the worlds sent forward for context calculation.
#
# - These aliases are provided in addition to the real world name. Applied recursively.

View File

@ -517,6 +517,13 @@ apply-shorthand: true
# | Extra settings | #
# +----------------------------------------------------------------------------------------------+ #
# A list of context calculators which will be skipped when calculating contexts.
#
# - You can disable context calculators by either:
# => specifying the Java class name used by the calculator (e.g. com.example.ExampleCalculator)
# => specifying a sub-section of the Java package used by the calculator (e.g. com.example)
disabled-context-calculators: []
# Allows you to set "aliases" for the worlds sent forward for context calculation.
#
# - These aliases are provided in addition to the real world name. Applied recursively.