Limit selectors to 3 in broadcast commands (#345)

This commit is contained in:
Allink 2023-06-03 21:00:32 +01:00 committed by GitHub
parent 7fabfdae1a
commit 1c3864e608
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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"))) {