From a20301c5983d17d3fb6dbcd463f85632cb2e7550 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:03:24 -0700 Subject: [PATCH] Fix regression with channel radius being constantly reset --- .../channels/CarbonChannelRegistry.java | 56 +++++++------------ .../carbon/common/config/ConfigManager.java | 13 +++++ .../carbon/common/config/PrimaryConfig.java | 11 +--- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java index d7df353c..336f84ca 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java @@ -70,7 +70,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.framework.qual.DefaultQualifier; 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.transformation.ConfigurationTransformation; @@ -126,44 +125,32 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann events.subscribe(CarbonReloadEvent.class, -99, true, event -> this.reloadConfigChannels()); } - public static ConfigurationTransformation.Versioned versioned() { + public static ConfigurationTransformation.Versioned configChatChannelUpgrader() { + // final ConfigurationTransformation initial; + return ConfigurationTransformation.versionedBuilder() - .addVersion(0, initialTransform()) + .versionKey(ConfigManager.CONFIG_VERSION_KEY) + // .addVersion(0, initial) .build(); } - private static ConfigurationTransformation initialTransform() { - return ConfigurationTransformation.builder() - .addAction(NodePath.path(), (path, value) -> { - value.node("radius").set(-1); + public static N upgradeConfigChatChannelNode(final N node) throws ConfigurateException { + if (true) { + // No transformations yet! + return node; + } - return null; - }) - .build(); - } - - // https://github.com/SpongePowered/Configurate/blob/1ec74f6474237585aee858b636d9761d237839d5/examples/src/main/java/org/spongepowered/configurate/examples/Transformations.java#L107 - - /** - * Apply the transformations to a node. - * - *

This method also prints information about the version update that - * occurred

- * - * @param node the node to transform - * @param node type - * @return provided node, after transformation - */ - public static N updateNode(final N node) throws ConfigurateException { if (!node.virtual()) { // we only want to migrate existing data - final ConfigurationTransformation.Versioned trans = versioned(); - final int startVersion = trans.version(node); - trans.apply(node); - final int endVersion = trans.version(node); + final ConfigurationTransformation.Versioned upgrader = configChatChannelUpgrader(); + final int from = upgrader.version(node); + upgrader.apply(node); + final int to = upgrader.version(node); - if (startVersion != endVersion) { // we might not have made any changes + ConfigManager.configVersionComment(node, upgrader); + + if (from != to) { // we might not have made any changes // TODO: use logger - //CarbonChatProvider.carbonChat().logger().info("Updated config schema from " + startVersion + " to " + endVersion); + //CarbonChatProvider.carbonChat().logger().info("Updated config schema from " + from + " to " + to); } } @@ -315,15 +302,14 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann final Class type = special == null ? ConfigChatChannel.class : special.cls(); final ConfigurationLoader loader = this.config.configurationLoader(channelFile, ConfigManager.extractHeader(type)); - final ConfigurationNode loaded = updateNode(loader.load()); + final ConfigurationNode loaded = upgradeConfigChatChannelNode(loader.load()); 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); + loaded.set(type, channel); + loader.save(loaded); return channel; } catch (final ConfigurateException exception) { diff --git a/common/src/main/java/net/draycia/carbon/common/config/ConfigManager.java b/common/src/main/java/net/draycia/carbon/common/config/ConfigManager.java index 0a97bb53..7d564b4a 100644 --- a/common/src/main/java/net/draycia/carbon/common/config/ConfigManager.java +++ b/common/src/main/java/net/draycia/carbon/common/config/ConfigManager.java @@ -42,6 +42,7 @@ 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.CommentedConfigurationNode; import org.spongepowered.configurate.CommentedConfigurationNodeIntermediary; import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.ConfigurationNode; @@ -50,11 +51,13 @@ 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; +import org.spongepowered.configurate.transformation.ConfigurationTransformation; @DefaultQualifier(NonNull.class) @Singleton public final class ConfigManager { + public static final String CONFIG_VERSION_KEY = "config-version"; private static final String PRIMARY_CONFIG_FILE_NAME = "config.conf"; private static final String COMMAND_SETTINGS_FILE_NAME = "command-settings.conf"; @@ -187,4 +190,14 @@ public final class ConfigManager { } } + public static void configVersionComment( + final N rootNode, + final ConfigurationTransformation.Versioned versionedTransformation + ) { + final ConfigurationNode versionNode = rootNode.node(versionedTransformation.versionKey()); + if (!versionNode.virtual() && versionNode instanceof CommentedConfigurationNode commented) { + commented.comment("Used internally to track changes to the config. Do not edit manually!"); + } + } + } diff --git a/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java b/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java index 9ce05627..70c7e011 100644 --- a/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java +++ b/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java @@ -28,7 +28,6 @@ import net.kyori.adventure.sound.Sound; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.framework.qual.DefaultQualifier; -import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.NodePath; @@ -186,7 +185,7 @@ public class PrimaryConfig { @SuppressWarnings("unused") public static void upgrade(final ConfigurationNode node) { final ConfigurationTransformation.VersionedBuilder builder = ConfigurationTransformation.versionedBuilder() - .versionKey("config-version"); + .versionKey(ConfigManager.CONFIG_VERSION_KEY); final ConfigurationTransformation initial = ConfigurationTransformation.builder() .addAction(NodePath.path("use-carbon-nicknames"), (path, value) -> new Object[]{"nickname-settings", "use-carbon-nicknames"}) .build(); @@ -202,12 +201,8 @@ public class PrimaryConfig { } catch (final ConfigurateException e) { Exceptions.rethrow(e); } - if (from == ConfigurationTransformation.Versioned.VERSION_UNKNOWN) { - final ConfigurationNode versionNode = node.node(upgrader.versionKey()); - if (versionNode instanceof CommentedConfigurationNode commented) { - commented.comment("Used internally to track changes to the config. Do not edit manually!"); - } - } + + ConfigManager.configVersionComment(node, upgrader); } @ConfigSerializable