Skip to content

Commit

Permalink
Don't allow setting nicknames to other online players usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
Draycia committed Dec 20, 2024
1 parent dad284b commit 7e0fb78
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package net.draycia.carbon.common.command.commands;

import com.google.inject.Inject;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
import net.draycia.carbon.common.command.CommandSettings;
Expand All @@ -44,18 +45,21 @@
@DefaultQualifier(NonNull.class)
public final class NicknameCommand extends CarbonCommand {

private final CarbonChat carbonChat;
private final CommandManager<Commander> commandManager;
private final CarbonMessages carbonMessages;
private final ParserFactory parserFactory;
private final ConfigManager config;

@Inject
public NicknameCommand(
final CarbonChat carbonChat,
final CommandManager<Commander> commandManager,
final CarbonMessages carbonMessages,
final ParserFactory parserFactory,
final ConfigManager config
) {
this.carbonChat = carbonChat;
this.commandManager = commandManager;
this.carbonMessages = carbonMessages;
this.parserFactory = parserFactory;
Expand Down Expand Up @@ -146,7 +150,19 @@ private void applyNickname(final Commander sender, final CarbonPlayer target, fi
return;
}

target.nickname(parsedNick);
if (sender instanceof PlayerCommander playerCommander) {
if (playerCommander.carbonPlayer().username().equalsIgnoreCase(plainNick)) {
this.resetNickname(sender, playerCommander.carbonPlayer());
return;
}

for (final CarbonPlayer player : this.carbonChat.server().players()) {
if (player.username().equalsIgnoreCase(plainNick)) {
this.carbonMessages.nicknameErrorImpersonation(sender, parsedNick);
return;
}
}
}

if (sender instanceof PlayerCommander playerCommander
&& playerCommander.carbonPlayer().uuid().equals(target.uuid())) {
Expand All @@ -157,6 +173,8 @@ private void applyNickname(final Commander sender, final CarbonPlayer target, fi
this.carbonMessages.nicknameSet(target, parsedNick);
this.carbonMessages.nicknameSetOthers(sender, target.username(), parsedNick);
}

target.nickname(parsedNick);
}

private void checkOwnNickname(final CarbonPlayer sender) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ void nicknameErrorCharacterLimit(
@Message("nickname.error.filter")
void nicknameErrorFilter(final Audience audience, final Component nickname);

@Message("nickname.error.impersonation")
void nicknameErrorImpersonation(final Audience audience, final Component nickname);

@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 @@ -131,6 +131,7 @@ 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_length>~<max_length> characters.
nickname.error.blacklist=<red>Nickname "<nickname>" is not allowed. Please choose another name.
nickname.error.filter=<red>Nicknames must be alphanumeric!
nickname.error.impersonation=<red>You cannot impersonate others!
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 7e0fb78

Please # to comment.