From 1c3864e608a578528b145d87d76014bf4b55a676 Mon Sep 17 00:00:00 2001 From: Allink <44676012+allinkdev@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:00:32 +0100 Subject: [PATCH] Limit selectors to 3 in broadcast commands (#345) --- .../extras/modules/server/ServerCommand.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java b/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java index 9d9c76a..38df39e 100644 --- a/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java +++ b/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java @@ -19,6 +19,7 @@ import java.util.regex.Pattern; public final class ServerCommand implements Listener { private static final Pattern AS_AT_PATTERN = Pattern.compile( "\\b(as|at|facing entity) @[ae]\\b"); + private static final Pattern SELECTOR_PATTERN = Pattern.compile("(?>\\s)*@[aepsr](?>\\s)*"); private static final Logger LOGGER = JavaPlugin.getPlugin(Main.class).getLogger(); public static boolean checkExecuteCommand(final String cmd) { @@ -50,6 +51,19 @@ public final class ServerCommand implements Listener { ); } + private static String checkSelectors(final String[] arr) { + final String[] args = Arrays.copyOfRange(arr, 1, arr.length); + final String str = String.join(" ", args); + final Matcher matcher = SELECTOR_PATTERN.matcher(str); + final long count = matcher.results().count(); + + if (count > 3) { + return "cancel"; + } + + return null; + } + public static String checkCommand(final CommandSender sender, final String command, final boolean isConsoleCommand) { return checkCommand(sender, command, isConsoleCommand, 1); @@ -146,6 +160,19 @@ public final class ServerCommand implements Listener { } } break; + case "/minecraft:ban": + case "/ban": + case "/minecraft:kick": + case "/kick": + case "/minecraft:tell": + case "/tell": + case "/minecraft:msg": + case "/msg": + case "/minecraft:w": + case "/w": + case "/minecraft:say": + case "/say": + return checkSelectors(arr); case "/minecraft:spreadplayers": case "/spreadplayers": if (arr.length == 7 && (arr[6].contains("@e") || arr[6].contains("@a"))) {