From f73485054ee06aa58cf7462e61179f6f560e9178 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:19:19 -0700 Subject: [PATCH] Move nickname settings to a section --- .../command/commands/NicknameCommand.java | 6 +- .../carbon/common/config/ConfigManager.java | 7 +- .../carbon/common/config/PrimaryConfig.java | 104 ++++++++++++------ .../common/users/CarbonPlayerCommon.java | 4 +- .../common/users/db/DatabaseUserManager.java | 2 + .../carbon/paper/users/CarbonPlayerPaper.java | 2 +- 6 files changed, 85 insertions(+), 40 deletions(-) diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java index 69f32ee79..57e92a801 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java @@ -76,7 +76,7 @@ public Key key() { @Override public void init() { - if (!this.config.primaryConfig().useCarbonNicknames()) { + if (!this.config.primaryConfig().nickname().useCarbonNicknames()) { return; } @@ -132,8 +132,8 @@ private void applyNickname(final Commander sender, final CarbonPlayer target, fi // If the nickname is caught in the character limit, return without setting a nickname. final int nickNameLength = PlainTextComponentSerializer.plainText().serialize(parsedNick.get()).length(); - final int minLength = this.config.primaryConfig().nicknameMinLength(); - final int maxLength = this.config.primaryConfig().nicknameMaxLength(); + final int minLength = this.config.primaryConfig().nickname().minLength(); + final int maxLength = this.config.primaryConfig().nickname().maxLength(); if (nickNameLength < minLength || maxLength < nickNameLength) { this.carbonMessages.nicknameErrorCharacterLimit(sender, parsedNick.get(), minLength, maxLength); return; 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 7fa21ba20..3c6da177a 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 @@ -39,6 +39,7 @@ 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.hocon.HoconConfigurationLoader; import org.spongepowered.configurate.loader.ConfigurationLoader; @@ -133,6 +134,10 @@ public ConfigurationLoader configurationLoader(final Path file) { try { final var node = loader.load(); + try { + clazz.getDeclaredMethod("upgrade", ConfigurationNode.class).invoke(null, node); + } catch (final NoSuchMethodException ignore) { + } final @Nullable T config = node.get(clazz); if (config == null) { throw new ConfigurateException(node, "Failed to deserialize " + clazz.getName() + " from node"); @@ -140,7 +145,7 @@ public ConfigurationLoader configurationLoader(final Path file) { node.set(clazz, config); loader.save(node); return config; - } catch (final ConfigurateException exception) { + } catch (final ConfigurateException | ReflectiveOperationException exception) { this.logger.error("Failed to load config '{}'", file, exception); return null; } 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 61557626e..746b4f75f 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 @@ -22,13 +22,19 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import net.draycia.carbon.common.util.Exceptions; import net.kyori.adventure.key.Key; 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; import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.meta.Comment; +import org.spongepowered.configurate.transformation.ConfigurationTransformation; @ConfigSerializable @DefaultQualifier(NonNull.class) @@ -38,34 +44,34 @@ public class PrimaryConfig { private Locale defaultLocale = Locale.US; @Comment(""" - The default channel that new players will be in when they join. - If the channel is not found or the player cannot use the channel, they will speak in basic non-channel chat. - """) + The default channel that new players will be in when they join. + If the channel is not found or the player cannot use the channel, they will speak in basic non-channel chat. + """) private Key defaultChannel = Key.key("carbon", "global"); @Comment(""" - The service that will be used to store and load player information. - One of: JSON, H2, MYSQL, PSQL - Note: If you choose MYSQL or PSQL make sure you configure the "database-settings" section of this file! - """) + The service that will be used to store and load player information. + One of: JSON, H2, MYSQL, PSQL + Note: If you choose MYSQL or PSQL make sure you configure the "database-settings" section of this file! + """) private StorageType storageType = StorageType.JSON; @Comment(""" - When "storage-type" is set to MYSQL or PSQL, this section configures the database connection. - If JSON or H2 storage is used, this section can be ignored. - """) + When "storage-type" is set to MYSQL or PSQL, this section configures the database connection. + If JSON or H2 storage is used, this section can be ignored. + """) private DatabaseSettings databaseSettings = new DatabaseSettings(); @Comment("Various ClearChat command settings.") private ClearChatSettings clearChatSettings = new ClearChatSettings(); @Comment(""" - Plugin-wide custom placeholders. - These will be parsed in all messages rendered and sent by Carbon. - This includes chat, command feedback, and others. - Make sure to close your tags so they do not bleed into other formats. - Only a single pass is done so custom placeholders will not work within each other. - """) + Plugin-wide custom placeholders. + These will be parsed in all messages rendered and sent by Carbon. + This includes chat, command feedback, and others. + Make sure to close your tags so they do not bleed into other formats. + Only a single pass is done so custom placeholders will not work within each other. + """) private Map customPlaceholders = Map.of(); @Comment("The suggestions shown when using the TAB key in chat.") @@ -89,15 +95,11 @@ public class PrimaryConfig { ); private MessagingSettings messagingSettings = new MessagingSettings(); + private NicknameSettings nicknameSettings = new NicknameSettings(); - @Comment("Whether Carbon's nickname management should be used. Disable this if you wish to have another plugin manage nicknames.") - private boolean useCarbonNicknames = true; - - @Comment("Minimum number of characters in nickname.") - private int nicknameMinLength = 3; - - @Comment("Maximum number of characters in nickname.") - private int nicknameMaxLength = 16; + public NicknameSettings nickname() { + return this.nicknameSettings; + } @Comment("Whether Carbon should check for updates using the GitHub API on startup.") private boolean updateChecker = true; @@ -178,20 +180,56 @@ public MessagingSettings messagingSettings() { return this.messageSound; } - public boolean useCarbonNicknames() { - return this.useCarbonNicknames; + public boolean updateChecker() { + return this.updateChecker; } - public int nicknameMinLength() { - return this.nicknameMinLength; + @SuppressWarnings("unused") + public static void upgrade(final ConfigurationNode node) { + final ConfigurationTransformation.VersionedBuilder builder = ConfigurationTransformation.versionedBuilder() + .versionKey("config-version"); + final ConfigurationTransformation initial = ConfigurationTransformation.builder() + .addAction(NodePath.path("use-carbon-nicknames"), (path, value) -> new Object[]{"nickname-settings", "use-carbon-nicknames"}) + .build(); + builder.addVersion(0, initial); + final ConfigurationTransformation.Versioned upgrader = builder.build(); + final int from = upgrader.version(node); + try { + upgrader.apply(node); + } 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!"); + } + } } - public int nicknameMaxLength() { - return this.nicknameMaxLength; - } + @ConfigSerializable + public static final class NicknameSettings { - public boolean updateChecker() { - return this.updateChecker; + @Comment("Minimum number of characters in nickname (excluding formatting).") + private int minLength = 3; + + @Comment("Maximum number of characters in nickname (excluding formatting).") + private int maxLength = 16; + + @Comment("Whether Carbon's nickname management should be used. Disable this if you wish to have another plugin manage nicknames.") + private boolean useCarbonNicknames = true; + + public boolean useCarbonNicknames() { + return this.useCarbonNicknames; + } + + public int minLength() { + return this.minLength; + } + + public int maxLength() { + return this.maxLength; + } } public enum StorageType { diff --git a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java index 66f832cce..b60f5cc73 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java +++ b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java @@ -170,7 +170,7 @@ public Audience audience() { @Override public @Nullable Component nickname() { - if (!this.config.primaryConfig().useCarbonNicknames()) { + if (!this.config.primaryConfig().nickname().useCarbonNicknames()) { return null; } return this.displayName.orNull(); @@ -437,7 +437,7 @@ public boolean transientLoadedNeedsUnload() { @Override public boolean hasNickname() { - if (!this.config.primaryConfig().useCarbonNicknames()) { + if (!this.config.primaryConfig().nickname().useCarbonNicknames()) { return false; } return this.displayName.hasValue(); diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java index a0fbb6720..a402ed1b7 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java @@ -271,6 +271,8 @@ public DatabaseUserManager create(final String migrationsLocation, final Consume } private record CarbonLogCreator(Logger logger) implements LogCreator { + + @Override public Log createLogger(final Class clazz) { final Logger l = this.logger; return new Log() { diff --git a/paper/src/main/java/net/draycia/carbon/paper/users/CarbonPlayerPaper.java b/paper/src/main/java/net/draycia/carbon/paper/users/CarbonPlayerPaper.java index 0df838c9b..009813c70 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/users/CarbonPlayerPaper.java +++ b/paper/src/main/java/net/draycia/carbon/paper/users/CarbonPlayerPaper.java @@ -55,7 +55,7 @@ private CarbonPlayerPaper( ) { super(carbonPlayerCommon); - if (config.primaryConfig().useCarbonNicknames()) { + if (config.primaryConfig().nickname().useCarbonNicknames()) { this.player().ifPresent(this.applyDisplayNameToBukkit(carbonPlayerCommon.nickname())); } }