Improved execute as/at/facing detection (#298)

* Improved execute as/at/facing detection

* Made atAsPattern a class attribute
This commit is contained in:
hhhzzzsss 2021-07-08 19:36:46 -05:00 committed by GitHub
parent 4f1d2a0eab
commit e22044a158
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,8 @@
package pw.kaboom.extras.modules.server;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.block.CommandBlock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
@ -9,6 +12,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerCommandEvent;
public final class ServerCommand implements Listener {
private static Pattern asAtPattern = Pattern.compile("\\b(as|at|facing entity) @[ae]\\b");
public static boolean checkExecuteCommand(final String cmd) {
return ("execute".equalsIgnoreCase(cmd)
|| "banlist".equalsIgnoreCase(cmd)
@ -77,6 +82,13 @@ public final class ServerCommand implements Listener {
case "/execute":
if (arr.length >= 2) {
int asAtCount = 0;
Matcher asAtMatcher = asAtPattern.matcher(command.toLowerCase());
while (asAtMatcher.find()) {
asAtCount++;
}
if (asAtCount >= 2) {
return "cancel";
}
for (int i = 1; i < arr.length; i++) {
if ("run".equalsIgnoreCase(arr[i])) {
@ -107,14 +119,6 @@ public final class ServerCommand implements Listener {
}
break;
}
if ("as".equalsIgnoreCase(arr[i]) || "at".equalsIgnoreCase(arr[i]) || "facing".equalsIgnoreCase(arr[i])) {
asAtCount++;
}
}
if (asAtCount >= 2) {
return "cancel";
}
}
break;