From 654964d375c2373b628b7189a7f1d157b0848578 Mon Sep 17 00:00:00 2001 From: NamiUni Date: Fri, 15 Sep 2023 15:12:54 +0900 Subject: [PATCH 1/2] Add nickname character limit. --- .../common/command/commands/NicknameCommand.java | 12 ++++++++++++ .../carbon/common/config/PrimaryConfig.java | 14 ++++++++++++++ .../carbon/common/messages/CarbonMessages.java | 8 ++++++++ .../resources/locale/messages-en_US.properties | 1 + 4 files changed, 35 insertions(+) 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 a051a05a9..95482705c 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 @@ -39,6 +39,7 @@ import net.kyori.adventure.audience.Audience; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; @@ -125,8 +126,19 @@ private void resetNickname(final Commander sender, final CarbonPlayer target) { } private void applyNickname(final Commander sender, final CarbonPlayer target, final String nick) { + // Lazy since the sender might not have permission to set the nickname final Supplier parsedNick = Suppliers.memoize(() -> parseNickname(sender, nick)); + + // 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 minLimit = this.config.primaryConfig().nicknameMinLimit(); + final int maxLimit = this.config.primaryConfig().nicknameMaxLimit(); + if (nickNameLength < minLimit || maxLimit < nickNameLength) { + this.carbonMessages.nicknameErrorCharacterLimit(sender, parsedNick.get(), minLimit, maxLimit); + return; + } + target.nickname(parsedNick.get()); if (sender instanceof PlayerCommander playerCommander 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 2d0c2b8c9..bcfc7d256 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 @@ -93,6 +93,12 @@ public class PrimaryConfig { @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 nicknameMinLimit = 3; + + @Comment("Maximum number of characters in nickname.") + private int nicknameMaxLimit = 16; + @Comment("Whether Carbon should check for updates using the GitHub API on startup.") private boolean updateChecker = true; @@ -176,6 +182,14 @@ public boolean useCarbonNicknames() { return this.useCarbonNicknames; } + public int nicknameMinLimit() { + return this.nicknameMinLimit; + } + + public int nicknameMaxLimit() { + return this.nicknameMaxLimit; + } + public boolean updateChecker() { return this.updateChecker; } diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index 9ac226f3d..85f8a5845 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -180,6 +180,14 @@ void whisperTargetNotSet( @Message("nickname.set.others") void nicknameSetOthers(final Audience audience, final String target, final Component nickname); + @Message("nickname.error.character_limit") + void nicknameErrorCharacterLimit( + final Audience audience, + final Component nickname, + @Placeholder("min_limit") final int minLimit, + @Placeholder("max_limit") final int maxLimit + ); + @Message("nickname.show.others") void nicknameShowOthers(final Audience audience, final String target, final Component nickname); diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 34332a3c9..8ac88f59c 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -97,6 +97,7 @@ nickname.show.others.unset= does not have a nickname set nickname.show.others='s nickname is nickname.show.unset=You do not have a nickname set nickname.show=Your nickname is +nickname.error.character_limit=Nickname "" has exceeded the character limit. Must be set to ~ characters. reply.target.missing=You have no-one to reply to reply.target.self=You cannot whisper to yourself whisper.console=[] -> [] From 963276bc33d3141256887fd4948ebfeffb1fcabb Mon Sep 17 00:00:00 2001 From: NamiUni Date: Mon, 18 Sep 2023 09:15:21 +0900 Subject: [PATCH 2/2] Write Length, not Limit. --- .../common/command/commands/NicknameCommand.java | 8 ++++---- .../draycia/carbon/common/config/PrimaryConfig.java | 12 ++++++------ .../carbon/common/messages/CarbonMessages.java | 4 ++-- .../main/resources/locale/messages-en_US.properties | 2 +- 4 files changed, 13 insertions(+), 13 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 95482705c..69f32ee79 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 @@ -132,10 +132,10 @@ 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 minLimit = this.config.primaryConfig().nicknameMinLimit(); - final int maxLimit = this.config.primaryConfig().nicknameMaxLimit(); - if (nickNameLength < minLimit || maxLimit < nickNameLength) { - this.carbonMessages.nicknameErrorCharacterLimit(sender, parsedNick.get(), minLimit, maxLimit); + final int minLength = this.config.primaryConfig().nicknameMinLength(); + final int maxLength = this.config.primaryConfig().nicknameMaxLength(); + 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/PrimaryConfig.java b/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java index bcfc7d256..61557626e 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 @@ -94,10 +94,10 @@ public class PrimaryConfig { private boolean useCarbonNicknames = true; @Comment("Minimum number of characters in nickname.") - private int nicknameMinLimit = 3; + private int nicknameMinLength = 3; @Comment("Maximum number of characters in nickname.") - private int nicknameMaxLimit = 16; + private int nicknameMaxLength = 16; @Comment("Whether Carbon should check for updates using the GitHub API on startup.") private boolean updateChecker = true; @@ -182,12 +182,12 @@ public boolean useCarbonNicknames() { return this.useCarbonNicknames; } - public int nicknameMinLimit() { - return this.nicknameMinLimit; + public int nicknameMinLength() { + return this.nicknameMinLength; } - public int nicknameMaxLimit() { - return this.nicknameMaxLimit; + public int nicknameMaxLength() { + return this.nicknameMaxLength; } public boolean updateChecker() { diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index 85f8a5845..e08afb816 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -184,8 +184,8 @@ void whisperTargetNotSet( void nicknameErrorCharacterLimit( final Audience audience, final Component nickname, - @Placeholder("min_limit") final int minLimit, - @Placeholder("max_limit") final int maxLimit + final int minLength, + final int maxLength ); @Message("nickname.show.others") diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 8ac88f59c..77a5d4c2c 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -97,7 +97,7 @@ nickname.show.others.unset= does not have a nickname set nickname.show.others='s nickname is nickname.show.unset=You do not have a nickname set nickname.show=Your nickname is -nickname.error.character_limit=Nickname "" has exceeded the character limit. Must be set to ~ characters. +nickname.error.character_limit=Nickname "" has exceeded the character limit. Must be set to ~ characters. reply.target.missing=You have no-one to reply to reply.target.self=You cannot whisper to yourself whisper.console=[] -> []