Refactor op command detection

This commit is contained in:
Luck 2020-08-01 10:20:49 +01:00
parent 92e008ff0a
commit 1eab127f50
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 14 additions and 22 deletions

View File

@ -38,7 +38,11 @@ import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.event.server.RemoteServerCommandEvent;
import org.bukkit.event.server.ServerCommandEvent;
import java.util.regex.Pattern;
public class BukkitPlatformListener implements Listener {
private static final Pattern OP_COMMAND_PATTERN = Pattern.compile("^/?(\\w+:)?(deop|op)( .*)?$");
private final LPBukkitPlugin plugin;
public BukkitPlatformListener(LPBukkitPlugin plugin) {
@ -60,8 +64,8 @@ public class BukkitPlatformListener implements Listener {
handleCommand(e.getSender(), e.getCommand().toLowerCase(), e);
}
private void handleCommand(CommandSender sender, String s, Cancellable event) {
if (s.isEmpty()) {
private void handleCommand(CommandSender sender, String cmdLine, Cancellable event) {
if (cmdLine.isEmpty()) {
return;
}
@ -69,15 +73,7 @@ public class BukkitPlatformListener implements Listener {
return;
}
if (s.charAt(0) == '/') {
s = s.substring(1);
}
if (s.contains(":")) {
s = s.substring(s.indexOf(':') + 1);
}
if (s.equals("op") || s.startsWith("op ") || s.equals("deop") || s.startsWith("deop ")) {
if (OP_COMMAND_PATTERN.matcher(cmdLine).matches()) {
event.setCancelled(true);
sender.sendMessage(Message.OP_DISABLED.asString(this.plugin.getLocaleManager()));
}

View File

@ -37,7 +37,11 @@ import cn.nukkit.event.player.PlayerCommandPreprocessEvent;
import cn.nukkit.event.server.RemoteServerCommandEvent;
import cn.nukkit.event.server.ServerCommandEvent;
import java.util.regex.Pattern;
public class NukkitPlatformListener implements Listener {
private static final Pattern OP_COMMAND_PATTERN = Pattern.compile("^/?(\\w+:)?(deop|op)( .*)?$");
private final LPNukkitPlugin plugin;
public NukkitPlatformListener(LPNukkitPlugin plugin) {
@ -59,8 +63,8 @@ public class NukkitPlatformListener implements Listener {
handleCommand(e.getSender(), e.getCommand().toLowerCase(), e);
}
private void handleCommand(CommandSender sender, String s, Cancellable event) {
if (s.isEmpty()) {
private void handleCommand(CommandSender sender, String cmdLine, Cancellable event) {
if (cmdLine.isEmpty()) {
return;
}
@ -68,15 +72,7 @@ public class NukkitPlatformListener implements Listener {
return;
}
if (s.charAt(0) == '/') {
s = s.substring(1);
}
if (s.contains(":")) {
s = s.substring(s.indexOf(':') + 1);
}
if (s.equals("op") || s.startsWith("op ") || s.equals("deop") || s.startsWith("deop ")) {
if (OP_COMMAND_PATTERN.matcher(cmdLine).matches()) {
event.setCancelled(true);
sender.sendMessage(Message.OP_DISABLED.asString(this.plugin.getLocaleManager()));
}