From a552e585dcaeffafa0c3b722a8ea27a6528b1e4b Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" Date: Thu, 18 Nov 2021 15:31:22 +0100 Subject: [PATCH] feat(nicknames): allow blocking names with regular expressions #14 --- .../java/net/silthus/chat/commands/NicknameCommands.java | 9 ++++++--- src/main/resources/config.default.yml | 4 +++- src/main/resources/config.yml | 4 +++- .../net/silthus/chat/commands/NicknameCommandsTest.java | 6 ++++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/silthus/chat/commands/NicknameCommands.java b/src/main/java/net/silthus/chat/commands/NicknameCommands.java index df7e8b1ed..7973d965d 100644 --- a/src/main/java/net/silthus/chat/commands/NicknameCommands.java +++ b/src/main/java/net/silthus/chat/commands/NicknameCommands.java @@ -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.*; @@ -83,8 +83,11 @@ private void validateNickname(String name) { } private void validateBlockedNicknames(String name) { - List 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); } diff --git a/src/main/resources/config.default.yml b/src/main/resources/config.default.yml index 721230140..a371d534b 100644 --- a/src/main/resources/config.default.yml +++ b/src/main/resources/config.default.yml @@ -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. diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 721230140..a371d534b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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. diff --git a/src/test/java/net/silthus/chat/commands/NicknameCommandsTest.java b/src/test/java/net/silthus/chat/commands/NicknameCommandsTest.java index 567c61930..8da4f5eaf 100644 --- a/src/test/java/net/silthus/chat/commands/NicknameCommandsTest.java +++ b/src/test/java/net/silthus/chat/commands/NicknameCommandsTest.java @@ -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");