Always override existing config comments (closes #311)

This commit is contained in:
Jason Penilla committed 2023-10-23 15:19:21 -07:00
1 parent 5483bfd834
commit 13769649d6
2 files changed
+33 -19

No files matched your search

@@ -68,8 +68,6 @@ import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
@Singleton
@@ -77,17 +75,6 @@ import org.spongepowered.configurate.transformation.ConfigurationTransformation;
public class CarbonChannelRegistry extends ChatListenerInternal implements ChannelRegistry {
private static final String PARTYCHAT_CONF = "partychat.conf";
private static @MonotonicNonNull ObjectMapper<ConfigChatChannel> MAPPER;
private static @MonotonicNonNull ObjectMapper<PartyChatChannel> PARTY_MAPPER;
static {
try {
MAPPER = ObjectMapper.factory().get(ConfigChatChannel.class);
PARTY_MAPPER = ObjectMapper.factory().get(PartyChatChannel.class);
} catch (final SerializationException e) {
e.printStackTrace();
}
}
private final Path configChannelDir;
private final Injector injector;
@@ -297,9 +284,21 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
final ConfigurationLoader<?> loader = this.config.configurationLoader(channelFile);
try {
final Class<? extends ConfigChatChannel> type = this.config.primaryConfig().partyChat().enabled && channelFile.getFileName().toString().equals(PARTYCHAT_CONF)
? PartyChatChannel.class
: ConfigChatChannel.class;
final ConfigurationNode loaded = updateNode(loader.load());
loader.save(loaded);
return (this.config.primaryConfig().partyChat().enabled && channelFile.getFileName().toString().equals(PARTYCHAT_CONF) ? PARTY_MAPPER : MAPPER).load(loaded);
final @Nullable ConfigChatChannel channel = loaded.get(type);
if (channel == null) {
throw new ConfigurateException("Config deserialized to null.");
}
final ConfigurationNode newNode = loader.createNode();
newNode.set(type, channel);
loader.save(newNode);
return channel;
} catch (final ConfigurateException exception) {
this.logger.warn("Failed to load channel from file '{}'", channelFile, exception);
}
@@ -38,10 +38,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.spongepowered.configurate.CommentedConfigurationNodeIntermediary;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.objectmapping.meta.Comment;
import org.spongepowered.configurate.objectmapping.meta.Processor;
@DefaultQualifier(NonNull.class)
@Singleton
@@ -112,15 +116,26 @@ public final class ConfigManager {
final ConfigurateComponentSerializer serializer =
ConfigurateComponentSerializer.configurate();
return opts.shouldCopyDefaults(true).serializers(serializerBuilder ->
serializerBuilder.registerAll(serializer.serializers())
.register(Locale.class, this.locale)
);
return opts.shouldCopyDefaults(true)
.serializers(serializerBuilder ->
serializerBuilder.registerAll(serializer.serializers())
.register(Locale.class, this.locale)
.registerAnnotatedObjects(ObjectMapper.factoryBuilder()
.addProcessor(Comment.class, overrideComments())
.build()));
})
.path(file)
.build();
}
private static Processor.Factory<Comment, Object> overrideComments() {
return (data, fieldType) -> (value, destination) -> {
if (destination instanceof final CommentedConfigurationNodeIntermediary<?> commented) {
commented.comment(data.value());
}
};
}
public <T> @Nullable T load(final Class<T> clazz, final String fileName) {
final Path file = this.dataDirectory.resolve(fileName);
try {