From 5c0d82f306c7400702494da7f95a135ecbd69eeb Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 25 May 2020 10:42:32 +0100 Subject: [PATCH] Refactor config handler classes --- .../luckperms/bukkit/BukkitConfigAdapter.java | 2 +- .../luckperms/bukkit/LPBukkitPlugin.java | 2 +- .../luckperms/bungee/BungeeConfigAdapter.java | 2 +- .../luckperms/bungee/LPBungeePlugin.java | 2 +- .../command/abstraction/CommandException.java | 2 +- .../common/command/utils/ArgumentParser.java | 6 +- .../common/config/AbstractConfiguration.java | 108 -------------- .../common/config/ConfigKeyTypes.java | 136 ------------------ .../luckperms/common/config/ConfigKeys.java | 132 ++++++++--------- .../common/config/LuckPermsConfiguration.java | 59 ++++---- .../config/generic/KeyedConfiguration.java | 123 ++++++++++++++++ .../adapter/ConfigurateConfigAdapter.java | 2 +- .../adapter/ConfigurationAdapter.java | 2 +- .../config/{ => generic/key}/ConfigKey.java | 19 +-- .../config/generic/key/ConfigKeyFactory.java | 99 +++++++++++++ .../config/generic/key/SimpleConfigKey.java | 69 +++++++++ .../plugin/AbstractLuckPermsPlugin.java | 5 +- .../luckperms/nukkit/LPNukkitPlugin.java | 2 +- .../luckperms/nukkit/NukkitConfigAdapter.java | 2 +- .../luckperms/sponge/LPSpongePlugin.java | 2 +- .../luckperms/sponge/SpongeConfigAdapter.java | 4 +- .../luckperms/velocity/LPVelocityPlugin.java | 2 +- .../velocity/VelocityConfigAdapter.java | 4 +- 23 files changed, 404 insertions(+), 382 deletions(-) delete mode 100644 common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java delete mode 100644 common/src/main/java/me/lucko/luckperms/common/config/ConfigKeyTypes.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/config/generic/KeyedConfiguration.java rename common/src/main/java/me/lucko/luckperms/common/config/{ => generic}/adapter/ConfigurateConfigAdapter.java (98%) rename common/src/main/java/me/lucko/luckperms/common/config/{ => generic}/adapter/ConfigurationAdapter.java (96%) rename common/src/main/java/me/lucko/luckperms/common/config/{ => generic/key}/ConfigKey.java (78%) create mode 100644 common/src/main/java/me/lucko/luckperms/common/config/generic/key/ConfigKeyFactory.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/config/generic/key/SimpleConfigKey.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java index eb0c8cbc4..5bf43c44b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java @@ -25,7 +25,7 @@ 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 org.bukkit.configuration.ConfigurationSection; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index be7ea9010..d9b48700b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -48,7 +48,7 @@ import me.lucko.luckperms.common.api.implementation.ApiUser; import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.command.access.CommandPermission; 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.event.AbstractEventBus; import me.lucko.luckperms.common.messaging.MessagingFactory; diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java index a0c8189c1..edfae1728 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java @@ -25,7 +25,7 @@ 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 net.md_5.bungee.config.Configuration; diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java index 7168d805f..49baa86a8 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -35,7 +35,7 @@ import me.lucko.luckperms.bungee.messaging.BungeeMessagingFactory; import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.calculator.CalculatorFactory; 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.event.AbstractEventBus; import me.lucko.luckperms.common.messaging.MessagingFactory; diff --git a/common/src/main/java/me/lucko/luckperms/common/command/abstraction/CommandException.java b/common/src/main/java/me/lucko/luckperms/common/command/abstraction/CommandException.java index a658e8abb..99dd052b7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/command/abstraction/CommandException.java +++ b/common/src/main/java/me/lucko/luckperms/common/command/abstraction/CommandException.java @@ -33,7 +33,7 @@ import me.lucko.luckperms.common.sender.Sender; */ 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) { return handle(sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java b/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java index e44b5c11b..091dd787e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java +++ b/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java @@ -221,7 +221,7 @@ public class ArgumentParser { public static class DetailedUsageException extends ArgumentException { @Override - public CommandResult handle(Sender sender) { + protected CommandResult handle(Sender sender) { throw new UnsupportedOperationException(); } @@ -240,7 +240,7 @@ public class ArgumentParser { public static class PastDateException extends ArgumentException { @Override - public CommandResult handle(Sender sender) { + protected CommandResult handle(Sender sender) { Message.PAST_DATE_ERROR.send(sender); return CommandResult.INVALID_ARGS; } @@ -254,7 +254,7 @@ public class ArgumentParser { } @Override - public CommandResult handle(Sender sender) { + protected CommandResult handle(Sender sender) { Message.ILLEGAL_DATE_ERROR.send(sender, this.invalidDate); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java deleted file mode 100644 index 069c7abcb..000000000 --- a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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}. - * - *

Values are loaded into memory on init.

- */ -public class AbstractConfiguration implements LuckPermsConfiguration { - - /** - * The configurations loaded values. - * - *

The value corresponding to each key is stored at the index defined - * by {@link ConfigKey#ordinal()}.

- */ - 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 get(ConfigKey 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; - } -} diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeyTypes.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeyTypes.java deleted file mode 100644 index d6b174592..000000000 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeyTypes.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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 = ConfigurationAdapter::getBoolean; - private static final KeyFactory STRING = ConfigurationAdapter::getString; - private static final KeyFactory LOWERCASE_STRING = (adapter, path, def) -> adapter.getString(path, def).toLowerCase(); - private static final KeyFactory> STRING_MAP = (config, path, def) -> ImmutableMap.copyOf(config.getStringMap(path, ImmutableMap.of())); - - public static BaseConfigKey booleanKey(String path, boolean def) { - return BOOLEAN.createKey(path, def); - } - - public static BaseConfigKey stringKey(String path, String def) { - return STRING.createKey(path, def); - } - - public static BaseConfigKey lowercaseStringKey(String path, String def) { - return LOWERCASE_STRING.createKey(path, def); - } - - public static BaseConfigKey> mapKey(String path) { - return STRING_MAP.createKey(path, null); - } - - public static CustomKey customKey(Function function) { - return new CustomKey<>(function); - } - - public static EnduringKey enduringKey(ConfigKey delegate) { - return new EnduringKey<>(delegate); - } - - /** - * Functional interface that extracts values from a {@link ConfigurationAdapter}. - * - * @param the value type. - */ - @FunctionalInterface - public interface KeyFactory { - T getValue(ConfigurationAdapter config, String path, T def); - - default BaseConfigKey createKey(String path, T def) { - return new FunctionalKey<>(this, path, def); - } - } - - public abstract static class BaseConfigKey implements ConfigKey { - int ordinal = -1; - - BaseConfigKey() { - - } - - @Override - public int ordinal() { - return this.ordinal; - } - } - - private static class FunctionalKey extends BaseConfigKey implements ConfigKey { - private final KeyFactory factory; - private final String path; - private final T def; - - FunctionalKey(KeyFactory 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 extends BaseConfigKey { - private final Function function; - - private CustomKey(Function function) { - this.function = function; - } - - @Override - public T get(ConfigurationAdapter adapter) { - return this.function.apply(adapter); - } - } - - public static class EnduringKey extends BaseConfigKey { - private final ConfigKey delegate; - - private EnduringKey(ConfigKey delegate) { - this.delegate = delegate; - } - - @Override - public T get(ConfigurationAdapter adapter) { - return this.delegate.get(adapter); - } - } - -} diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index 98021b866..de97b12bd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -30,6 +30,9 @@ 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.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.metastacking.SimpleMetaStackDefinition; 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.meta.MetaValueSelector; -import java.lang.reflect.Modifier; import java.util.ArrayList; -import java.util.Arrays; import java.util.EnumMap; import java.util.EnumSet; import java.util.List; @@ -62,12 +63,12 @@ import java.util.Objects; import java.util.Set; import java.util.function.Function; -import static me.lucko.luckperms.common.config.ConfigKeyTypes.booleanKey; -import static me.lucko.luckperms.common.config.ConfigKeyTypes.customKey; -import static me.lucko.luckperms.common.config.ConfigKeyTypes.enduringKey; -import static me.lucko.luckperms.common.config.ConfigKeyTypes.lowercaseStringKey; -import static me.lucko.luckperms.common.config.ConfigKeyTypes.mapKey; -import static me.lucko.luckperms.common.config.ConfigKeyTypes.stringKey; +import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.booleanKey; +import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.key; +import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.lowercaseStringKey; +import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.mapKey; +import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.notReloadable; +import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.stringKey; /** * 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. */ - public static final ConfigKey SYNC_TIME = enduringKey(customKey(c -> { + public static final ConfigKey SYNC_TIME = notReloadable(key(c -> { int val = c.getInteger("sync-minutes", -1); if (val == -1) { val = c.getInteger("data.sync-minutes", -1); @@ -97,7 +98,7 @@ public final class ConfigKeys { /** * The default global contexts instance */ - public static final ConfigKey GLOBAL_QUERY_OPTIONS = customKey(c -> { + public static final ConfigKey GLOBAL_QUERY_OPTIONS = key(c -> { Set flags = EnumSet.of(Flag.RESOLVE_INHERITANCE); if (c.getBoolean("include-global", true)) { flags.add(Flag.INCLUDE_NODES_WITHOUT_SERVER_CONTEXT); @@ -118,7 +119,7 @@ public final class ConfigKeys { /** * The default contexts satisfy mode */ - public static final ConfigKey CONTEXT_SATISFY_MODE = customKey(c -> { + public static final ConfigKey CONTEXT_SATISFY_MODE = key(c -> { String value = c.getString("context-satisfy-mode", "at-least-one-value-per-key"); if (value.toLowerCase().equals("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. */ - public static final ConfigKey UPDATE_CLIENT_COMMAND_LIST = enduringKey(booleanKey("update-client-command-list", true)); + public static final ConfigKey UPDATE_CLIENT_COMMAND_LIST = notReloadable(booleanKey("update-client-command-list", true)); /** * 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 */ - public static final ConfigKey TEMPORARY_ADD_BEHAVIOUR = customKey(c -> { + public static final ConfigKey TEMPORARY_ADD_BEHAVIOUR = key(c -> { String option = c.getString("temporary-add-behaviour", "deny").toLowerCase(); if (!option.equals("deny") && !option.equals("replace") && !option.equals("accumulate")) { option = "deny"; @@ -171,7 +172,7 @@ public final class ConfigKeys { /** * How primary groups should be calculated. */ - public static final ConfigKey PRIMARY_GROUP_CALCULATION_METHOD = enduringKey(customKey(c -> { + public static final ConfigKey PRIMARY_GROUP_CALCULATION_METHOD = notReloadable(key(c -> { String option = c.getString("primary-group-calculation", "stored").toLowerCase(); if (!option.equals("stored") && !option.equals("parents-by-weight") && !option.equals("all-parents-by-weight")) { 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. */ - public static final ConfigKey> PRIMARY_GROUP_CALCULATION = enduringKey(customKey(c -> { + public static final ConfigKey> PRIMARY_GROUP_CALCULATION = notReloadable(key(c -> { String option = PRIMARY_GROUP_CALCULATION_METHOD.get(c); switch (option) { case "stored": @@ -215,12 +216,12 @@ public final class ConfigKeys { /** * If wildcards are being applied */ - public static final ConfigKey APPLYING_WILDCARDS = enduringKey(booleanKey("apply-wildcards", true)); + public static final ConfigKey APPLYING_WILDCARDS = notReloadable(booleanKey("apply-wildcards", true)); /** * If Sponge's implicit permission inheritance system should be applied */ - public static final ConfigKey APPLYING_WILDCARDS_SPONGE = enduringKey(customKey(c -> { + public static final ConfigKey APPLYING_WILDCARDS_SPONGE = notReloadable(key(c -> { boolean def = c.getPlugin().getBootstrap().getType() == Platform.Type.SPONGE; return c.getBoolean("apply-sponge-implicit-wildcards", def); })); @@ -228,57 +229,57 @@ public final class ConfigKeys { /** * If regex permissions are being applied */ - public static final ConfigKey APPLYING_REGEX = enduringKey(booleanKey("apply-regex", true)); + public static final ConfigKey APPLYING_REGEX = notReloadable(booleanKey("apply-regex", true)); /** * If shorthand permissions are being applied */ - public static final ConfigKey APPLYING_SHORTHAND = enduringKey(booleanKey("apply-shorthand", true)); + public static final ConfigKey APPLYING_SHORTHAND = notReloadable(booleanKey("apply-shorthand", true)); /** * If Bukkit child permissions are being applied. This setting is ignored on other platforms. */ - public static final ConfigKey APPLY_BUKKIT_CHILD_PERMISSIONS = enduringKey(booleanKey("apply-bukkit-child-permissions", true)); + public static final ConfigKey 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. */ - public static final ConfigKey APPLY_BUKKIT_DEFAULT_PERMISSIONS = enduringKey(booleanKey("apply-bukkit-default-permissions", true)); + public static final ConfigKey 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. */ - public static final ConfigKey APPLY_BUKKIT_ATTACHMENT_PERMISSIONS = enduringKey(booleanKey("apply-bukkit-attachment-permissions", true)); + public static final ConfigKey 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. */ - public static final ConfigKey APPLY_NUKKIT_CHILD_PERMISSIONS = enduringKey(booleanKey("apply-nukkit-child-permissions", true)); + public static final ConfigKey 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. */ - public static final ConfigKey APPLY_NUKKIT_DEFAULT_PERMISSIONS = enduringKey(booleanKey("apply-nukkit-default-permissions", true)); + public static final ConfigKey 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. */ - public static final ConfigKey APPLY_NUKKIT_ATTACHMENT_PERMISSIONS = enduringKey(booleanKey("apply-nukkit-attachment-permissions", true)); + public static final ConfigKey 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. */ - public static final ConfigKey APPLY_BUNGEE_CONFIG_PERMISSIONS = enduringKey(booleanKey("apply-bungee-config-permissions", false)); + public static final ConfigKey APPLY_BUNGEE_CONFIG_PERMISSIONS = notReloadable(booleanKey("apply-bungee-config-permissions", false)); /** * If Sponge default subjects should be applied */ - public static final ConfigKey APPLY_SPONGE_DEFAULT_SUBJECTS = enduringKey(booleanKey("apply-sponge-default-subjects", true)); + public static final ConfigKey APPLY_SPONGE_DEFAULT_SUBJECTS = notReloadable(booleanKey("apply-sponge-default-subjects", true)); /** * The algorithm LuckPerms should use when traversing the "inheritance tree" */ - public static final ConfigKey INHERITANCE_TRAVERSAL_ALGORITHM = customKey(c -> { + public static final ConfigKey INHERITANCE_TRAVERSAL_ALGORITHM = key(c -> { String value = c.getString("inheritance-traversal-algorithm", "depth-first-pre-order"); switch (value.toLowerCase()) { case "breadth-first": @@ -299,7 +300,7 @@ public final class ConfigKeys { /** * The meta value selector */ - public static final ConfigKey META_VALUE_SELECTOR = customKey(c -> { + public static final ConfigKey META_VALUE_SELECTOR = key(c -> { SimpleMetaValueSelector.Strategy defaultStrategy = SimpleMetaValueSelector.Strategy.parse(c.getString("meta-value-selection-default", "inheritance")); Map strategies = c.getStringMap("meta-value-selection", ImmutableMap.of()).entrySet().stream() .map(e -> { @@ -315,7 +316,7 @@ public final class ConfigKeys { /** * The configured group weightings */ - public static final ConfigKey> GROUP_WEIGHTS = customKey(c -> { + public static final ConfigKey> GROUP_WEIGHTS = key(c -> { return c.getStringMap("group-weight", ImmutableMap.of()).entrySet().stream().collect(ImmutableCollectors.toMap( e -> e.getKey().toLowerCase(), e -> { @@ -331,7 +332,7 @@ public final class ConfigKeys { /** * Creates a new prefix MetaStack element based upon the configured values. */ - public static final ConfigKey PREFIX_FORMATTING_OPTIONS = customKey(l -> { + public static final ConfigKey PREFIX_FORMATTING_OPTIONS = key(l -> { List format = l.getStringList("meta-formatting.prefix.format", new ArrayList<>()); if (format.isEmpty()) { format.add("highest"); @@ -358,7 +359,7 @@ public final class ConfigKeys { /** * Creates a new suffix MetaStack element based upon the configured values. */ - public static final ConfigKey SUFFIX_FORMATTING_OPTIONS = customKey(l -> { + public static final ConfigKey SUFFIX_FORMATTING_OPTIONS = key(l -> { List format = l.getStringList("meta-formatting.suffix.format", new ArrayList<>()); if (format.isEmpty()) { format.add("highest"); @@ -390,17 +391,17 @@ public final class ConfigKeys { /** * If auto op is enabled. Only used by the Bukkit platform. */ - public static final ConfigKey AUTO_OP = enduringKey(booleanKey("auto-op", false)); + public static final ConfigKey AUTO_OP = notReloadable(booleanKey("auto-op", false)); /** * If server operators should be enabled. Only used by the Bukkit platform. */ - public static final ConfigKey OPS_ENABLED = enduringKey(customKey(c -> !AUTO_OP.get(c) && c.getBoolean("enable-ops", true))); + public static final ConfigKey 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. */ - public static final ConfigKey COMMANDS_ALLOW_OP = enduringKey(booleanKey("commands-allow-op", true)); + public static final ConfigKey COMMANDS_ALLOW_OP = notReloadable(booleanKey("commands-allow-op", true)); /** * 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. */ - public static final ConfigKey VAULT_SERVER = customKey(c -> { + public static final ConfigKey VAULT_SERVER = key(c -> { // default to true for backwards compatibility if (USE_VAULT_SERVER.get(c)) { return c.getString("vault-server", "global").toLowerCase(); @@ -447,7 +448,7 @@ public final class ConfigKeys { /** * The world rewrites map */ - public static final ConfigKey> WORLD_REWRITES = customKey(c -> { + public static final ConfigKey> WORLD_REWRITES = key(c -> { return c.getStringMap("world-rewrite", ImmutableMap.of()).entrySet().stream() .collect(ImmutableCollectors.toMap( e -> e.getKey().toLowerCase(), @@ -463,7 +464,7 @@ public final class ConfigKeys { /** * The database settings, username, password, etc for use by any database */ - public static final ConfigKey DATABASE_VALUES = enduringKey(customKey(c -> { + public static final ConfigKey DATABASE_VALUES = notReloadable(key(c -> { 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 maxLifetime = c.getInteger("data.pool-settings.maximum-lifetime", 1800000); @@ -482,28 +483,28 @@ public final class ConfigKeys { /** * The prefix for any SQL tables */ - public static final ConfigKey SQL_TABLE_PREFIX = enduringKey(customKey(c -> { + public static final ConfigKey SQL_TABLE_PREFIX = notReloadable(key(c -> { return c.getString("data.table-prefix", c.getString("data.table_prefix", "luckperms_")); })); /** * The prefix for any MongoDB collections */ - public static final ConfigKey MONGODB_COLLECTION_PREFIX = enduringKey(customKey(c -> { + public static final ConfigKey MONGODB_COLLECTION_PREFIX = notReloadable(key(c -> { return c.getString("data.mongodb-collection-prefix", c.getString("data.mongodb_collection_prefix", "")); })); /** * MongoDB ClientConnectionURI to override default connection options */ - public static final ConfigKey MONGODB_CONNECTION_URI = enduringKey(customKey(c -> { + public static final ConfigKey MONGODB_CONNECTION_URI = notReloadable(key(c -> { return c.getString("data.mongodb-connection-uri", c.getString("data.mongodb_connection_URI", "")); })); /** * The name of the storage method being used */ - public static final ConfigKey STORAGE_METHOD = enduringKey(customKey(c -> { + public static final ConfigKey STORAGE_METHOD = notReloadable(key(c -> { return StorageType.parse(c.getString("storage-method", "h2"), StorageType.H2); })); @@ -515,12 +516,12 @@ public final class ConfigKeys { /** * If split storage is being used */ - public static final ConfigKey SPLIT_STORAGE = enduringKey(booleanKey("split-storage.enabled", false)); + public static final ConfigKey SPLIT_STORAGE = notReloadable(booleanKey("split-storage.enabled", false)); /** * The options for split storage */ - public static final ConfigKey> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> { + public static final ConfigKey> SPLIT_STORAGE_OPTIONS = notReloadable(key(c -> { EnumMap map = new EnumMap<>(SplitStorageType.class); 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)); @@ -533,42 +534,42 @@ public final class ConfigKeys { /** * The name of the messaging service in use, or "none" if not enabled */ - public static final ConfigKey MESSAGING_SERVICE = enduringKey(lowercaseStringKey("messaging-service", "auto")); + public static final ConfigKey MESSAGING_SERVICE = notReloadable(lowercaseStringKey("messaging-service", "auto")); /** * If updates should be automatically pushed by the messaging service */ - public static final ConfigKey AUTO_PUSH_UPDATES = enduringKey(booleanKey("auto-push-updates", true)); + public static final ConfigKey AUTO_PUSH_UPDATES = notReloadable(booleanKey("auto-push-updates", true)); /** * If LuckPerms should push logging entries to connected servers via the messaging service */ - public static final ConfigKey PUSH_LOG_ENTRIES = enduringKey(booleanKey("push-log-entries", true)); + public static final ConfigKey PUSH_LOG_ENTRIES = notReloadable(booleanKey("push-log-entries", true)); /** * If LuckPerms should broadcast received logging entries to players on this platform */ - public static final ConfigKey BROADCAST_RECEIVED_LOG_ENTRIES = enduringKey(booleanKey("broadcast-received-log-entries", false)); + public static final ConfigKey BROADCAST_RECEIVED_LOG_ENTRIES = notReloadable(booleanKey("broadcast-received-log-entries", false)); /** * If redis messaging is enabled */ - public static final ConfigKey REDIS_ENABLED = enduringKey(booleanKey("redis.enabled", false)); + public static final ConfigKey REDIS_ENABLED = notReloadable(booleanKey("redis.enabled", false)); /** * The address of the redis server */ - public static final ConfigKey REDIS_ADDRESS = enduringKey(stringKey("redis.address", null)); + public static final ConfigKey REDIS_ADDRESS = notReloadable(stringKey("redis.address", null)); /** * The password in use by the redis server, or an empty string if there is no password */ - public static final ConfigKey REDIS_PASSWORD = enduringKey(stringKey("redis.password", "")); + public static final ConfigKey REDIS_PASSWORD = notReloadable(stringKey("redis.password", "")); /** * If the redis connection should use SSL */ - public static final ConfigKey REDIS_SSL = enduringKey(booleanKey("redis.ssl", false)); + public static final ConfigKey REDIS_SSL = notReloadable(booleanKey("redis.ssl", false)); /** * The URL of the bytebin instance used to upload data @@ -590,33 +591,12 @@ public final class ConfigKeys { */ public static final ConfigKey TREE_VIEWER_URL_PATTERN = stringKey("tree-viewer-url", "https://luckperms.net/treeview/"); - private static final List> 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. - * - * @return the defined keys + * A list of the keys defined in this class. */ + private static final List> KEYS = KeyedConfiguration.initialise(ConfigKeys.class); + public static List> getKeys() { return KEYS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java index c72bd80f4..f2480cf96 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java @@ -25,44 +25,39 @@ 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; -/** - * The master configuration used by LuckPerms. - */ -public interface LuckPermsConfiguration { +public class LuckPermsConfiguration extends KeyedConfiguration { + private final LuckPermsPlugin plugin; + private final ContextsFile contextsFile; - /** - * Gets the main plugin instance. - * - * @return the plugin instance - */ - LuckPermsPlugin getPlugin(); + public LuckPermsConfiguration(LuckPermsPlugin plugin, ConfigurationAdapter adapter) { + super(adapter, ConfigKeys.getKeys()); + this.plugin = plugin; + this.contextsFile = new ContextsFile(this); - /** - * Gets the object which wraps the 'contexts.json' file. - * - * @return the contexts file wrapper object - */ - ContextsFile getContextsFile(); + init(); + } - /** - * Reloads the configuration. - */ - void reload(); + @Override + protected void load(boolean initial) { + super.load(initial); + this.contextsFile.load(); + } - /** - * Loads all configuration values. - */ - void load(); + @Override + public void reload() { + super.reload(); + getPlugin().getEventDispatcher().dispatchConfigReload(); + } - /** - * Gets the value of a given context key. - * - * @param key the key - * @param the key return type - * @return the value mapped to the given key. May be null. - */ - T get(ConfigKey key); + public ContextsFile getContextsFile() { + return this.contextsFile; + } + public LuckPermsPlugin getPlugin() { + return this.plugin; + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/config/generic/KeyedConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/generic/KeyedConfiguration.java new file mode 100644 index 000000000..c6809fca8 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/config/generic/KeyedConfiguration.java @@ -0,0 +1,123 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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> keys; + private final ValuesMap values; + + public KeyedConfiguration(ConfigurationAdapter adapter, List> 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 the key return type + * @return the value mapped to the given key. May be null. + */ + public T get(ConfigKey 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> initialise(Class keysClass) { + // get a list of all keys + List> 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 get(ConfigKey key) { + return (T) this.values[key.ordinal()]; + } + + public void put(ConfigKey key, Object value) { + this.values[key.ordinal()] = value; + } + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurateConfigAdapter.java b/common/src/main/java/me/lucko/luckperms/common/config/generic/adapter/ConfigurateConfigAdapter.java similarity index 98% rename from common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurateConfigAdapter.java rename to common/src/main/java/me/lucko/luckperms/common/config/generic/adapter/ConfigurateConfigAdapter.java index ff7ea68e0..26841d615 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurateConfigAdapter.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/generic/adapter/ConfigurateConfigAdapter.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.common.config.adapter; +package me.lucko.luckperms.common.config.generic.adapter; import com.google.common.base.Splitter; diff --git a/common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurationAdapter.java b/common/src/main/java/me/lucko/luckperms/common/config/generic/adapter/ConfigurationAdapter.java similarity index 96% rename from common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurationAdapter.java rename to common/src/main/java/me/lucko/luckperms/common/config/generic/adapter/ConfigurationAdapter.java index 91c228f82..d91d67eab 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurationAdapter.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/generic/adapter/ConfigurationAdapter.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.common.config.adapter; +package me.lucko.luckperms.common.config.generic.adapter; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKey.java b/common/src/main/java/me/lucko/luckperms/common/config/generic/key/ConfigKey.java similarity index 78% rename from common/src/main/java/me/lucko/luckperms/common/config/ConfigKey.java rename to common/src/main/java/me/lucko/luckperms/common/config/generic/key/ConfigKey.java index 7e18cde31..f97608910 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/generic/key/ConfigKey.java @@ -23,9 +23,9 @@ * 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. @@ -35,20 +35,21 @@ import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; public interface ConfigKey { /** - * Gets the position of this key within the {@link ConfigKeys} enum. - * - *

This is set shortly after the key is created, during the initialisation - * of {@link ConfigKeys}.

+ * Gets the position of this key within the keys enum. * * @return the position */ int ordinal(); /** - * Resolves and returns the value mapped to this key using the given config instance. + * Gets if the config key can be reloaded. * - *

The {@link LuckPermsConfiguration#get(ConfigKey)} method should be used to - * retrieve the value, as opposed to calling this directly.

+ * @return the if the key can be reloaded + */ + boolean reloadable(); + + /** + * Resolves and returns the value mapped to this key using the given config instance. * * @param adapter the config adapter instance * @return the value mapped to this key diff --git a/common/src/main/java/me/lucko/luckperms/common/config/generic/key/ConfigKeyFactory.java b/common/src/main/java/me/lucko/luckperms/common/config/generic/key/ConfigKeyFactory.java new file mode 100644 index 000000000..58afb78b3 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/config/generic/key/ConfigKeyFactory.java @@ -0,0 +1,99 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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 { + + ConfigKeyFactory BOOLEAN = ConfigurationAdapter::getBoolean; + ConfigKeyFactory STRING = ConfigurationAdapter::getString; + ConfigKeyFactory LOWERCASE_STRING = (adapter, path, def) -> adapter.getString(path, def).toLowerCase(); + ConfigKeyFactory> STRING_MAP = (config, path, def) -> ImmutableMap.copyOf(config.getStringMap(path, ImmutableMap.of())); + + static SimpleConfigKey key(Function function) { + return new SimpleConfigKey<>(function); + } + + static SimpleConfigKey notReloadable(SimpleConfigKey key) { + key.setReloadable(false); + return key; + } + + static SimpleConfigKey booleanKey(String path, boolean def) { + return key(new Bound<>(BOOLEAN, path, def)); + } + + static SimpleConfigKey stringKey(String path, String def) { + return key(new Bound<>(STRING, path, def)); + } + + static SimpleConfigKey lowercaseStringKey(String path, String def) { + return key(new Bound<>(LOWERCASE_STRING, path, def)); + } + + static SimpleConfigKey> 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 the value type + */ + class Bound implements Function { + private final ConfigKeyFactory factory; + private final String path; + private final T def; + + Bound(ConfigKeyFactory 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); + } + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/config/generic/key/SimpleConfigKey.java b/common/src/main/java/me/lucko/luckperms/common/config/generic/key/SimpleConfigKey.java new file mode 100644 index 000000000..3e2012960 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/config/generic/key/SimpleConfigKey.java @@ -0,0 +1,69 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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 the value type + */ +public class SimpleConfigKey implements ConfigKey { + private final Function function; + + private int ordinal = -1; + private boolean reloadable = true; + + SimpleConfigKey(Function 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; + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java index 2bef6423d..916378c52 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java @@ -29,10 +29,9 @@ import me.lucko.luckperms.common.actionlog.LogDispatcher; import me.lucko.luckperms.common.api.ApiRegistrationUtil; import me.lucko.luckperms.common.api.LuckPermsApiProvider; 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.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.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.DependencyManager; @@ -113,7 +112,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { // load configuration getLogger().info("Loading configuration..."); - this.configuration = new AbstractConfiguration(this, provideConfigurationAdapter()); + this.configuration = new LuckPermsConfiguration(this, provideConfigurationAdapter()); // load locale this.localeManager = new LocaleManager(); diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java index abaf783ed..0fb8a0d08 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java @@ -30,7 +30,7 @@ import me.lucko.luckperms.common.api.implementation.ApiUser; import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.command.access.CommandPermission; 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.messaging.MessagingFactory; import me.lucko.luckperms.common.model.User; diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitConfigAdapter.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitConfigAdapter.java index 973a99047..e53937d4a 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitConfigAdapter.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/NukkitConfigAdapter.java @@ -25,7 +25,7 @@ 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 cn.nukkit.utils.Config; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index b1196a3dc..5cc296478 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -29,7 +29,7 @@ import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.calculator.CalculatorFactory; import me.lucko.luckperms.common.command.abstraction.Command; 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.event.AbstractEventBus; import me.lucko.luckperms.common.messaging.MessagingFactory; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java index 8ff341d77..592f20cd4 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java @@ -25,8 +25,8 @@ package me.lucko.luckperms.sponge; -import me.lucko.luckperms.common.config.adapter.ConfigurateConfigAdapter; -import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; +import me.lucko.luckperms.common.config.generic.adapter.ConfigurateConfigAdapter; +import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import ninja.leaping.configurate.ConfigurationNode; diff --git a/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java b/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java index ce32c8699..1ee7d93f7 100644 --- a/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java +++ b/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.velocity; import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.calculator.CalculatorFactory; 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.event.AbstractEventBus; import me.lucko.luckperms.common.messaging.MessagingFactory; diff --git a/velocity/src/main/java/me/lucko/luckperms/velocity/VelocityConfigAdapter.java b/velocity/src/main/java/me/lucko/luckperms/velocity/VelocityConfigAdapter.java index 174b10b04..9e21cecd4 100644 --- a/velocity/src/main/java/me/lucko/luckperms/velocity/VelocityConfigAdapter.java +++ b/velocity/src/main/java/me/lucko/luckperms/velocity/VelocityConfigAdapter.java @@ -25,8 +25,8 @@ package me.lucko.luckperms.velocity; -import me.lucko.luckperms.common.config.adapter.ConfigurateConfigAdapter; -import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; +import me.lucko.luckperms.common.config.generic.adapter.ConfigurateConfigAdapter; +import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import ninja.leaping.configurate.ConfigurationNode;