From dc5c80d8123afd4ff8997078901f2133ba3c6052 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Fri, 1 Apr 2022 19:30:37 +0200 Subject: [PATCH] clean up PlayerCommandPreprocess listener (#3557) --- .../bukkit/listener/PlayerEventListener.java | 112 +++++++++--------- .../com/plotsquared/core/util/RegExUtil.java | 1 + 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java index 8628daa02..06110b633 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -76,7 +76,6 @@ import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.PremiumVerification; -import com.plotsquared.core.util.RegExUtil; import com.plotsquared.core.util.entity.EntityCategories; import com.plotsquared.core.util.task.TaskManager; 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) public void playerCommand(PlayerCommandPreprocessEvent event) { - String msg = event.getMessage().toLowerCase().replaceAll("/", "").trim(); + String msg = event.getMessage().replace("/", "").toLowerCase(Locale.ROOT).trim(); if (msg.isEmpty()) { return; } @@ -240,11 +238,9 @@ public class PlayerEventListener extends PlotListener implements Listener { String[] parts = msg.split(" "); Plot plot = plotPlayer.getCurrentPlot(); // Check WorldEdit - switch (parts[0].toLowerCase()) { + switch (parts[0]) { case "up": - case "/up": case "worldedit:up": - case "worldedit:/up": if (plot == null || (!plot.isAdded(plotPlayer.getUUID()) && !Permissions .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_OTHER, true))) { event.setCancelled(true); @@ -258,63 +254,67 @@ public class PlayerEventListener extends PlotListener implements Listener { List blockedCommands = plot != null ? plot.getFlag(BlockedCmdsFlag.class) : area.getFlag(BlockedCmdsFlag.class); - if (!blockedCommands.isEmpty() && !Permissions - .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_INTERACT_BLOCKED_CMDS)) { - String part = parts[0]; - if (parts[0].contains(":")) { - part = parts[0].split(":")[1]; - msg = msg.replace(parts[0].split(":")[0] + ':', ""); - } - String s1 = part; - List aliases = new ArrayList<>(); - for (HelpTopic cmdLabel : Bukkit.getServer().getHelpMap().getHelpTopics()) { - if (part.equals(cmdLabel.getName())) { - break; - } - String label = cmdLabel.getName().replaceFirst("/", ""); - if (aliases.contains(label)) { - continue; - } - PluginCommand p; - if ((p = Bukkit.getPluginCommand(label)) != null) { - for (String a : p.getAliases()) { - if (aliases.contains(a)) { - continue; - } - aliases.add(a); - a = a.replaceFirst("/", ""); - if (!a.equals(label) && a.equals(part)) { - part = label; - break; - } - } - } - } - if (!s1.equals(part)) { - msg = msg.replace(s1, part); - } - for (String s : blockedCommands) { - Pattern pattern; - if (!RegExUtil.compiledPatterns.containsKey(s)) { - RegExUtil.compiledPatterns.put(s, pattern = Pattern.compile(s)); + if (blockedCommands.isEmpty()) { + return; + } + 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 { - pattern = RegExUtil.compiledPatterns.get(s); + perm = "plots.admin.command.blocked-cmds.road"; } - 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; + } + } + } + + private String replaceAliases(String msg, String part) { + String s1 = part; + Set aliases = new HashSet<>(); + for (HelpTopic cmdLabel : Bukkit.getServer().getHelpMap().getHelpTopics()) { + if (part.equals(cmdLabel.getName())) { + break; + } + String label = cmdLabel.getName().replaceFirst("/", ""); + if (aliases.contains(label)) { + continue; + } + PluginCommand p = Bukkit.getPluginCommand(label); + if (p != null) { + for (String a : p.getAliases()) { + if (aliases.contains(a)) { + continue; } - if (!Permissions.hasPermission(plotPlayer, perm)) { - plotPlayer.sendMessage(TranslatableCaption.of("blockedcmds.command_blocked")); - event.setCancelled(true); + aliases.add(a); + a = a.replaceFirst("/", ""); + if (!a.equals(label) && a.equals(part)) { + part = label; + break; } - return; } } } + if (!s1.equals(part)) { + msg = msg.replace(s1, part); + } + return msg; } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) diff --git a/Core/src/main/java/com/plotsquared/core/util/RegExUtil.java b/Core/src/main/java/com/plotsquared/core/util/RegExUtil.java index b45f1af68..9f504fdf0 100644 --- a/Core/src/main/java/com/plotsquared/core/util/RegExUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/RegExUtil.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; +@Deprecated(since = "TODO", forRemoval = true) public class RegExUtil { public static Map compiledPatterns;