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=[] -> []