Refactor config handler classes

This commit is contained in:
Luck 2020-05-25 10:42:32 +01:00
parent 5a6176def5
commit 5c0d82f306
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
23 changed files with 404 additions and 382 deletions

View File

@ -25,7 +25,7 @@
package me.lucko.luckperms.bukkit; package me.lucko.luckperms.bukkit;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;

View File

@ -48,7 +48,7 @@ import me.lucko.luckperms.common.api.implementation.ApiUser;
import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.command.access.CommandPermission; import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus; import me.lucko.luckperms.common.event.AbstractEventBus;
import me.lucko.luckperms.common.messaging.MessagingFactory; import me.lucko.luckperms.common.messaging.MessagingFactory;

View File

@ -25,7 +25,7 @@
package me.lucko.luckperms.bungee; package me.lucko.luckperms.bungee;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;

View File

@ -35,7 +35,7 @@ import me.lucko.luckperms.bungee.messaging.BungeeMessagingFactory;
import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.api.LuckPermsApiProvider;
import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.command.CommandManager; import me.lucko.luckperms.common.command.CommandManager;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus; import me.lucko.luckperms.common.event.AbstractEventBus;
import me.lucko.luckperms.common.messaging.MessagingFactory; import me.lucko.luckperms.common.messaging.MessagingFactory;

View File

@ -33,7 +33,7 @@ import me.lucko.luckperms.common.sender.Sender;
*/ */
public abstract class CommandException extends Exception { public abstract class CommandException extends Exception {
public abstract CommandResult handle(Sender sender); protected abstract CommandResult handle(Sender sender);
public CommandResult handle(Sender sender, String label, Command<?> command) { public CommandResult handle(Sender sender, String label, Command<?> command) {
return handle(sender); return handle(sender);

View File

@ -221,7 +221,7 @@ public class ArgumentParser {
public static class DetailedUsageException extends ArgumentException { public static class DetailedUsageException extends ArgumentException {
@Override @Override
public CommandResult handle(Sender sender) { protected CommandResult handle(Sender sender) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -240,7 +240,7 @@ public class ArgumentParser {
public static class PastDateException extends ArgumentException { public static class PastDateException extends ArgumentException {
@Override @Override
public CommandResult handle(Sender sender) { protected CommandResult handle(Sender sender) {
Message.PAST_DATE_ERROR.send(sender); Message.PAST_DATE_ERROR.send(sender);
return CommandResult.INVALID_ARGS; return CommandResult.INVALID_ARGS;
} }
@ -254,7 +254,7 @@ public class ArgumentParser {
} }
@Override @Override
public CommandResult handle(Sender sender) { protected CommandResult handle(Sender sender) {
Message.ILLEGAL_DATE_ERROR.send(sender, this.invalidDate); Message.ILLEGAL_DATE_ERROR.send(sender, this.invalidDate);
return CommandResult.INVALID_ARGS; return CommandResult.INVALID_ARGS;
} }

View File

@ -1,108 +0,0 @@
/*
* 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.config;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
/**
* An abstract implementation of {@link LuckPermsConfiguration}.
*
* <p>Values are loaded into memory on init.</p>
*/
public class AbstractConfiguration implements LuckPermsConfiguration {
/**
* The configurations loaded values.
*
* <p>The value corresponding to each key is stored at the index defined
* by {@link ConfigKey#ordinal()}.</p>
*/
private Object[] values = null;
private final LuckPermsPlugin plugin;
private final ConfigurationAdapter adapter;
// the contextsfile handler
private final ContextsFile contextsFile = new ContextsFile(this);
public AbstractConfiguration(LuckPermsPlugin plugin, ConfigurationAdapter adapter) {
this.plugin = plugin;
this.adapter = adapter;
load();
}
@SuppressWarnings("unchecked")
@Override
public <T> T get(ConfigKey<T> key) {
return (T) this.values[key.ordinal()];
}
@Override
public synchronized void load() {
// if this is a reload operation
boolean reload = true;
// if values are null, must be loading for the first time
if (this.values == null) {
this.values = new Object[ConfigKeys.getKeys().size()];
reload = false;
}
for (ConfigKey<?> key : ConfigKeys.getKeys()) {
// don't reload enduring keys.
if (reload && key instanceof ConfigKeyTypes.EnduringKey) {
continue;
}
// load the value for the key
Object value = key.get(this.adapter);
this.values[key.ordinal()] = value;
}
// load the contexts file
this.contextsFile.load();
}
@Override
public void reload() {
this.adapter.reload();
load();
getPlugin().getEventDispatcher().dispatchConfigReload();
}
@Override
public LuckPermsPlugin getPlugin() {
return this.plugin;
}
@Override
public ContextsFile getContextsFile() {
return this.contextsFile;
}
}

View File

@ -1,136 +0,0 @@
/*
* 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.config;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter;
import java.util.Map;
import java.util.function.Function;
public class ConfigKeyTypes {
private static final KeyFactory<Boolean> BOOLEAN = ConfigurationAdapter::getBoolean;
private static final KeyFactory<String> STRING = ConfigurationAdapter::getString;
private static final KeyFactory<String> LOWERCASE_STRING = (adapter, path, def) -> adapter.getString(path, def).toLowerCase();
private static final KeyFactory<Map<String, String>> STRING_MAP = (config, path, def) -> ImmutableMap.copyOf(config.getStringMap(path, ImmutableMap.of()));
public static BaseConfigKey<Boolean> booleanKey(String path, boolean def) {
return BOOLEAN.createKey(path, def);
}
public static BaseConfigKey<String> stringKey(String path, String def) {
return STRING.createKey(path, def);
}
public static BaseConfigKey<String> lowercaseStringKey(String path, String def) {
return LOWERCASE_STRING.createKey(path, def);
}
public static BaseConfigKey<Map<String, String>> mapKey(String path) {
return STRING_MAP.createKey(path, null);
}
public static <T> CustomKey<T> customKey(Function<ConfigurationAdapter, T> function) {
return new CustomKey<>(function);
}
public static <T> EnduringKey<T> enduringKey(ConfigKey<T> delegate) {
return new EnduringKey<>(delegate);
}
/**
* Functional interface that extracts values from a {@link ConfigurationAdapter}.
*
* @param <T> the value type.
*/
@FunctionalInterface
public interface KeyFactory<T> {
T getValue(ConfigurationAdapter config, String path, T def);
default BaseConfigKey<T> createKey(String path, T def) {
return new FunctionalKey<>(this, path, def);
}
}
public abstract static class BaseConfigKey<T> implements ConfigKey<T> {
int ordinal = -1;
BaseConfigKey() {
}
@Override
public int ordinal() {
return this.ordinal;
}
}
private static class FunctionalKey<T> extends BaseConfigKey<T> implements ConfigKey<T> {
private final KeyFactory<T> factory;
private final String path;
private final T def;
FunctionalKey(KeyFactory<T> factory, String path, T def) {
this.factory = factory;
this.path = path;
this.def = def;
}
@Override
public T get(ConfigurationAdapter adapter) {
return this.factory.getValue(adapter, this.path, this.def);
}
}
public static class CustomKey<T> extends BaseConfigKey<T> {
private final Function<ConfigurationAdapter, T> function;
private CustomKey(Function<ConfigurationAdapter, T> function) {
this.function = function;
}
@Override
public T get(ConfigurationAdapter adapter) {
return this.function.apply(adapter);
}
}
public static class EnduringKey<T> extends BaseConfigKey<T> {
private final ConfigKey<T> delegate;
private EnduringKey(ConfigKey<T> delegate) {
this.delegate = delegate;
}
@Override
public T get(ConfigurationAdapter adapter) {
return this.delegate.get(adapter);
}
}
}

View File

@ -30,6 +30,9 @@ import com.google.common.collect.Maps;
import me.lucko.luckperms.common.cacheddata.type.SimpleMetaValueSelector; import me.lucko.luckperms.common.cacheddata.type.SimpleMetaValueSelector;
import me.lucko.luckperms.common.command.utils.ArgumentParser; import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.config.generic.KeyedConfiguration;
import me.lucko.luckperms.common.config.generic.key.ConfigKey;
import me.lucko.luckperms.common.config.generic.key.SimpleConfigKey;
import me.lucko.luckperms.common.graph.TraversalAlgorithm; import me.lucko.luckperms.common.graph.TraversalAlgorithm;
import me.lucko.luckperms.common.metastacking.SimpleMetaStackDefinition; import me.lucko.luckperms.common.metastacking.SimpleMetaStackDefinition;
import me.lucko.luckperms.common.metastacking.StandardStackElements; import me.lucko.luckperms.common.metastacking.StandardStackElements;
@ -51,9 +54,7 @@ import net.luckperms.api.query.QueryMode;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.query.QueryOptions;
import net.luckperms.api.query.meta.MetaValueSelector; import net.luckperms.api.query.meta.MetaValueSelector;
import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
@ -62,12 +63,12 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import static me.lucko.luckperms.common.config.ConfigKeyTypes.booleanKey; import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.booleanKey;
import static me.lucko.luckperms.common.config.ConfigKeyTypes.customKey; import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.key;
import static me.lucko.luckperms.common.config.ConfigKeyTypes.enduringKey; import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.lowercaseStringKey;
import static me.lucko.luckperms.common.config.ConfigKeyTypes.lowercaseStringKey; import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.mapKey;
import static me.lucko.luckperms.common.config.ConfigKeyTypes.mapKey; import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.notReloadable;
import static me.lucko.luckperms.common.config.ConfigKeyTypes.stringKey; import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.stringKey;
/** /**
* All of the {@link ConfigKey}s used by LuckPerms. * All of the {@link ConfigKey}s used by LuckPerms.
@ -86,7 +87,7 @@ public final class ConfigKeys {
/** /**
* How many minutes to wait between syncs. A value <= 0 will disable syncing. * How many minutes to wait between syncs. A value <= 0 will disable syncing.
*/ */
public static final ConfigKey<Integer> SYNC_TIME = enduringKey(customKey(c -> { public static final ConfigKey<Integer> SYNC_TIME = notReloadable(key(c -> {
int val = c.getInteger("sync-minutes", -1); int val = c.getInteger("sync-minutes", -1);
if (val == -1) { if (val == -1) {
val = c.getInteger("data.sync-minutes", -1); val = c.getInteger("data.sync-minutes", -1);
@ -97,7 +98,7 @@ public final class ConfigKeys {
/** /**
* The default global contexts instance * The default global contexts instance
*/ */
public static final ConfigKey<QueryOptions> GLOBAL_QUERY_OPTIONS = customKey(c -> { public static final ConfigKey<QueryOptions> GLOBAL_QUERY_OPTIONS = key(c -> {
Set<Flag> flags = EnumSet.of(Flag.RESOLVE_INHERITANCE); Set<Flag> flags = EnumSet.of(Flag.RESOLVE_INHERITANCE);
if (c.getBoolean("include-global", true)) { if (c.getBoolean("include-global", true)) {
flags.add(Flag.INCLUDE_NODES_WITHOUT_SERVER_CONTEXT); flags.add(Flag.INCLUDE_NODES_WITHOUT_SERVER_CONTEXT);
@ -118,7 +119,7 @@ public final class ConfigKeys {
/** /**
* The default contexts satisfy mode * The default contexts satisfy mode
*/ */
public static final ConfigKey<ContextSatisfyMode> CONTEXT_SATISFY_MODE = customKey(c -> { public static final ConfigKey<ContextSatisfyMode> CONTEXT_SATISFY_MODE = key(c -> {
String value = c.getString("context-satisfy-mode", "at-least-one-value-per-key"); String value = c.getString("context-satisfy-mode", "at-least-one-value-per-key");
if (value.toLowerCase().equals("all-values-per-key")) { if (value.toLowerCase().equals("all-values-per-key")) {
return ContextSatisfyMode.ALL_VALUES_PER_KEY; return ContextSatisfyMode.ALL_VALUES_PER_KEY;
@ -149,7 +150,7 @@ public final class ConfigKeys {
/** /**
* If LuckPerms should update the list of commands sent to the client when permissions are changed. * If LuckPerms should update the list of commands sent to the client when permissions are changed.
*/ */
public static final ConfigKey<Boolean> UPDATE_CLIENT_COMMAND_LIST = enduringKey(booleanKey("update-client-command-list", true)); public static final ConfigKey<Boolean> UPDATE_CLIENT_COMMAND_LIST = notReloadable(booleanKey("update-client-command-list", true));
/** /**
* If LuckPerms should attempt to resolve Vanilla command target selectors for LP commands. * If LuckPerms should attempt to resolve Vanilla command target selectors for LP commands.
@ -159,7 +160,7 @@ public final class ConfigKeys {
/** /**
* Controls how temporary add commands should behave * Controls how temporary add commands should behave
*/ */
public static final ConfigKey<TemporaryNodeMergeStrategy> TEMPORARY_ADD_BEHAVIOUR = customKey(c -> { public static final ConfigKey<TemporaryNodeMergeStrategy> TEMPORARY_ADD_BEHAVIOUR = key(c -> {
String option = c.getString("temporary-add-behaviour", "deny").toLowerCase(); String option = c.getString("temporary-add-behaviour", "deny").toLowerCase();
if (!option.equals("deny") && !option.equals("replace") && !option.equals("accumulate")) { if (!option.equals("deny") && !option.equals("replace") && !option.equals("accumulate")) {
option = "deny"; option = "deny";
@ -171,7 +172,7 @@ public final class ConfigKeys {
/** /**
* How primary groups should be calculated. * How primary groups should be calculated.
*/ */
public static final ConfigKey<String> PRIMARY_GROUP_CALCULATION_METHOD = enduringKey(customKey(c -> { public static final ConfigKey<String> PRIMARY_GROUP_CALCULATION_METHOD = notReloadable(key(c -> {
String option = c.getString("primary-group-calculation", "stored").toLowerCase(); String option = c.getString("primary-group-calculation", "stored").toLowerCase();
if (!option.equals("stored") && !option.equals("parents-by-weight") && !option.equals("all-parents-by-weight")) { if (!option.equals("stored") && !option.equals("parents-by-weight") && !option.equals("all-parents-by-weight")) {
option = "stored"; option = "stored";
@ -183,7 +184,7 @@ public final class ConfigKeys {
/** /**
* A function to create primary group holder instances based upon the {@link #PRIMARY_GROUP_CALCULATION_METHOD} setting. * A function to create primary group holder instances based upon the {@link #PRIMARY_GROUP_CALCULATION_METHOD} setting.
*/ */
public static final ConfigKey<Function<User, PrimaryGroupHolder>> PRIMARY_GROUP_CALCULATION = enduringKey(customKey(c -> { public static final ConfigKey<Function<User, PrimaryGroupHolder>> PRIMARY_GROUP_CALCULATION = notReloadable(key(c -> {
String option = PRIMARY_GROUP_CALCULATION_METHOD.get(c); String option = PRIMARY_GROUP_CALCULATION_METHOD.get(c);
switch (option) { switch (option) {
case "stored": case "stored":
@ -215,12 +216,12 @@ public final class ConfigKeys {
/** /**
* If wildcards are being applied * If wildcards are being applied
*/ */
public static final ConfigKey<Boolean> APPLYING_WILDCARDS = enduringKey(booleanKey("apply-wildcards", true)); public static final ConfigKey<Boolean> APPLYING_WILDCARDS = notReloadable(booleanKey("apply-wildcards", true));
/** /**
* If Sponge's implicit permission inheritance system should be applied * If Sponge's implicit permission inheritance system should be applied
*/ */
public static final ConfigKey<Boolean> APPLYING_WILDCARDS_SPONGE = enduringKey(customKey(c -> { public static final ConfigKey<Boolean> APPLYING_WILDCARDS_SPONGE = notReloadable(key(c -> {
boolean def = c.getPlugin().getBootstrap().getType() == Platform.Type.SPONGE; boolean def = c.getPlugin().getBootstrap().getType() == Platform.Type.SPONGE;
return c.getBoolean("apply-sponge-implicit-wildcards", def); return c.getBoolean("apply-sponge-implicit-wildcards", def);
})); }));
@ -228,57 +229,57 @@ public final class ConfigKeys {
/** /**
* If regex permissions are being applied * If regex permissions are being applied
*/ */
public static final ConfigKey<Boolean> APPLYING_REGEX = enduringKey(booleanKey("apply-regex", true)); public static final ConfigKey<Boolean> APPLYING_REGEX = notReloadable(booleanKey("apply-regex", true));
/** /**
* If shorthand permissions are being applied * If shorthand permissions are being applied
*/ */
public static final ConfigKey<Boolean> APPLYING_SHORTHAND = enduringKey(booleanKey("apply-shorthand", true)); public static final ConfigKey<Boolean> APPLYING_SHORTHAND = notReloadable(booleanKey("apply-shorthand", true));
/** /**
* If Bukkit child permissions are being applied. This setting is ignored on other platforms. * If Bukkit child permissions are being applied. This setting is ignored on other platforms.
*/ */
public static final ConfigKey<Boolean> APPLY_BUKKIT_CHILD_PERMISSIONS = enduringKey(booleanKey("apply-bukkit-child-permissions", true)); public static final ConfigKey<Boolean> APPLY_BUKKIT_CHILD_PERMISSIONS = notReloadable(booleanKey("apply-bukkit-child-permissions", true));
/** /**
* If Bukkit default permissions are being applied. This setting is ignored on other platforms. * If Bukkit default permissions are being applied. This setting is ignored on other platforms.
*/ */
public static final ConfigKey<Boolean> APPLY_BUKKIT_DEFAULT_PERMISSIONS = enduringKey(booleanKey("apply-bukkit-default-permissions", true)); public static final ConfigKey<Boolean> APPLY_BUKKIT_DEFAULT_PERMISSIONS = notReloadable(booleanKey("apply-bukkit-default-permissions", true));
/** /**
* If Bukkit attachment permissions are being applied. This setting is ignored on other platforms. * If Bukkit attachment permissions are being applied. This setting is ignored on other platforms.
*/ */
public static final ConfigKey<Boolean> APPLY_BUKKIT_ATTACHMENT_PERMISSIONS = enduringKey(booleanKey("apply-bukkit-attachment-permissions", true)); public static final ConfigKey<Boolean> APPLY_BUKKIT_ATTACHMENT_PERMISSIONS = notReloadable(booleanKey("apply-bukkit-attachment-permissions", true));
/** /**
* If Nukkit child permissions are being applied. This setting is ignored on other platforms. * If Nukkit child permissions are being applied. This setting is ignored on other platforms.
*/ */
public static final ConfigKey<Boolean> APPLY_NUKKIT_CHILD_PERMISSIONS = enduringKey(booleanKey("apply-nukkit-child-permissions", true)); public static final ConfigKey<Boolean> APPLY_NUKKIT_CHILD_PERMISSIONS = notReloadable(booleanKey("apply-nukkit-child-permissions", true));
/** /**
* If Nukkit default permissions are being applied. This setting is ignored on other platforms. * If Nukkit default permissions are being applied. This setting is ignored on other platforms.
*/ */
public static final ConfigKey<Boolean> APPLY_NUKKIT_DEFAULT_PERMISSIONS = enduringKey(booleanKey("apply-nukkit-default-permissions", true)); public static final ConfigKey<Boolean> APPLY_NUKKIT_DEFAULT_PERMISSIONS = notReloadable(booleanKey("apply-nukkit-default-permissions", true));
/** /**
* If Nukkit attachment permissions are being applied. This setting is ignored on other platforms. * If Nukkit attachment permissions are being applied. This setting is ignored on other platforms.
*/ */
public static final ConfigKey<Boolean> APPLY_NUKKIT_ATTACHMENT_PERMISSIONS = enduringKey(booleanKey("apply-nukkit-attachment-permissions", true)); public static final ConfigKey<Boolean> APPLY_NUKKIT_ATTACHMENT_PERMISSIONS = notReloadable(booleanKey("apply-nukkit-attachment-permissions", true));
/** /**
* If BungeeCord configured permissions are being applied. This setting is ignored on other platforms. * If BungeeCord configured permissions are being applied. This setting is ignored on other platforms.
*/ */
public static final ConfigKey<Boolean> APPLY_BUNGEE_CONFIG_PERMISSIONS = enduringKey(booleanKey("apply-bungee-config-permissions", false)); public static final ConfigKey<Boolean> APPLY_BUNGEE_CONFIG_PERMISSIONS = notReloadable(booleanKey("apply-bungee-config-permissions", false));
/** /**
* If Sponge default subjects should be applied * If Sponge default subjects should be applied
*/ */
public static final ConfigKey<Boolean> APPLY_SPONGE_DEFAULT_SUBJECTS = enduringKey(booleanKey("apply-sponge-default-subjects", true)); public static final ConfigKey<Boolean> APPLY_SPONGE_DEFAULT_SUBJECTS = notReloadable(booleanKey("apply-sponge-default-subjects", true));
/** /**
* The algorithm LuckPerms should use when traversing the "inheritance tree" * The algorithm LuckPerms should use when traversing the "inheritance tree"
*/ */
public static final ConfigKey<TraversalAlgorithm> INHERITANCE_TRAVERSAL_ALGORITHM = customKey(c -> { public static final ConfigKey<TraversalAlgorithm> INHERITANCE_TRAVERSAL_ALGORITHM = key(c -> {
String value = c.getString("inheritance-traversal-algorithm", "depth-first-pre-order"); String value = c.getString("inheritance-traversal-algorithm", "depth-first-pre-order");
switch (value.toLowerCase()) { switch (value.toLowerCase()) {
case "breadth-first": case "breadth-first":
@ -299,7 +300,7 @@ public final class ConfigKeys {
/** /**
* The meta value selector * The meta value selector
*/ */
public static final ConfigKey<MetaValueSelector> META_VALUE_SELECTOR = customKey(c -> { public static final ConfigKey<MetaValueSelector> META_VALUE_SELECTOR = key(c -> {
SimpleMetaValueSelector.Strategy defaultStrategy = SimpleMetaValueSelector.Strategy.parse(c.getString("meta-value-selection-default", "inheritance")); 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<String, SimpleMetaValueSelector.Strategy> strategies = c.getStringMap("meta-value-selection", ImmutableMap.of()).entrySet().stream()
.map(e -> { .map(e -> {
@ -315,7 +316,7 @@ public final class ConfigKeys {
/** /**
* The configured group weightings * The configured group weightings
*/ */
public static final ConfigKey<Map<String, Integer>> GROUP_WEIGHTS = customKey(c -> { public static final ConfigKey<Map<String, Integer>> GROUP_WEIGHTS = key(c -> {
return c.getStringMap("group-weight", ImmutableMap.of()).entrySet().stream().collect(ImmutableCollectors.toMap( return c.getStringMap("group-weight", ImmutableMap.of()).entrySet().stream().collect(ImmutableCollectors.toMap(
e -> e.getKey().toLowerCase(), e -> e.getKey().toLowerCase(),
e -> { e -> {
@ -331,7 +332,7 @@ public final class ConfigKeys {
/** /**
* Creates a new prefix MetaStack element based upon the configured values. * Creates a new prefix MetaStack element based upon the configured values.
*/ */
public static final ConfigKey<MetaStackDefinition> PREFIX_FORMATTING_OPTIONS = customKey(l -> { public static final ConfigKey<MetaStackDefinition> PREFIX_FORMATTING_OPTIONS = key(l -> {
List<String> format = l.getStringList("meta-formatting.prefix.format", new ArrayList<>()); List<String> format = l.getStringList("meta-formatting.prefix.format", new ArrayList<>());
if (format.isEmpty()) { if (format.isEmpty()) {
format.add("highest"); format.add("highest");
@ -358,7 +359,7 @@ public final class ConfigKeys {
/** /**
* Creates a new suffix MetaStack element based upon the configured values. * Creates a new suffix MetaStack element based upon the configured values.
*/ */
public static final ConfigKey<MetaStackDefinition> SUFFIX_FORMATTING_OPTIONS = customKey(l -> { public static final ConfigKey<MetaStackDefinition> SUFFIX_FORMATTING_OPTIONS = key(l -> {
List<String> format = l.getStringList("meta-formatting.suffix.format", new ArrayList<>()); List<String> format = l.getStringList("meta-formatting.suffix.format", new ArrayList<>());
if (format.isEmpty()) { if (format.isEmpty()) {
format.add("highest"); format.add("highest");
@ -390,17 +391,17 @@ public final class ConfigKeys {
/** /**
* If auto op is enabled. Only used by the Bukkit platform. * If auto op is enabled. Only used by the Bukkit platform.
*/ */
public static final ConfigKey<Boolean> AUTO_OP = enduringKey(booleanKey("auto-op", false)); public static final ConfigKey<Boolean> AUTO_OP = notReloadable(booleanKey("auto-op", false));
/** /**
* If server operators should be enabled. Only used by the Bukkit platform. * If server operators should be enabled. Only used by the Bukkit platform.
*/ */
public static final ConfigKey<Boolean> OPS_ENABLED = enduringKey(customKey(c -> !AUTO_OP.get(c) && c.getBoolean("enable-ops", true))); public static final ConfigKey<Boolean> OPS_ENABLED = notReloadable(key(c -> !AUTO_OP.get(c) && c.getBoolean("enable-ops", true)));
/** /**
* If server operators should be able to use LuckPerms commands by default. Only used by the Bukkit platform. * If server operators should be able to use LuckPerms commands by default. Only used by the Bukkit platform.
*/ */
public static final ConfigKey<Boolean> COMMANDS_ALLOW_OP = enduringKey(booleanKey("commands-allow-op", true)); public static final ConfigKey<Boolean> COMMANDS_ALLOW_OP = notReloadable(booleanKey("commands-allow-op", true));
/** /**
* If Vault lookups for offline players on the main server thread should be enabled * If Vault lookups for offline players on the main server thread should be enabled
@ -425,7 +426,7 @@ public final class ConfigKeys {
/** /**
* The name of the server to use for Vault. * The name of the server to use for Vault.
*/ */
public static final ConfigKey<String> VAULT_SERVER = customKey(c -> { public static final ConfigKey<String> VAULT_SERVER = key(c -> {
// default to true for backwards compatibility // default to true for backwards compatibility
if (USE_VAULT_SERVER.get(c)) { if (USE_VAULT_SERVER.get(c)) {
return c.getString("vault-server", "global").toLowerCase(); return c.getString("vault-server", "global").toLowerCase();
@ -447,7 +448,7 @@ public final class ConfigKeys {
/** /**
* The world rewrites map * The world rewrites map
*/ */
public static final ConfigKey<Map<String, String>> WORLD_REWRITES = customKey(c -> { public static final ConfigKey<Map<String, String>> WORLD_REWRITES = key(c -> {
return c.getStringMap("world-rewrite", ImmutableMap.of()).entrySet().stream() return c.getStringMap("world-rewrite", ImmutableMap.of()).entrySet().stream()
.collect(ImmutableCollectors.toMap( .collect(ImmutableCollectors.toMap(
e -> e.getKey().toLowerCase(), e -> e.getKey().toLowerCase(),
@ -463,7 +464,7 @@ public final class ConfigKeys {
/** /**
* The database settings, username, password, etc for use by any database * The database settings, username, password, etc for use by any database
*/ */
public static final ConfigKey<StorageCredentials> DATABASE_VALUES = enduringKey(customKey(c -> { public static final ConfigKey<StorageCredentials> DATABASE_VALUES = notReloadable(key(c -> {
int maxPoolSize = c.getInteger("data.pool-settings.maximum-pool-size", c.getInteger("data.pool-size", 10)); int maxPoolSize = c.getInteger("data.pool-settings.maximum-pool-size", c.getInteger("data.pool-size", 10));
int minIdle = c.getInteger("data.pool-settings.minimum-idle", maxPoolSize); int minIdle = c.getInteger("data.pool-settings.minimum-idle", maxPoolSize);
int maxLifetime = c.getInteger("data.pool-settings.maximum-lifetime", 1800000); int maxLifetime = c.getInteger("data.pool-settings.maximum-lifetime", 1800000);
@ -482,28 +483,28 @@ public final class ConfigKeys {
/** /**
* The prefix for any SQL tables * The prefix for any SQL tables
*/ */
public static final ConfigKey<String> SQL_TABLE_PREFIX = enduringKey(customKey(c -> { public static final ConfigKey<String> SQL_TABLE_PREFIX = notReloadable(key(c -> {
return c.getString("data.table-prefix", c.getString("data.table_prefix", "luckperms_")); return c.getString("data.table-prefix", c.getString("data.table_prefix", "luckperms_"));
})); }));
/** /**
* The prefix for any MongoDB collections * The prefix for any MongoDB collections
*/ */
public static final ConfigKey<String> MONGODB_COLLECTION_PREFIX = enduringKey(customKey(c -> { public static final ConfigKey<String> MONGODB_COLLECTION_PREFIX = notReloadable(key(c -> {
return c.getString("data.mongodb-collection-prefix", c.getString("data.mongodb_collection_prefix", "")); return c.getString("data.mongodb-collection-prefix", c.getString("data.mongodb_collection_prefix", ""));
})); }));
/** /**
* MongoDB ClientConnectionURI to override default connection options * MongoDB ClientConnectionURI to override default connection options
*/ */
public static final ConfigKey<String> MONGODB_CONNECTION_URI = enduringKey(customKey(c -> { public static final ConfigKey<String> MONGODB_CONNECTION_URI = notReloadable(key(c -> {
return c.getString("data.mongodb-connection-uri", c.getString("data.mongodb_connection_URI", "")); return c.getString("data.mongodb-connection-uri", c.getString("data.mongodb_connection_URI", ""));
})); }));
/** /**
* The name of the storage method being used * The name of the storage method being used
*/ */
public static final ConfigKey<StorageType> STORAGE_METHOD = enduringKey(customKey(c -> { public static final ConfigKey<StorageType> STORAGE_METHOD = notReloadable(key(c -> {
return StorageType.parse(c.getString("storage-method", "h2"), StorageType.H2); return StorageType.parse(c.getString("storage-method", "h2"), StorageType.H2);
})); }));
@ -515,12 +516,12 @@ public final class ConfigKeys {
/** /**
* If split storage is being used * If split storage is being used
*/ */
public static final ConfigKey<Boolean> SPLIT_STORAGE = enduringKey(booleanKey("split-storage.enabled", false)); public static final ConfigKey<Boolean> SPLIT_STORAGE = notReloadable(booleanKey("split-storage.enabled", false));
/** /**
* The options for split storage * The options for split storage
*/ */
public static final ConfigKey<Map<SplitStorageType, StorageType>> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> { public static final ConfigKey<Map<SplitStorageType, StorageType>> SPLIT_STORAGE_OPTIONS = notReloadable(key(c -> {
EnumMap<SplitStorageType, StorageType> map = new EnumMap<>(SplitStorageType.class); EnumMap<SplitStorageType, StorageType> map = new EnumMap<>(SplitStorageType.class);
map.put(SplitStorageType.USER, StorageType.parse(c.getString("split-storage.methods.user", "h2"), StorageType.H2)); map.put(SplitStorageType.USER, StorageType.parse(c.getString("split-storage.methods.user", "h2"), StorageType.H2));
map.put(SplitStorageType.GROUP, StorageType.parse(c.getString("split-storage.methods.group", "h2"), StorageType.H2)); map.put(SplitStorageType.GROUP, StorageType.parse(c.getString("split-storage.methods.group", "h2"), StorageType.H2));
@ -533,42 +534,42 @@ public final class ConfigKeys {
/** /**
* The name of the messaging service in use, or "none" if not enabled * The name of the messaging service in use, or "none" if not enabled
*/ */
public static final ConfigKey<String> MESSAGING_SERVICE = enduringKey(lowercaseStringKey("messaging-service", "auto")); public static final ConfigKey<String> MESSAGING_SERVICE = notReloadable(lowercaseStringKey("messaging-service", "auto"));
/** /**
* If updates should be automatically pushed by the messaging service * If updates should be automatically pushed by the messaging service
*/ */
public static final ConfigKey<Boolean> AUTO_PUSH_UPDATES = enduringKey(booleanKey("auto-push-updates", true)); public static final ConfigKey<Boolean> AUTO_PUSH_UPDATES = notReloadable(booleanKey("auto-push-updates", true));
/** /**
* If LuckPerms should push logging entries to connected servers via the messaging service * If LuckPerms should push logging entries to connected servers via the messaging service
*/ */
public static final ConfigKey<Boolean> PUSH_LOG_ENTRIES = enduringKey(booleanKey("push-log-entries", true)); public static final ConfigKey<Boolean> PUSH_LOG_ENTRIES = notReloadable(booleanKey("push-log-entries", true));
/** /**
* If LuckPerms should broadcast received logging entries to players on this platform * If LuckPerms should broadcast received logging entries to players on this platform
*/ */
public static final ConfigKey<Boolean> BROADCAST_RECEIVED_LOG_ENTRIES = enduringKey(booleanKey("broadcast-received-log-entries", false)); public static final ConfigKey<Boolean> BROADCAST_RECEIVED_LOG_ENTRIES = notReloadable(booleanKey("broadcast-received-log-entries", false));
/** /**
* If redis messaging is enabled * If redis messaging is enabled
*/ */
public static final ConfigKey<Boolean> REDIS_ENABLED = enduringKey(booleanKey("redis.enabled", false)); public static final ConfigKey<Boolean> REDIS_ENABLED = notReloadable(booleanKey("redis.enabled", false));
/** /**
* The address of the redis server * The address of the redis server
*/ */
public static final ConfigKey<String> REDIS_ADDRESS = enduringKey(stringKey("redis.address", null)); public static final ConfigKey<String> REDIS_ADDRESS = notReloadable(stringKey("redis.address", null));
/** /**
* The password in use by the redis server, or an empty string if there is no password * The password in use by the redis server, or an empty string if there is no password
*/ */
public static final ConfigKey<String> REDIS_PASSWORD = enduringKey(stringKey("redis.password", "")); public static final ConfigKey<String> REDIS_PASSWORD = notReloadable(stringKey("redis.password", ""));
/** /**
* If the redis connection should use SSL * If the redis connection should use SSL
*/ */
public static final ConfigKey<Boolean> REDIS_SSL = enduringKey(booleanKey("redis.ssl", false)); public static final ConfigKey<Boolean> REDIS_SSL = notReloadable(booleanKey("redis.ssl", false));
/** /**
* The URL of the bytebin instance used to upload data * The URL of the bytebin instance used to upload data
@ -590,33 +591,12 @@ public final class ConfigKeys {
*/ */
public static final ConfigKey<String> TREE_VIEWER_URL_PATTERN = stringKey("tree-viewer-url", "https://luckperms.net/treeview/"); public static final ConfigKey<String> TREE_VIEWER_URL_PATTERN = stringKey("tree-viewer-url", "https://luckperms.net/treeview/");
private static final List<ConfigKeyTypes.BaseConfigKey<?>> KEYS;
static {
// get a list of all keys
KEYS = Arrays.stream(ConfigKeys.class.getFields())
.filter(f -> Modifier.isStatic(f.getModifiers()))
.filter(f -> ConfigKey.class.equals(f.getType()))
.map(f -> {
try {
return (ConfigKeyTypes.BaseConfigKey<?>) f.get(null);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
})
.collect(ImmutableCollectors.toList());
// set ordinal values
for (int i = 0; i < KEYS.size(); i++) {
KEYS.get(i).ordinal = i;
}
}
/** /**
* Gets a list of the keys defined in this class. * A list of the keys defined in this class.
*
* @return the defined keys
*/ */
private static final List<SimpleConfigKey<?>> KEYS = KeyedConfiguration.initialise(ConfigKeys.class);
public static List<? extends ConfigKey<?>> getKeys() { public static List<? extends ConfigKey<?>> getKeys() {
return KEYS; return KEYS;
} }

View File

@ -25,44 +25,39 @@
package me.lucko.luckperms.common.config; package me.lucko.luckperms.common.config;
import me.lucko.luckperms.common.config.generic.KeyedConfiguration;
import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
/** public class LuckPermsConfiguration extends KeyedConfiguration {
* The master configuration used by LuckPerms. private final LuckPermsPlugin plugin;
*/ private final ContextsFile contextsFile;
public interface LuckPermsConfiguration {
/** public LuckPermsConfiguration(LuckPermsPlugin plugin, ConfigurationAdapter adapter) {
* Gets the main plugin instance. super(adapter, ConfigKeys.getKeys());
* this.plugin = plugin;
* @return the plugin instance this.contextsFile = new ContextsFile(this);
*/
LuckPermsPlugin getPlugin();
/** init();
* Gets the object which wraps the 'contexts.json' file. }
*
* @return the contexts file wrapper object
*/
ContextsFile getContextsFile();
/** @Override
* Reloads the configuration. protected void load(boolean initial) {
*/ super.load(initial);
void reload(); this.contextsFile.load();
}
/** @Override
* Loads all configuration values. public void reload() {
*/ super.reload();
void load(); getPlugin().getEventDispatcher().dispatchConfigReload();
}
/** public ContextsFile getContextsFile() {
* Gets the value of a given context key. return this.contextsFile;
* }
* @param key the key
* @param <T> the key return type
* @return the value mapped to the given key. May be null.
*/
<T> T get(ConfigKey<T> key);
public LuckPermsPlugin getPlugin() {
return this.plugin;
}
} }

View File

@ -0,0 +1,123 @@
/*
* 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.config.generic;
import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.config.generic.key.ConfigKey;
import me.lucko.luckperms.common.config.generic.key.SimpleConfigKey;
import me.lucko.luckperms.common.util.ImmutableCollectors;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
public class KeyedConfiguration {
private final ConfigurationAdapter adapter;
private final List<? extends ConfigKey<?>> keys;
private final ValuesMap values;
public KeyedConfiguration(ConfigurationAdapter adapter, List<? extends ConfigKey<?>> keys) {
this.adapter = adapter;
this.keys = keys;
this.values = new ValuesMap(keys.size());
}
protected void init() {
load(true);
}
/**
* Gets the value of a given context key.
*
* @param key the key
* @param <T> the key return type
* @return the value mapped to the given key. May be null.
*/
public <T> T get(ConfigKey<T> key) {
return this.values.get(key);
}
protected void load(boolean initial) {
for (ConfigKey<?> key : this.keys) {
if (initial || key.reloadable()) {
this.values.put(key, key.get(this.adapter));
}
}
}
/**
* Reloads the configuration.
*/
public void reload() {
this.adapter.reload();
load(false);
}
/**
* Initialises the given pseudo-enum keys class.
*
* @param keysClass the keys class
* @return the list of keys defined by the class with their ordinal values set
*/
public static List<SimpleConfigKey<?>> initialise(Class<?> keysClass) {
// get a list of all keys
List<SimpleConfigKey<?>> keys = Arrays.stream(keysClass.getFields())
.filter(f -> Modifier.isStatic(f.getModifiers()))
.filter(f -> ConfigKey.class.equals(f.getType()))
.map(f -> {
try {
return (SimpleConfigKey<?>) f.get(null);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
})
.collect(ImmutableCollectors.toList());
// set ordinal values
for (int i = 0; i < keys.size(); i++) {
keys.get(i).setOrdinal(i);
}
return keys;
}
public static class ValuesMap {
private final Object[] values;
public ValuesMap(int size) {
this.values = new Object[size];
}
@SuppressWarnings("unchecked")
public <T> T get(ConfigKey<T> key) {
return (T) this.values[key.ordinal()];
}
public void put(ConfigKey<?> key, Object value) {
this.values[key.ordinal()] = value;
}
}
}

View File

@ -23,7 +23,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
package me.lucko.luckperms.common.config.adapter; package me.lucko.luckperms.common.config.generic.adapter;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;

View File

@ -23,7 +23,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
package me.lucko.luckperms.common.config.adapter; package me.lucko.luckperms.common.config.generic.adapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;

View File

@ -23,9 +23,9 @@
* SOFTWARE. * SOFTWARE.
*/ */
package me.lucko.luckperms.common.config; package me.lucko.luckperms.common.config.generic.key;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
/** /**
* Represents a key in the configuration. * Represents a key in the configuration.
@ -35,20 +35,21 @@ import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter;
public interface ConfigKey<T> { public interface ConfigKey<T> {
/** /**
* Gets the position of this key within the {@link ConfigKeys} enum. * Gets the position of this key within the keys enum.
*
* <p>This is set shortly after the key is created, during the initialisation
* of {@link ConfigKeys}.</p>
* *
* @return the position * @return the position
*/ */
int ordinal(); int ordinal();
/** /**
* Resolves and returns the value mapped to this key using the given config instance. * Gets if the config key can be reloaded.
* *
* <p>The {@link LuckPermsConfiguration#get(ConfigKey)} method should be used to * @return the if the key can be reloaded
* retrieve the value, as opposed to calling this directly.</p> */
boolean reloadable();
/**
* Resolves and returns the value mapped to this key using the given config instance.
* *
* @param adapter the config adapter instance * @param adapter the config adapter instance
* @return the value mapped to this key * @return the value mapped to this key

View File

@ -0,0 +1,99 @@
/*
* 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.config.generic.key;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import java.util.Map;
import java.util.function.Function;
public interface ConfigKeyFactory<T> {
ConfigKeyFactory<Boolean> BOOLEAN = ConfigurationAdapter::getBoolean;
ConfigKeyFactory<String> STRING = ConfigurationAdapter::getString;
ConfigKeyFactory<String> LOWERCASE_STRING = (adapter, path, def) -> adapter.getString(path, def).toLowerCase();
ConfigKeyFactory<Map<String, String>> STRING_MAP = (config, path, def) -> ImmutableMap.copyOf(config.getStringMap(path, ImmutableMap.of()));
static <T> SimpleConfigKey<T> key(Function<ConfigurationAdapter, T> function) {
return new SimpleConfigKey<>(function);
}
static <T> SimpleConfigKey<T> notReloadable(SimpleConfigKey<T> key) {
key.setReloadable(false);
return key;
}
static SimpleConfigKey<Boolean> booleanKey(String path, boolean def) {
return key(new Bound<>(BOOLEAN, path, def));
}
static SimpleConfigKey<String> stringKey(String path, String def) {
return key(new Bound<>(STRING, path, def));
}
static SimpleConfigKey<String> lowercaseStringKey(String path, String def) {
return key(new Bound<>(LOWERCASE_STRING, path, def));
}
static SimpleConfigKey<Map<String, String>> mapKey(String path) {
return key(new Bound<>(STRING_MAP, path, null));
}
/**
* Extracts the value from the config.
*
* @param config the config
* @param path the path where the value is
* @param def the default value
* @return the value
*/
T getValue(ConfigurationAdapter config, String path, T def);
/**
* A {@link ConfigKeyFactory} bound to a given {@code path}.
*
* @param <T> the value type
*/
class Bound<T> implements Function<ConfigurationAdapter, T> {
private final ConfigKeyFactory<T> factory;
private final String path;
private final T def;
Bound(ConfigKeyFactory<T> factory, String path, T def) {
this.factory = factory;
this.path = path;
this.def = def;
}
@Override
public T apply(ConfigurationAdapter adapter) {
return this.factory.getValue(adapter, this.path, this.def);
}
}
}

View File

@ -0,0 +1,69 @@
/*
* 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.config.generic.key;
import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import java.util.function.Function;
/**
* Basic {@link ConfigKey} implementation.
*
* @param <T> the value type
*/
public class SimpleConfigKey<T> implements ConfigKey<T> {
private final Function<? super ConfigurationAdapter, ? extends T> function;
private int ordinal = -1;
private boolean reloadable = true;
SimpleConfigKey(Function<? super ConfigurationAdapter, ? extends T> function) {
this.function = function;
}
@Override
public T get(ConfigurationAdapter adapter) {
return this.function.apply(adapter);
}
@Override
public int ordinal() {
return this.ordinal;
}
@Override
public boolean reloadable() {
return this.reloadable;
}
public void setOrdinal(int ordinal) {
this.ordinal = ordinal;
}
public void setReloadable(boolean reloadable) {
this.reloadable = reloadable;
}
}

View File

@ -29,10 +29,9 @@ import me.lucko.luckperms.common.actionlog.LogDispatcher;
import me.lucko.luckperms.common.api.ApiRegistrationUtil; import me.lucko.luckperms.common.api.ApiRegistrationUtil;
import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.api.LuckPermsApiProvider;
import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.config.AbstractConfiguration;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.config.LuckPermsConfiguration;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.context.LPStaticContextsCalculator; import me.lucko.luckperms.common.context.LPStaticContextsCalculator;
import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.dependencies.DependencyManager; import me.lucko.luckperms.common.dependencies.DependencyManager;
@ -113,7 +112,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
// load configuration // load configuration
getLogger().info("Loading configuration..."); getLogger().info("Loading configuration...");
this.configuration = new AbstractConfiguration(this, provideConfigurationAdapter()); this.configuration = new LuckPermsConfiguration(this, provideConfigurationAdapter());
// load locale // load locale
this.localeManager = new LocaleManager(); this.localeManager = new LocaleManager();

View File

@ -30,7 +30,7 @@ import me.lucko.luckperms.common.api.implementation.ApiUser;
import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.command.access.CommandPermission; import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.event.AbstractEventBus; import me.lucko.luckperms.common.event.AbstractEventBus;
import me.lucko.luckperms.common.messaging.MessagingFactory; import me.lucko.luckperms.common.messaging.MessagingFactory;
import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.model.User;

View File

@ -25,7 +25,7 @@
package me.lucko.luckperms.nukkit; package me.lucko.luckperms.nukkit;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import cn.nukkit.utils.Config; import cn.nukkit.utils.Config;

View File

@ -29,7 +29,7 @@ import me.lucko.luckperms.common.api.LuckPermsApiProvider;
import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.command.abstraction.Command; import me.lucko.luckperms.common.command.abstraction.Command;
import me.lucko.luckperms.common.command.access.CommandPermission; import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus; import me.lucko.luckperms.common.event.AbstractEventBus;
import me.lucko.luckperms.common.messaging.MessagingFactory; import me.lucko.luckperms.common.messaging.MessagingFactory;

View File

@ -25,8 +25,8 @@
package me.lucko.luckperms.sponge; package me.lucko.luckperms.sponge;
import me.lucko.luckperms.common.config.adapter.ConfigurateConfigAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurateConfigAdapter;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.ConfigurationNode;

View File

@ -28,7 +28,7 @@ package me.lucko.luckperms.velocity;
import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.api.LuckPermsApiProvider;
import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.command.CommandManager; import me.lucko.luckperms.common.command.CommandManager;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus; import me.lucko.luckperms.common.event.AbstractEventBus;
import me.lucko.luckperms.common.messaging.MessagingFactory; import me.lucko.luckperms.common.messaging.MessagingFactory;

View File

@ -25,8 +25,8 @@
package me.lucko.luckperms.velocity; package me.lucko.luckperms.velocity;
import me.lucko.luckperms.common.config.adapter.ConfigurateConfigAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurateConfigAdapter;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.ConfigurationNode;