Config improvements

This commit is contained in:
Vankka 2023-05-20 20:25:52 +03:00
parent bded7cc26d
commit 7314fdb72b
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
6 changed files with 49 additions and 9 deletions

View File

@ -19,6 +19,7 @@
package com.discordsrv.bukkit.config.main; package com.discordsrv.bukkit.config.main;
import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.channel.GameChannel;
import com.discordsrv.common.config.annotation.Order;
import com.discordsrv.common.config.main.MainConfig; import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.main.PluginIntegrationConfig; import com.discordsrv.common.config.main.PluginIntegrationConfig;
import com.discordsrv.common.config.main.channels.base.ChannelConfig; import com.discordsrv.common.config.main.channels.base.ChannelConfig;
@ -35,8 +36,10 @@ public class BukkitConfig extends MainConfig {
channels.put(ChannelConfig.DEFAULT_KEY, new ServerBaseChannelConfig()); channels.put(ChannelConfig.DEFAULT_KEY, new ServerBaseChannelConfig());
} }
@Order(5)
public BukkitRequiredLinkingConfig requiredLinking = new BukkitRequiredLinkingConfig(); public BukkitRequiredLinkingConfig requiredLinking = new BukkitRequiredLinkingConfig();
@Order(100)
public PluginIntegrationConfig integrations = new PluginIntegrationConfig(); public PluginIntegrationConfig integrations = new PluginIntegrationConfig();
@Override @Override

View File

@ -84,7 +84,7 @@ public class DiscordSRVMinecraftRenderer extends DefaultMinecraftRenderer {
GuildChannel guildChannel = jda.getGuildChannelById(channel); GuildChannel guildChannel = jda.getGuildChannelById(channel);
Context context = CONTEXT.get(); Context context = CONTEXT.get();
String format = context != null ? context.config.map(cfg -> cfg.mentions).get(cfg -> cfg.message) : null; String format = context != null ? context.config.map(cfg -> cfg.mentions).get(cfg -> cfg.messageUrl) : null;
if (format == null || guildChannel == null) { if (format == null || guildChannel == null) {
return super.appendLink(part, link); return super.appendLink(part, link);
} }

View File

@ -24,7 +24,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
@ConfigSerializable @ConfigSerializable
public class MinecraftAuthConfig { public class MinecraftAuthConfig {
@Comment("If minecraftauth.me connections are allowed for Discord linking (requires linked-accounts.provider to be \"auto\" or \"minecraftauth\").\n" @Comment("If minecraftauth.me connections are allowed for Discord linking (when linked-accounts.provider is \"auto\" or \"minecraftauth\").\n"
+ "Requires a connection to: minecraftauth.me\n" + "Requires a connection to: minecraftauth.me\n"
+ "Privacy Policy: https://minecraftauth.me/privacy") + "Privacy Policy: https://minecraftauth.me/privacy")
public boolean allow = true; public boolean allow = true;

View File

@ -30,8 +30,8 @@ public class StorageConfig {
@Comment("The storage backend to use.\n\n" @Comment("The storage backend to use.\n\n"
+ "- H2\n" + "- H2\n"
+ "- MySQL\n") + "- MySQL")
public String backend = "h2"; public String backend = "H2";
@Comment("SQL table prefix") @Comment("SQL table prefix")
public String sqlTablePrefix = "discordsrv_"; public String sqlTablePrefix = "discordsrv_";

View File

@ -63,7 +63,7 @@ public class DiscordToMinecraftChatConfig {
public Format channel = new Format("[hover:show_text:Click to go to channel][click:open_url:%channel_jump_url%]&#5865f2#%channel_name%", "&#5865f2#deleted-channel"); public Format channel = new Format("[hover:show_text:Click to go to channel][click:open_url:%channel_jump_url%]&#5865f2#%channel_name%", "&#5865f2#deleted-channel");
public Format user = new Format("[hover:show_text:Tag: %user_tag%&r\nRoles: %user_roles_, |text_&7&oNone%]&#5865f2@%user_effective_name|user_name%", "&#5865f2@Unknown user"); public Format user = new Format("[hover:show_text:Tag: %user_tag%&r\nRoles: %user_roles_, |text_&7&oNone%]&#5865f2@%user_effective_name|user_name%", "&#5865f2@Unknown user");
public String message = "[hover:show_text:Click to go to message][click:open_url:%jump_url%]&#5865f2#%channel_name% > ..."; public String messageUrl = "[hover:show_text:Click to go to message][click:open_url:%jump_url%]&#5865f2#%channel_name% > ...";
@ConfigSerializable @ConfigSerializable
public static class Format { public static class Format {

View File

@ -35,18 +35,21 @@ import com.discordsrv.common.config.serializer.PatternSerializer;
import com.discordsrv.common.config.serializer.SendableDiscordMessageSerializer; import com.discordsrv.common.config.serializer.SendableDiscordMessageSerializer;
import com.discordsrv.common.exception.ConfigException; import com.discordsrv.common.exception.ConfigException;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.*;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.loader.AbstractConfigurationLoader; import org.spongepowered.configurate.loader.AbstractConfigurationLoader;
import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.FieldDiscoverer; import org.spongepowered.configurate.objectmapping.FieldDiscoverer;
import org.spongepowered.configurate.objectmapping.ObjectMapper; import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.objectmapping.meta.Comment;
import org.spongepowered.configurate.objectmapping.meta.Processor;
import org.spongepowered.configurate.serialize.SerializationException; import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import org.spongepowered.configurate.util.NamingScheme; import org.spongepowered.configurate.util.NamingScheme;
import org.spongepowered.configurate.util.NamingSchemes; import org.spongepowered.configurate.util.NamingSchemes;
import org.spongepowered.configurate.yaml.ScalarStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.lang.reflect.Type;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -103,6 +106,26 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
.shouldCopyDefaults(false) .shouldCopyDefaults(false)
.implicitInitialization(false) .implicitInitialization(false)
.serializers(builder -> { .serializers(builder -> {
builder.register(String.class, new TypeSerializer<String>() {
@Override
public String deserialize(Type type, ConfigurationNode node) {
return node.getString();
}
@Override
public void serialize(Type type, @org.checkerframework.checker.nullness.qual.Nullable String obj, ConfigurationNode node) {
RepresentationHint<ScalarStyle> hint = YamlConfigurationLoader.SCALAR_STYLE;
ScalarStyle style = node.hint(hint);
if (style == hint.defaultValue()) {
// Set scalar style for strings to double quotes, by default
node = node.hint(hint, ScalarStyle.DOUBLE_QUOTED);
}
node.raw(obj);
}
});
builder.register(BaseChannelConfig.class, getChannelConfigSerializer(objectMapper)); builder.register(BaseChannelConfig.class, getChannelConfigSerializer(objectMapper));
builder.register(Color.class, new ColorSerializer()); builder.register(Color.class, new ColorSerializer());
builder.register(Pattern.class, new PatternSerializer()); builder.register(Pattern.class, new PatternSerializer());
@ -136,6 +159,20 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
}); });
return ObjectMapper.factoryBuilder() return ObjectMapper.factoryBuilder()
.addProcessor(Comment.class, (data, fieldType) -> {
Processor<Object> processor = Processor.comments().make(data, fieldType);
return (value, destination) -> {
processor.process(value, destination);
if (destination instanceof CommentedConfigurationNode) {
String comment = ((CommentedConfigurationNode) destination).comment();
if (comment != null) {
// Yaml doesn't render empty lines correctly, so we add a space when there are double line breaks
((CommentedConfigurationNode) destination).comment(comment.replace("\n\n", "\n \n"));
}
}
};
})
.defaultNamingScheme(NAMING_SCHEME) .defaultNamingScheme(NAMING_SCHEME)
.addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.emptyConstructorObject(), fieldOrder)) .addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.emptyConstructorObject(), fieldOrder))
.addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.record(), fieldOrder)); .addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.record(), fieldOrder));