Skip to content

Commit

Permalink
Add nickname character limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
NamiUni committed Sep 15, 2023
1 parent 128a9de commit 654964d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

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

Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locale/messages-en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ nickname.show.others.unset=<target><red> does not have a nickname set
nickname.show.others=<target><green>'s nickname is </green><nickname>
nickname.show.unset=<red>You do not have a nickname set
nickname.show=<green>Your nickname is </green><nickname>
nickname.error.character_limit=<red>Nickname "<nickname>" has exceeded the character limit. Must be set to <min_limit>~<max_limit> characters.
reply.target.missing=<red>You have no-one to reply to
reply.target.self=<red>You cannot whisper to yourself
whisper.console=<gold>[<green><sender_display_name></green>] -> [<green><recipient_display_name></green>] <message>
Expand Down

0 comments on commit 654964d

Please sign in to comment.