From 925fe4a4b2256839bcfa1ec0a433709a5e6f6142 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 27 Mar 2020 19:53:00 -0400 Subject: [PATCH] Revert command blocker as the one pushed was not working whatsoever --- .../blocking/command/CommandBlocker.java | 42 ++++++++++++++----- .../blocking/command/CommandBlockerEntry.java | 10 ++++- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlocker.java b/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlocker.java index 2c1666ef..55243c78 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlocker.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlocker.java @@ -71,20 +71,21 @@ public class CommandBlocker extends FreedomService final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]); final CommandBlockerAction action = CommandBlockerAction.fromToken(parts[1]); - String strCommand = parts[2].toLowerCase().substring(1); + String commandName = parts[2].toLowerCase().substring(1); final String message = (parts.length > 3 ? parts[3] : null); - if (rank == null || action == null || strCommand == null || strCommand.isEmpty()) + if (rank == null || action == null || commandName == null || commandName.isEmpty()) { FLog.warning("Invalid command blocker entry: " + rawEntry); continue; } - final String[] commandParts = strCommand.split(" "); - String commandName = strCommand.toLowerCase(); + final String[] commandParts = commandName.split(" "); + String subCommand = null; if (commandParts.length > 1) { commandName = commandParts[0]; + subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).trim().toLowerCase(); } final Command command = commandMap.getCommand(commandName); @@ -94,21 +95,25 @@ public class CommandBlocker extends FreedomService { unknownCommands.add(commandName); } - - if (entryList.containsKey(strCommand)) + else { - FLog.warning("Not blocking: /" + strCommand + " - Duplicate entry exists!"); + commandName = command.getName().toLowerCase(); + } + + if (entryList.containsKey(commandName)) + { + FLog.warning("Not blocking: /" + commandName + " - Duplicate entry exists!"); continue; } - final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, strCommand, message); + final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, commandName, subCommand, message); entryList.put(blockedCommandEntry.getCommand(), blockedCommandEntry); if (command != null) { for (String alias : command.getAliases()) { - entryList.put(strCommand.replaceFirst(commandName, alias), blockedCommandEntry); + entryList.put(alias.toLowerCase(), blockedCommandEntry); } } } @@ -148,7 +153,6 @@ public class CommandBlocker extends FreedomService // Format command = command.toLowerCase().trim(); command = command.startsWith("/") ? command.substring(1) : command; - command = command.replaceAll("\"", ""); // Check for plugin specific commands final String[] commandParts = command.split(" "); @@ -180,13 +184,29 @@ public class CommandBlocker extends FreedomService return true; } + // Obtain sub command, if it exists + String subCommand = null; + if (commandParts.length > 1) + { + subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).toLowerCase(); + } + // Obtain entry - final CommandBlockerEntry entry = entryList.get(command); + final CommandBlockerEntry entry = entryList.get(commandParts[0]); if (entry == null) { return false; } + // Validate sub command + if (entry.getSubCommand() != null) + { + if (subCommand == null || !subCommand.startsWith(entry.getSubCommand())) + { + return false; + } + } + if (entry.getRank().hasPermission(sender)) { return false; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlockerEntry.java b/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlockerEntry.java index 6a4f7f82..deb43964 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlockerEntry.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/blocking/command/CommandBlockerEntry.java @@ -18,13 +18,21 @@ public class CommandBlockerEntry @Getter private final String command; @Getter + private final String subCommand; + @Getter private final String message; public CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String message) + { + this(rank, action, command, null, message); + } + + public CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String subCommand, String message) { this.rank = rank; this.action = action; this.command = command; + this.subCommand = ((subCommand == null) ? null : subCommand.toLowerCase().trim()); this.message = ((message == null || message.equals("_")) ? "That command is blocked." : message); } @@ -43,4 +51,4 @@ public class CommandBlockerEntry } FUtil.playerMsg(sender, FUtil.colorize(message)); } -} +} \ No newline at end of file