Skip to content

Commit

Permalink
feat(nicknames): allow blocking names with regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Nov 18, 2021
1 parent abd945b commit a552e58
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/main/java/net/silthus/chat/commands/NicknameCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static net.kyori.adventure.text.Component.text;
import static net.silthus.chat.Constants.Language.Commands.Nicknames.*;
Expand Down Expand Up @@ -83,8 +83,11 @@ private void validateNickname(String name) {
}

private void validateBlockedNicknames(String name) {
List<String> blockedNickNames = plugin.getPluginConfig().player().blockedNickNames();
if (blockedNickNames.stream().anyMatch(name::equalsIgnoreCase))
boolean nicknameIsBlocked = plugin.getPluginConfig().player().blockedNickNames()
.stream().map(s -> Pattern.compile(s, Pattern.CASE_INSENSITIVE))
.map(pattern -> pattern.matcher(name))
.anyMatch(Matcher::matches);
if (nicknameIsBlocked)
throw new ConditionFailedException(key(BLOCKED), "{nickname}", name);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ players:
# Set a regular expression of allowed nicknames.
# This defaults to the allowed Minecraft nicknames.
nickname_pattern: "[a-zA-Z0-9_]{3,16}"
# Players without the schat.nickname.allow-blocked cannot use these nicknames.
# Players without the schat.nickname.set.blocked cannot use these nicknames.
# The names are matched case-insensitive. nOtcH will be blocked as well.
# You can also use regular expressions to block nicknames. The expressions are matched case-insensitive as well.
blocked_nicknames:
- Notch
- .*admin.*
# You can fine tune the behaviour of the console chat in this section.
console:
# Set the name players see when chatting from the console.
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ players:
# Set a regular expression of allowed nicknames.
# This defaults to the allowed Minecraft nicknames.
nickname_pattern: "[a-zA-Z0-9_]{3,16}"
# Players without the schat.nickname.allow-blocked cannot use these nicknames.
# Players without the schat.nickname.set.blocked cannot use these nicknames.
# The names are matched case-insensitive. nOtcH will be blocked as well.
# You can also use regular expressions to block nicknames. The expressions are matched case-insensitive as well.
blocked_nicknames:
- Notch
- .*admin.*
# You can fine tune the behaviour of the console chat in this section.
console:
# Set the name players see when chatting from the console.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ void nick_blocksNamesNotMatchingPattern() {
assertThat(getLastMessage(player)).contains("is not valid");
}

@Test
void nick_isBlockedIfMatchesBlockPattern() {
player.performCommand("nickname Administrator");
assertThat(getLastMessage(player)).contains("cannot be used");
}

@Test
void nick_blocksNamesInBlacklist() {
player.performCommand("nick nOtcH");
Expand Down

0 comments on commit a552e58

Please sign in to comment.