Move nickname settings to a section

This commit is contained in:
Jason Penilla committed 2023-09-18 13:19:19 -07:00
1 parent 2b1a3cd301
commit f73485054e
6 files changed
+88 -43

No files matched your search

@@ -76,7 +76,7 @@ public final class NicknameCommand extends CarbonCommand {
@Override @Override
public void init() { public void init() {
if (!this.config.primaryConfig().useCarbonNicknames()) { if (!this.config.primaryConfig().nickname().useCarbonNicknames()) {
return; return;
} }
@@ -132,8 +132,8 @@ public final class NicknameCommand extends CarbonCommand {
// If the nickname is caught in the character limit, return without setting a nickname. // 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 nickNameLength = PlainTextComponentSerializer.plainText().serialize(parsedNick.get()).length();
final int minLength = this.config.primaryConfig().nicknameMinLength(); final int minLength = this.config.primaryConfig().nickname().minLength();
final int maxLength = this.config.primaryConfig().nicknameMaxLength(); final int maxLength = this.config.primaryConfig().nickname().maxLength();
if (nickNameLength < minLength || maxLength < nickNameLength) { if (nickNameLength < minLength || maxLength < nickNameLength) {
this.carbonMessages.nicknameErrorCharacterLimit(sender, parsedNick.get(), minLength, maxLength); this.carbonMessages.nicknameErrorCharacterLimit(sender, parsedNick.get(), minLength, maxLength);
return; return;
@@ -39,6 +39,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier; import org.checkerframework.framework.qual.DefaultQualifier;
import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader; import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.loader.ConfigurationLoader; import org.spongepowered.configurate.loader.ConfigurationLoader;
@@ -133,6 +134,10 @@ public final class ConfigManager {
try { try {
final var node = loader.load(); 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); final @Nullable T config = node.get(clazz);
if (config == null) { if (config == null) {
throw new ConfigurateException(node, "Failed to deserialize " + clazz.getName() + " from node"); throw new ConfigurateException(node, "Failed to deserialize " + clazz.getName() + " from node");
@@ -140,7 +145,7 @@ public final class ConfigManager {
node.set(clazz, config); node.set(clazz, config);
loader.save(node); loader.save(node);
return config; return config;
} catch (final ConfigurateException exception) { } catch (final ConfigurateException | ReflectiveOperationException exception) {
this.logger.error("Failed to load config '{}'", file, exception); this.logger.error("Failed to load config '{}'", file, exception);
return null; return null;
} }
@@ -22,13 +22,19 @@ package net.draycia.carbon.common.config;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import net.draycia.carbon.common.util.Exceptions;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.Sound;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier; 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.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment; import org.spongepowered.configurate.objectmapping.meta.Comment;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
@ConfigSerializable @ConfigSerializable
@DefaultQualifier(NonNull.class) @DefaultQualifier(NonNull.class)
@@ -38,34 +44,34 @@ public class PrimaryConfig {
private Locale defaultLocale = Locale.US; private Locale defaultLocale = Locale.US;
@Comment(""" @Comment("""
The default channel that new players will be in when they join. 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. 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"); private Key defaultChannel = Key.key("carbon", "global");
@Comment(""" @Comment("""
The service that will be used to store and load player information. The service that will be used to store and load player information.
One of: JSON, H2, MYSQL, PSQL One of: JSON, H2, MYSQL, PSQL
Note: If you choose MYSQL or PSQL make sure you configure the "database-settings" section of this file! Note: If you choose MYSQL or PSQL make sure you configure the "database-settings" section of this file!
""") """)
private StorageType storageType = StorageType.JSON; private StorageType storageType = StorageType.JSON;
@Comment(""" @Comment("""
When "storage-type" is set to MYSQL or PSQL, this section configures the database connection. 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. If JSON or H2 storage is used, this section can be ignored.
""") """)
private DatabaseSettings databaseSettings = new DatabaseSettings(); private DatabaseSettings databaseSettings = new DatabaseSettings();
@Comment("Various ClearChat command settings.") @Comment("Various ClearChat command settings.")
private ClearChatSettings clearChatSettings = new ClearChatSettings(); private ClearChatSettings clearChatSettings = new ClearChatSettings();
@Comment(""" @Comment("""
Plugin-wide custom placeholders. Plugin-wide custom placeholders.
These will be parsed in all messages rendered and sent by Carbon. These will be parsed in all messages rendered and sent by Carbon.
This includes chat, command feedback, and others. This includes chat, command feedback, and others.
Make sure to close your tags so they do not bleed into other formats. 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. Only a single pass is done so custom placeholders will not work within each other.
""") """)
private Map<String, String> customPlaceholders = Map.of(); private Map<String, String> customPlaceholders = Map.of();
@Comment("The suggestions shown when using the TAB key in chat.") @Comment("The suggestions shown when using the TAB key in chat.")
@@ -89,15 +95,11 @@ public class PrimaryConfig {
); );
private MessagingSettings messagingSettings = new MessagingSettings(); 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.") public NicknameSettings nickname() {
private boolean useCarbonNicknames = true; return this.nicknameSettings;
}
@Comment("Minimum number of characters in nickname.")
private int nicknameMinLength = 3;
@Comment("Maximum number of characters in nickname.")
private int nicknameMaxLength = 16;
@Comment("Whether Carbon should check for updates using the GitHub API on startup.") @Comment("Whether Carbon should check for updates using the GitHub API on startup.")
private boolean updateChecker = true; private boolean updateChecker = true;
@@ -178,22 +180,58 @@ public class PrimaryConfig {
return this.messageSound; return this.messageSound;
} }
public boolean useCarbonNicknames() {
return this.useCarbonNicknames;
}
public int nicknameMinLength() {
return this.nicknameMinLength;
}
public int nicknameMaxLength() {
return this.nicknameMaxLength;
}
public boolean updateChecker() { public boolean updateChecker() {
return this.updateChecker; return this.updateChecker;
} }
@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!");
}
}
}
@ConfigSerializable
public static final class NicknameSettings {
@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 { public enum StorageType {
JSON, JSON,
MYSQL, MYSQL,
@@ -170,7 +170,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public @Nullable Component nickname() { public @Nullable Component nickname() {
if (!this.config.primaryConfig().useCarbonNicknames()) { if (!this.config.primaryConfig().nickname().useCarbonNicknames()) {
return null; return null;
} }
return this.displayName.orNull(); return this.displayName.orNull();
@@ -437,7 +437,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public boolean hasNickname() { public boolean hasNickname() {
if (!this.config.primaryConfig().useCarbonNicknames()) { if (!this.config.primaryConfig().nickname().useCarbonNicknames()) {
return false; return false;
} }
return this.displayName.hasValue(); return this.displayName.hasValue();
@@ -271,6 +271,8 @@ public final class DatabaseUserManager extends CachingUserManager {
} }
private record CarbonLogCreator(Logger logger) implements LogCreator { private record CarbonLogCreator(Logger logger) implements LogCreator {
@Override
public Log createLogger(final Class<?> clazz) { public Log createLogger(final Class<?> clazz) {
final Logger l = this.logger; final Logger l = this.logger;
return new Log() { return new Log() {
@@ -55,7 +55,7 @@ public final class CarbonPlayerPaper extends WrappedCarbonPlayer implements Forw
) { ) {
super(carbonPlayerCommon); super(carbonPlayerCommon);
if (config.primaryConfig().useCarbonNicknames()) { if (config.primaryConfig().nickname().useCarbonNicknames()) {
this.player().ifPresent(this.applyDisplayNameToBukkit(carbonPlayerCommon.nickname())); this.player().ifPresent(this.applyDisplayNameToBukkit(carbonPlayerCommon.nickname()));
} }
} }