The command blocker is now functioning as per normal (#187)

This commit is contained in:
Nathan Curran 2020-02-27 14:31:21 +11:00 committed by GitHub
parent ce804ac23b
commit ecc907b535
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 39 deletions

View File

@ -71,21 +71,20 @@ public class CommandBlocker extends FreedomService
final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]); final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]);
final CommandBlockerAction action = CommandBlockerAction.fromToken(parts[1]); final CommandBlockerAction action = CommandBlockerAction.fromToken(parts[1]);
String commandName = parts[2].toLowerCase().substring(1); String strCommand = parts[2].toLowerCase().substring(1);
final String message = (parts.length > 3 ? parts[3] : null); final String message = (parts.length > 3 ? parts[3] : null);
if (rank == null || action == null || commandName == null || commandName.isEmpty()) if (rank == null || action == null || strCommand == null || strCommand.isEmpty())
{ {
FLog.warning("Invalid command blocker entry: " + rawEntry); FLog.warning("Invalid command blocker entry: " + rawEntry);
continue; continue;
} }
final String[] commandParts = commandName.split(" "); final String[] commandParts = strCommand.split(" ");
String subCommand = null; String commandName = strCommand.toLowerCase();
if (commandParts.length > 1) if (commandParts.length > 1)
{ {
commandName = commandParts[0]; commandName = commandParts[0];
subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).trim().toLowerCase();
} }
final Command command = commandMap.getCommand(commandName); final Command command = commandMap.getCommand(commandName);
@ -95,25 +94,21 @@ public class CommandBlocker extends FreedomService
{ {
unknownCommands.add(commandName); unknownCommands.add(commandName);
} }
else
{
commandName = command.getName().toLowerCase();
}
if (entryList.containsKey(commandName)) if (entryList.containsKey(strCommand))
{ {
FLog.warning("Not blocking: /" + commandName + " - Duplicate entry exists!"); FLog.warning("Not blocking: /" + strCommand + " - Duplicate entry exists!");
continue; continue;
} }
final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, commandName, subCommand, message); final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, strCommand, message);
entryList.put(blockedCommandEntry.getCommand(), blockedCommandEntry); entryList.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
if (command != null) if (command != null)
{ {
for (String alias : command.getAliases()) for (String alias : command.getAliases())
{ {
entryList.put(alias.toLowerCase(), blockedCommandEntry); entryList.put(strCommand.replaceFirst(commandName, alias), blockedCommandEntry);
} }
} }
} }
@ -153,6 +148,7 @@ public class CommandBlocker extends FreedomService
// Format // Format
command = command.toLowerCase().trim(); command = command.toLowerCase().trim();
command = command.startsWith("/") ? command.substring(1) : command; command = command.startsWith("/") ? command.substring(1) : command;
command = command.replaceAll("\"", "");
// Check for plugin specific commands // Check for plugin specific commands
final String[] commandParts = command.split(" "); final String[] commandParts = command.split(" ");
@ -184,29 +180,13 @@ public class CommandBlocker extends FreedomService
return true; 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 // Obtain entry
final CommandBlockerEntry entry = entryList.get(commandParts[0]); final CommandBlockerEntry entry = entryList.get(command);
if (entry == null) if (entry == null)
{ {
return false; return false;
} }
// Validate sub command
if (entry.getSubCommand() != null)
{
if (subCommand == null || !subCommand.startsWith(entry.getSubCommand()))
{
return false;
}
}
if (entry.getRank().hasPermission(sender)) if (entry.getRank().hasPermission(sender))
{ {
return false; return false;
@ -219,4 +199,4 @@ public class CommandBlocker extends FreedomService
return true; return true;
} }
} }

View File

@ -18,21 +18,13 @@ public class CommandBlockerEntry
@Getter @Getter
private final String command; private final String command;
@Getter @Getter
private final String subCommand;
@Getter
private final String message; private final String message;
public CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, 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.rank = rank;
this.action = action; this.action = action;
this.command = command; this.command = command;
this.subCommand = ((subCommand == null) ? null : subCommand.toLowerCase().trim());
this.message = ((message == null || message.equals("_")) ? "That command is blocked." : message); this.message = ((message == null || message.equals("_")) ? "That command is blocked." : message);
} }