Skip to content

Commit

Permalink
Move nickname settings to a section
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Sep 18, 2023
1 parent 2b1a3cd commit f734850
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Key key() {

@Override
public void init() {
if (!this.config.primaryConfig().useCarbonNicknames()) {
if (!this.config.primaryConfig().nickname().useCarbonNicknames()) {
return;
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -133,14 +134,18 @@ 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");
}
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<String, String> customPlaceholders = Map.of();

@Comment("The suggestions shown when using the TAB key in chat.")
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private CarbonPlayerPaper(
) {
super(carbonPlayerCommon);

if (config.primaryConfig().useCarbonNicknames()) {
if (config.primaryConfig().nickname().useCarbonNicknames()) {
this.player().ifPresent(this.applyDisplayNameToBukkit(carbonPlayerCommon.nickname()));
}
}
Expand Down

0 comments on commit f734850

Please sign in to comment.