From cd12530675f8a1b0a6bd3c771a939df6a8cfb538 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 31 Dec 2023 17:57:22 +0200 Subject: [PATCH] Some config improvements --- .../configurate/annotation/Constants.java | 6 +++-- .../abstraction/ConfigurateConfigManager.java | 25 ++++++++++--------- .../config/connection/ConnectionConfig.java | 4 +-- .../config/main/AvatarProviderConfig.java | 6 +++-- .../common/config/main/ConsoleConfig.java | 5 +++- .../config/main/DiscordCommandConfig.java | 11 ++++---- .../config/main/TimedUpdaterConfig.java | 19 +++++++++----- 7 files changed, 45 insertions(+), 31 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/annotation/Constants.java b/common/src/main/java/com/discordsrv/common/config/configurate/annotation/Constants.java index 79106ffe..64375794 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/annotation/Constants.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/annotation/Constants.java @@ -12,7 +12,8 @@ import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface Constants { - String[] value(); + String[] value() default {}; + int[] intValue() default {}; /** * Needs to go after {@link org.spongepowered.configurate.objectmapping.meta.Comment}. @@ -20,6 +21,7 @@ public @interface Constants { @Retention(RetentionPolicy.RUNTIME) @interface Comment { - String[] value(); + String[] value() default {}; + int[] intValue() default {}; } } diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java index e66fb016..20d9016f 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java @@ -53,10 +53,7 @@ import org.spongepowered.configurate.yaml.YamlConfigurationLoader; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.nio.file.Path; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.regex.Pattern; public abstract class ConfigurateConfigManager> @@ -218,13 +215,14 @@ public abstract class ConfigurateConfigManager (value, destination) -> { - if (data == null || data.value().length == 0) { + String[] values = getValues(data.value(), data.intValue()); + if (values.length == 0) { return; } @@ -234,18 +232,21 @@ public abstract class ConfigurateConfigManager values = new ArrayList<>(Arrays.asList(value)); + for (int i : intValue) { + values.add(String.valueOf(i)); + } + return values.toArray(new String[0]); + } + private static String doSubstitution(String input, String[] values) { for (int i = 0; i < values.length; i++) { input = input.replace("%" + (i + 1), values[i]); diff --git a/common/src/main/java/com/discordsrv/common/config/connection/ConnectionConfig.java b/common/src/main/java/com/discordsrv/common/config/connection/ConnectionConfig.java index c936bff0..90017d14 100644 --- a/common/src/main/java/com/discordsrv/common/config/connection/ConnectionConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/connection/ConnectionConfig.java @@ -28,12 +28,12 @@ public class ConnectionConfig implements Config { public static final String FILE_NAME = "connections.yaml"; - @Constants(MainConfig.FILE_NAME) + @Constants({MainConfig.FILE_NAME, "443", "https/wss"}) public static final String HEADER = "DiscordSRV's configuration file for connections to different external services.\n" + "This file is intended to contain connection details to services in order to keep them out of the %1\n" + "and to serve as a easy way to identify and control what external connections are being used.\n" + "\n" - + "All domains listed as \"Requires a connection to\" require port 443 (https/wss) unless otherwise specified\n" + + "All domains listed as \"Requires a connection to\" require port %2 (%3) unless otherwise specified\n" + "\n" + " ABSOLUTELY DO NOT SEND THIS FILE TO ANYONE - IT ONLY CONTAINS SECRETS\n"; diff --git a/common/src/main/java/com/discordsrv/common/config/main/AvatarProviderConfig.java b/common/src/main/java/com/discordsrv/common/config/main/AvatarProviderConfig.java index 9cdd37f2..6b472231 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/AvatarProviderConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/AvatarProviderConfig.java @@ -1,5 +1,6 @@ package com.discordsrv.common.config.main; +import com.discordsrv.common.config.configurate.annotation.Constants; import com.discordsrv.common.config.configurate.annotation.Untranslated; import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.meta.Comment; @@ -12,8 +13,9 @@ public class AvatarProviderConfig { public boolean autoDecideAvatarUrl = true; @Untranslated(Untranslated.Type.VALUE) + @Constants("auto-decide-avatar-url") @Comment("The template for URLs of player avatars\n" + - "This will be used for official Java players only if auto-decide-avatar-url is set to true\n" + - "This will be used ALWAYS if auto-decide-avatar-url is set to false") + "This will be used for official Java players only if %1 is set to true\n" + + "This will be used ALWAYS if %1 is set to false") public String avatarUrlTemplate = "https://crafatar.com/avatars/%player_uuid_short%.png?size=128&overlay#%player_skin_texture_id%"; } diff --git a/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java b/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java index 91fd75bd..5089aa84 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/ConsoleConfig.java @@ -5,7 +5,10 @@ import com.discordsrv.common.config.main.generic.GameCommandExecutionConditionCo import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.meta.Comment; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; @ConfigSerializable public class ConsoleConfig { diff --git a/common/src/main/java/com/discordsrv/common/config/main/DiscordCommandConfig.java b/common/src/main/java/com/discordsrv/common/config/main/DiscordCommandConfig.java index ab182585..89d65904 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/DiscordCommandConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/DiscordCommandConfig.java @@ -33,12 +33,11 @@ public class DiscordCommandConfig { public boolean ephemeral = true; @Comment("The mode for the command output, available options are:\n" - + "- %1: Regular Discord markdown\n" - + "- %2: A colored ansi code block\n" - + "- %3: Plain text\n" - + "- %4: Plain code block\n" - + "- %5: No command output") - @Constants.Comment({"markdown", "ansi", "plain", "code_block", "off"}) + + "- markdown: Regular Discord markdown\n" + + "- ansi: A colored ansi code block\n" + + "- plain: Plain text\n" + + "- code_block: Plain code block\n" + + "- off: No command output") public OutputMode outputMode = OutputMode.MARKDOWN; @Comment("At least one condition has to match to allow execution") diff --git a/common/src/main/java/com/discordsrv/common/config/main/TimedUpdaterConfig.java b/common/src/main/java/com/discordsrv/common/config/main/TimedUpdaterConfig.java index 2c44a8b8..cb5ffda4 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/TimedUpdaterConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/TimedUpdaterConfig.java @@ -18,6 +18,7 @@ package com.discordsrv.common.config.main; +import com.discordsrv.common.config.configurate.annotation.Constants; import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.meta.Comment; @@ -49,6 +50,8 @@ public class TimedUpdaterConfig { public static class VoiceChannelConfig implements UpdaterConfig { + private static final int MINIMUM_MINUTES = 10; + @Comment("The channel IDs.\n" + "The bot will need the \"View Channel\", \"Manage Channels\" and \"Connection\" permissions for the provided channels") public List channelIds = new ArrayList<>(); @@ -56,8 +59,9 @@ public class TimedUpdaterConfig { @Comment("The format for the channel name(s), placeholders are supported.") public String nameFormat = ""; - @Comment("The time between updates in minutes. The minimum time is 10 minutes.") - public int timeMinutes = 10; + @Constants.Comment(intValue = MINIMUM_MINUTES) + @Comment("The time between updates in minutes. The minimum time is %1 minutes.") + public int timeMinutes = MINIMUM_MINUTES; @Override public boolean any() { @@ -71,12 +75,14 @@ public class TimedUpdaterConfig { @Override public long minimumSeconds() { - return TimeUnit.MINUTES.toSeconds(10); + return TimeUnit.MINUTES.toSeconds(MINIMUM_MINUTES); } } public static class TextChannelConfig implements UpdaterConfig { + private static final int MINIMUM_MINUTES = 10; + @Comment("The channel IDs.\n" + "The bot will need the \"View Channel\" and \"Manage Channels\" permissions for the provided channels") public List channelIds = new ArrayList<>(); @@ -89,8 +95,9 @@ public class TimedUpdaterConfig { + "If this is blank, the topic will not be updated") public String topicFormat = ""; - @Comment("The time between updates in minutes. The minimum time is 10 minutes.") - public int timeMinutes = 10; + @Constants.Comment(intValue = MINIMUM_MINUTES) + @Comment("The time between updates in minutes. The minimum time is %1 minutes.") + public int timeMinutes = MINIMUM_MINUTES; @Override public boolean any() { @@ -104,7 +111,7 @@ public class TimedUpdaterConfig { @Override public long minimumSeconds() { - return TimeUnit.MINUTES.toSeconds(10); + return TimeUnit.MINUTES.toSeconds(MINIMUM_MINUTES); } }