-
-
Notifications
You must be signed in to change notification settings - Fork 9
GH-343 fix: block namespaced commands during combat and add regression test #343
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package com.eternalcode.combat.fight.controller; | ||
|
|
||
| import com.eternalcode.combat.WhitelistBlacklistMode; | ||
| import com.eternalcode.combat.config.implementation.PluginConfig; | ||
| import com.eternalcode.combat.fight.FightManager; | ||
| import com.eternalcode.combat.notification.NoticeService; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import java.util.regex.Pattern; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.EventPriority; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.player.PlayerCommandPreprocessEvent; | ||
|
|
||
| public class FightCommandController implements Listener { | ||
|
|
||
| private static final char SLASH = '/'; | ||
| private static final char NAMESPACE_SEPARATOR = ':'; | ||
| private static final char SPACE = ' '; | ||
| private static final String EMPTY = ""; | ||
| private static final String SINGLE_SPACE = " "; | ||
| private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+"); | ||
|
|
||
| private final FightManager fightManager; | ||
| private final NoticeService noticeService; | ||
| private final PluginConfig config; | ||
|
|
||
| public FightCommandController(FightManager fightManager, NoticeService noticeService, PluginConfig config) { | ||
| this.fightManager = fightManager; | ||
| this.noticeService = noticeService; | ||
| this.config = config; | ||
| } | ||
|
|
||
| static String normalizeCommand(String command) { | ||
| if (command == null || command.isBlank()) { | ||
| return EMPTY; | ||
| } | ||
|
|
||
| String normalized = command.strip(); | ||
|
|
||
| if (normalized.charAt(0) == SLASH) { | ||
| normalized = normalized.substring(1).stripLeading(); | ||
| } | ||
|
|
||
| if (normalized.isEmpty()) { | ||
| return EMPTY; | ||
| } | ||
|
|
||
| int colonIndex = normalized.indexOf(NAMESPACE_SEPARATOR); | ||
| int firstSpaceIndex = normalized.indexOf(SPACE); | ||
|
|
||
| if (colonIndex >= 0 && (firstSpaceIndex < 0 || colonIndex < firstSpaceIndex)) { | ||
| normalized = normalized.substring(colonIndex + 1); | ||
| } | ||
|
|
||
| return WHITESPACE_PATTERN.matcher(normalized).replaceAll(SINGLE_SPACE); | ||
| } | ||
|
|
||
| static boolean matches(String command, List<String> restrictedCommands) { | ||
| if (command.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| for (String restrictedCommand : restrictedCommands) { | ||
| String normalizedRestricted = normalizeCommand(restrictedCommand); | ||
|
|
||
| if (normalizedRestricted.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| boolean exactMatch = command.equalsIgnoreCase(normalizedRestricted); | ||
| boolean prefixMatch = command.regionMatches(true, 0, normalizedRestricted, 0, normalizedRestricted.length()) | ||
| && command.length() > normalizedRestricted.length() | ||
| && command.charAt(normalizedRestricted.length()) == SPACE; | ||
|
|
||
| if (exactMatch || prefixMatch) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
Comment on lines
+35
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would consider move that logic to other class to improve SRP. Currently this class is not only "controller"
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah maybe event to util? -> could be used in pull Add death commands |
||
|
|
||
| @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) | ||
| void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { | ||
| Player player = event.getPlayer(); | ||
| UUID uniqueId = player.getUniqueId(); | ||
|
|
||
| if (!this.fightManager.isInCombat(uniqueId)) { | ||
| return; | ||
| } | ||
|
|
||
| String command = normalizeCommand(event.getMessage()); | ||
|
|
||
| if (command.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| boolean matched = matches(command, this.config.commands.restrictedCommands); | ||
|
|
||
| WhitelistBlacklistMode mode = this.config.commands.commandRestrictionMode; | ||
|
|
||
| if (!mode.shouldBlock(matched)) { | ||
| return; | ||
| } | ||
|
|
||
| event.setCancelled(true); | ||
| this.noticeService.create() | ||
| .player(uniqueId) | ||
| .notice(this.config.messagesSettings.commandDisabledDuringCombat) | ||
| .send(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.eternalcode.combat.fight.controller; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need tests for that? |
||
| class FightCommandControllerTest { | ||
|
|
||
| @Test | ||
| void normalizeCommand_stripsSlashAndNamespace_collapsesWhitespace() { | ||
| assertEquals("tp Steve", FightCommandController.normalizeCommand("/minecraft:tp Steve")); | ||
| assertEquals("home set 1", FightCommandController.normalizeCommand("/home set 1")); | ||
| assertEquals("spawn", FightCommandController.normalizeCommand("/spawn")); | ||
| assertEquals("", FightCommandController.normalizeCommand("/ ")); | ||
| assertEquals("", FightCommandController.normalizeCommand(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void matches_exactAndPrefixMatch_caseInsensitive() { | ||
| List<String> restricted = List.of("home set", "tp", " ", "/minecraft:msg admin"); | ||
|
|
||
| assertTrue(FightCommandController.matches("home set", restricted)); | ||
| assertTrue(FightCommandController.matches("home set 1", restricted)); | ||
|
|
||
| assertTrue(FightCommandController.matches("TP", restricted)); | ||
| assertTrue(FightCommandController.matches("tp Steve", restricted)); | ||
|
|
||
| assertTrue(FightCommandController.matches("msg admin", restricted)); | ||
| assertTrue(FightCommandController.matches("msg admin hello", restricted)); | ||
|
|
||
| assertFalse(FightCommandController.matches("home setup", restricted)); | ||
| assertFalse(FightCommandController.matches("tpper", restricted)); | ||
| assertFalse(FightCommandController.matches("", restricted)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.