clean up PlayerCommandPreprocess listener

This commit is contained in:
SirYwell 2022-03-24 09:44:35 +01:00
parent efc2083798
commit 676ca9fdf1
2 changed files with 57 additions and 56 deletions

View File

@ -76,7 +76,6 @@ import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.PremiumVerification; import com.plotsquared.core.util.PremiumVerification;
import com.plotsquared.core.util.RegExUtil;
import com.plotsquared.core.util.entity.EntityCategories; import com.plotsquared.core.util.entity.EntityCategories;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime; import com.plotsquared.core.util.task.TaskTime;
@ -223,10 +222,9 @@ public class PlayerEventListener extends PlotListener implements Listener {
} }
} }
@SuppressWarnings("StringSplitter")
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void playerCommand(PlayerCommandPreprocessEvent event) { public void playerCommand(PlayerCommandPreprocessEvent event) {
String msg = event.getMessage().toLowerCase().replaceAll("/", "").trim(); String msg = event.getMessage().replace("/", "").toLowerCase(Locale.ROOT).trim();
if (msg.isEmpty()) { if (msg.isEmpty()) {
return; return;
} }
@ -240,11 +238,9 @@ public class PlayerEventListener extends PlotListener implements Listener {
String[] parts = msg.split(" "); String[] parts = msg.split(" ");
Plot plot = plotPlayer.getCurrentPlot(); Plot plot = plotPlayer.getCurrentPlot();
// Check WorldEdit // Check WorldEdit
switch (parts[0].toLowerCase()) { switch (parts[0]) {
case "up": case "up":
case "/up":
case "worldedit:up": case "worldedit:up":
case "worldedit:/up":
if (plot == null || (!plot.isAdded(plotPlayer.getUUID()) && !Permissions if (plot == null || (!plot.isAdded(plotPlayer.getUUID()) && !Permissions
.hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_OTHER, true))) { .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_OTHER, true))) {
event.setCancelled(true); event.setCancelled(true);
@ -258,15 +254,40 @@ public class PlayerEventListener extends PlotListener implements Listener {
List<String> blockedCommands = plot != null ? List<String> blockedCommands = plot != null ?
plot.getFlag(BlockedCmdsFlag.class) : plot.getFlag(BlockedCmdsFlag.class) :
area.getFlag(BlockedCmdsFlag.class); area.getFlag(BlockedCmdsFlag.class);
if (!blockedCommands.isEmpty() && !Permissions if (blockedCommands.isEmpty()) {
.hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_INTERACT_BLOCKED_CMDS)) { return;
String part = parts[0];
if (parts[0].contains(":")) {
part = parts[0].split(":")[1];
msg = msg.replace(parts[0].split(":")[0] + ':', "");
} }
if (Permissions.hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_INTERACT_BLOCKED_CMDS)) {
return;
}
// When using namespaced commands, we're not interested in the namespace
String part = parts[0];
if (part.contains(":")) {
String[] namespaced = part.split(":");
part = namespaced[1];
msg = msg.substring(namespaced[0].length() + 1);
}
msg = replaceAliases(msg, part);
for (String blocked : blockedCommands) {
if (blocked.equalsIgnoreCase(msg)) {
String perm;
if (plot != null && plot.isAdded(plotPlayer.getUUID())) {
perm = "plots.admin.command.blocked-cmds.shared";
} else {
perm = "plots.admin.command.blocked-cmds.road";
}
if (!Permissions.hasPermission(plotPlayer, perm)) {
plotPlayer.sendMessage(TranslatableCaption.of("blockedcmds.command_blocked"));
event.setCancelled(true);
}
return;
}
}
}
private String replaceAliases(String msg, String part) {
String s1 = part; String s1 = part;
List<String> aliases = new ArrayList<>(); Set<String> aliases = new HashSet<>();
for (HelpTopic cmdLabel : Bukkit.getServer().getHelpMap().getHelpTopics()) { for (HelpTopic cmdLabel : Bukkit.getServer().getHelpMap().getHelpTopics()) {
if (part.equals(cmdLabel.getName())) { if (part.equals(cmdLabel.getName())) {
break; break;
@ -275,8 +296,8 @@ public class PlayerEventListener extends PlotListener implements Listener {
if (aliases.contains(label)) { if (aliases.contains(label)) {
continue; continue;
} }
PluginCommand p; PluginCommand p = Bukkit.getPluginCommand(label);
if ((p = Bukkit.getPluginCommand(label)) != null) { if (p != null) {
for (String a : p.getAliases()) { for (String a : p.getAliases()) {
if (aliases.contains(a)) { if (aliases.contains(a)) {
continue; continue;
@ -293,28 +314,7 @@ public class PlayerEventListener extends PlotListener implements Listener {
if (!s1.equals(part)) { if (!s1.equals(part)) {
msg = msg.replace(s1, part); msg = msg.replace(s1, part);
} }
for (String s : blockedCommands) { return msg;
Pattern pattern;
if (!RegExUtil.compiledPatterns.containsKey(s)) {
RegExUtil.compiledPatterns.put(s, pattern = Pattern.compile(s));
} else {
pattern = RegExUtil.compiledPatterns.get(s);
}
if (pattern.matcher(msg).matches()) {
String perm;
if (plot != null && plot.isAdded(plotPlayer.getUUID())) {
perm = "plots.admin.command.blocked-cmds.shared";
} else {
perm = "plots.admin.command.blocked-cmds.road";
}
if (!Permissions.hasPermission(plotPlayer, perm)) {
plotPlayer.sendMessage(TranslatableCaption.of("blockedcmds.command_blocked"));
event.setCancelled(true);
}
return;
}
}
}
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)

View File

@ -29,6 +29,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@Deprecated(since = "TODO", forRemoval = true)
public class RegExUtil { public class RegExUtil {
public static Map<String, Pattern> compiledPatterns; public static Map<String, Pattern> compiledPatterns;