Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nickname character limit. #293

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
jpenilla marked this conversation as resolved.
Show resolved Hide resolved
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;
NamiUni marked this conversation as resolved.
Show resolved Hide resolved

@Comment("Maximum number of characters in nickname.")
private int nicknameMaxLimit = 16;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for the default setting of 16 characters is that MinecraftID can be set to a maximum of 16 characters.


@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,
NamiUni marked this conversation as resolved.
Show resolved Hide resolved
@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