Some config improvements

This commit is contained in:
Vankka 2023-12-31 17:57:22 +02:00
parent 3b7701ee1a
commit cd12530675
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
7 changed files with 45 additions and 31 deletions

View File

@ -12,7 +12,8 @@ import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Constants { public @interface Constants {
String[] value(); String[] value() default {};
int[] intValue() default {};
/** /**
* Needs to go after {@link org.spongepowered.configurate.objectmapping.meta.Comment}. * Needs to go after {@link org.spongepowered.configurate.objectmapping.meta.Comment}.
@ -20,6 +21,7 @@ public @interface Constants {
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@interface Comment { @interface Comment {
String[] value(); String[] value() default {};
int[] intValue() default {};
} }
} }

View File

@ -53,10 +53,7 @@ import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays; import java.util.*;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurationLoader<CommentedConfigurationNode>> public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurationLoader<CommentedConfigurationNode>>
@ -218,13 +215,14 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
String comment = ((CommentedConfigurationNode) destination).comment(); String comment = ((CommentedConfigurationNode) destination).comment();
if (comment != null) { if (comment != null) {
((CommentedConfigurationNode) destination).comment( ((CommentedConfigurationNode) destination).comment(
doSubstitution(comment, data.value()) doSubstitution(comment, getValues(data.value(), data.intValue()))
); );
} }
} }
}) })
.addProcessor(Constants.class, (data, fieldType) -> (value, destination) -> { .addProcessor(Constants.class, (data, fieldType) -> (value, destination) -> {
if (data == null || data.value().length == 0) { String[] values = getValues(data.value(), data.intValue());
if (values.length == 0) {
return; return;
} }
@ -234,18 +232,21 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
} }
try { try {
destination.set( destination.set(doSubstitution(destination.getString(), values));
doSubstitution(
destination.getString(),
data.value()
)
);
} catch (SerializationException e) { } catch (SerializationException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}); });
} }
private String[] getValues(String[] value, int[] intValue) {
List<String> 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) { private static String doSubstitution(String input, String[] values) {
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
input = input.replace("%" + (i + 1), values[i]); input = input.replace("%" + (i + 1), values[i]);

View File

@ -28,12 +28,12 @@ public class ConnectionConfig implements Config {
public static final String FILE_NAME = "connections.yaml"; 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" 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" + "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" + "and to serve as a easy way to identify and control what external connections are being used.\n"
+ "\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" + "\n"
+ " ABSOLUTELY DO NOT SEND THIS FILE TO ANYONE - IT ONLY CONTAINS SECRETS\n"; + " ABSOLUTELY DO NOT SEND THIS FILE TO ANYONE - IT ONLY CONTAINS SECRETS\n";

View File

@ -1,5 +1,6 @@
package com.discordsrv.common.config.main; package com.discordsrv.common.config.main;
import com.discordsrv.common.config.configurate.annotation.Constants;
import com.discordsrv.common.config.configurate.annotation.Untranslated; import com.discordsrv.common.config.configurate.annotation.Untranslated;
import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment; import org.spongepowered.configurate.objectmapping.meta.Comment;
@ -12,8 +13,9 @@ public class AvatarProviderConfig {
public boolean autoDecideAvatarUrl = true; public boolean autoDecideAvatarUrl = true;
@Untranslated(Untranslated.Type.VALUE) @Untranslated(Untranslated.Type.VALUE)
@Constants("auto-decide-avatar-url")
@Comment("The template for URLs of player avatars\n" + @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 for official Java players only if %1 is set to true\n" +
"This will be used ALWAYS if auto-decide-avatar-url is set to false") "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%"; public String avatarUrlTemplate = "https://crafatar.com/avatars/%player_uuid_short%.png?size=128&overlay#%player_skin_texture_id%";
} }

View File

@ -5,7 +5,10 @@ import com.discordsrv.common.config.main.generic.GameCommandExecutionConditionCo
import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment; 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 @ConfigSerializable
public class ConsoleConfig { public class ConsoleConfig {

View File

@ -33,12 +33,11 @@ public class DiscordCommandConfig {
public boolean ephemeral = true; public boolean ephemeral = true;
@Comment("The mode for the command output, available options are:\n" @Comment("The mode for the command output, available options are:\n"
+ "- %1: Regular Discord markdown\n" + "- markdown: Regular Discord markdown\n"
+ "- %2: A colored ansi code block\n" + "- ansi: A colored ansi code block\n"
+ "- %3: Plain text\n" + "- plain: Plain text\n"
+ "- %4: Plain code block\n" + "- code_block: Plain code block\n"
+ "- %5: No command output") + "- off: No command output")
@Constants.Comment({"markdown", "ansi", "plain", "code_block", "off"})
public OutputMode outputMode = OutputMode.MARKDOWN; public OutputMode outputMode = OutputMode.MARKDOWN;
@Comment("At least one condition has to match to allow execution") @Comment("At least one condition has to match to allow execution")

View File

@ -18,6 +18,7 @@
package com.discordsrv.common.config.main; 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.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment; import org.spongepowered.configurate.objectmapping.meta.Comment;
@ -49,6 +50,8 @@ public class TimedUpdaterConfig {
public static class VoiceChannelConfig implements UpdaterConfig { public static class VoiceChannelConfig implements UpdaterConfig {
private static final int MINIMUM_MINUTES = 10;
@Comment("The channel IDs.\n" @Comment("The channel IDs.\n"
+ "The bot will need the \"View Channel\", \"Manage Channels\" and \"Connection\" permissions for the provided channels") + "The bot will need the \"View Channel\", \"Manage Channels\" and \"Connection\" permissions for the provided channels")
public List<Long> channelIds = new ArrayList<>(); public List<Long> channelIds = new ArrayList<>();
@ -56,8 +59,9 @@ public class TimedUpdaterConfig {
@Comment("The format for the channel name(s), placeholders are supported.") @Comment("The format for the channel name(s), placeholders are supported.")
public String nameFormat = ""; public String nameFormat = "";
@Comment("The time between updates in minutes. The minimum time is 10 minutes.") @Constants.Comment(intValue = MINIMUM_MINUTES)
public int timeMinutes = 10; @Comment("The time between updates in minutes. The minimum time is %1 minutes.")
public int timeMinutes = MINIMUM_MINUTES;
@Override @Override
public boolean any() { public boolean any() {
@ -71,12 +75,14 @@ public class TimedUpdaterConfig {
@Override @Override
public long minimumSeconds() { public long minimumSeconds() {
return TimeUnit.MINUTES.toSeconds(10); return TimeUnit.MINUTES.toSeconds(MINIMUM_MINUTES);
} }
} }
public static class TextChannelConfig implements UpdaterConfig { public static class TextChannelConfig implements UpdaterConfig {
private static final int MINIMUM_MINUTES = 10;
@Comment("The channel IDs.\n" @Comment("The channel IDs.\n"
+ "The bot will need the \"View Channel\" and \"Manage Channels\" permissions for the provided channels") + "The bot will need the \"View Channel\" and \"Manage Channels\" permissions for the provided channels")
public List<Long> channelIds = new ArrayList<>(); public List<Long> channelIds = new ArrayList<>();
@ -89,8 +95,9 @@ public class TimedUpdaterConfig {
+ "If this is blank, the topic will not be updated") + "If this is blank, the topic will not be updated")
public String topicFormat = ""; public String topicFormat = "";
@Comment("The time between updates in minutes. The minimum time is 10 minutes.") @Constants.Comment(intValue = MINIMUM_MINUTES)
public int timeMinutes = 10; @Comment("The time between updates in minutes. The minimum time is %1 minutes.")
public int timeMinutes = MINIMUM_MINUTES;
@Override @Override
public boolean any() { public boolean any() {
@ -104,7 +111,7 @@ public class TimedUpdaterConfig {
@Override @Override
public long minimumSeconds() { public long minimumSeconds() {
return TimeUnit.MINUTES.toSeconds(10); return TimeUnit.MINUTES.toSeconds(MINIMUM_MINUTES);
} }
} }