clean up PlayerCommandPreprocess listener (#3557)

This commit is contained in:
Hannes Greule 2022-04-01 19:30:37 +02:00 committed by GitHub
parent 96e9a61e7c
commit dc5c80d812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,63 +254,67 @@ 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(":")) { if (Permissions.hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_INTERACT_BLOCKED_CMDS)) {
part = parts[0].split(":")[1]; return;
msg = msg.replace(parts[0].split(":")[0] + ':', ""); }
} // When using namespaced commands, we're not interested in the namespace
String s1 = part; String part = parts[0];
List<String> aliases = new ArrayList<>(); if (part.contains(":")) {
for (HelpTopic cmdLabel : Bukkit.getServer().getHelpMap().getHelpTopics()) { String[] namespaced = part.split(":");
if (part.equals(cmdLabel.getName())) { part = namespaced[1];
break; msg = msg.substring(namespaced[0].length() + 1);
} }
String label = cmdLabel.getName().replaceFirst("/", ""); msg = replaceAliases(msg, part);
if (aliases.contains(label)) { for (String blocked : blockedCommands) {
continue; if (blocked.equalsIgnoreCase(msg)) {
} String perm;
PluginCommand p; if (plot != null && plot.isAdded(plotPlayer.getUUID())) {
if ((p = Bukkit.getPluginCommand(label)) != null) { perm = "plots.admin.command.blocked-cmds.shared";
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));
} else { } else {
pattern = RegExUtil.compiledPatterns.get(s); perm = "plots.admin.command.blocked-cmds.road";
} }
if (pattern.matcher(msg).matches()) { if (!Permissions.hasPermission(plotPlayer, perm)) {
String perm; plotPlayer.sendMessage(TranslatableCaption.of("blockedcmds.command_blocked"));
if (plot != null && plot.isAdded(plotPlayer.getUUID())) { event.setCancelled(true);
perm = "plots.admin.command.blocked-cmds.shared"; }
} else { return;
perm = "plots.admin.command.blocked-cmds.road"; }
}
}
private String replaceAliases(String msg, String part) {
String s1 = part;
Set<String> 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)) { aliases.add(a);
plotPlayer.sendMessage(TranslatableCaption.of("blockedcmds.command_blocked")); a = a.replaceFirst("/", "");
event.setCancelled(true); 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) @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;