instead of checking if a command starts with the message, check if each argument of the command is equal to the message until you can't check anymore

This commit is contained in:
Taah 2022-04-10 00:42:53 -07:00
parent 9f7ac310d9
commit 06fcf531af
3 changed files with 35 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.services.impl.CommandBlockerService; import dev.plex.services.impl.CommandBlockerService;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -44,14 +45,36 @@ public class CommandListener extends PlexListener
{ {
Player player = event.getPlayer(); Player player = event.getPlayer();
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
String commandName = event.getMessage().split(" ")[0].replace("/", ""); String commandName = StringUtils.normalizeSpace(event.getMessage()).split(" ")[0].replace("/", "");
String arguments = StringUtils.normalizeSpace(event.getMessage().replace(event.getMessage().split(" ")[0], "")); String arguments = StringUtils.normalizeSpace(StringUtils.normalizeSpace(event.getMessage()).replace(event.getMessage().split(" ")[0], ""));
PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments); PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments);
AtomicReference<BlockedCommand> cmdRef = new AtomicReference<>(); AtomicReference<BlockedCommand> cmdRef = new AtomicReference<>();
PlexLog.debug("Blocked Commands List: " + CommandBlockerService.getBLOCKED_COMMANDS().size()); PlexLog.debug("Blocked Commands List: " + CommandBlockerService.getBLOCKED_COMMANDS().size());
CommandBlockerService.getBLOCKED_COMMANDS().stream().filter(blockedCommand -> blockedCommand.getCommand() != null).forEach(blockedCommand -> CommandBlockerService.getBLOCKED_COMMANDS().stream().filter(blockedCommand -> blockedCommand.getCommand() != null).forEach(blockedCommand ->
{ {
if (event.getMessage().replace("/", "").toLowerCase(Locale.ROOT).startsWith(blockedCommand.getCommand().toLowerCase(Locale.ROOT))) /*if (event.getMessage().replace("/", "").toLowerCase(Locale.ROOT).startsWith(blockedCommand.getCommand().toLowerCase(Locale.ROOT)))
{
PlexLog.debug("Used blocked command exactly matched");
cmdRef.set(blockedCommand);
return;
}*/
boolean matches = true;
String[] args = blockedCommand.getCommand().split(" ");
String[] cmdArgs = event.getMessage().replace("/", "").split(" ");
for (int i = 0; i < args.length; i++)
{
if (i+1 > cmdArgs.length)
{
matches = false;
break;
}
if (!args[i].equalsIgnoreCase(cmdArgs[i]))
{
matches = false;
break;
}
}
if (matches)
{ {
PlexLog.debug("Used blocked command exactly matched"); PlexLog.debug("Used blocked command exactly matched");
cmdRef.set(blockedCommand); cmdRef.set(blockedCommand);
@ -92,15 +115,15 @@ public class CommandListener extends PlexListener
{ {
case "e" -> { case "e" -> {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY)); event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked"));
} }
case "a" -> { case "a" -> {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY)); event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked"));
} }
case "s" -> { case "s" -> {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY)); event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked"));
} }
} }
} }

View File

@ -39,10 +39,10 @@ public class CommandBlockerService extends AbstractService
command.setRequiredLevel(args[1]); command.setRequiredLevel(args[1]);
command.setRegex(args[2]); command.setRegex(args[2]);
command.setMessage(s.substring(lastDelim + 1)); command.setMessage(s.substring(lastDelim + 1));
PlexLog.debug("=Found regex blocked="); /*PlexLog.debug("=Found regex blocked=");
PlexLog.debug(" Regex: " + command.getRegex()); PlexLog.debug(" Regex: " + command.getRegex());
PlexLog.debug(" Message: " + command.getMessage()); PlexLog.debug(" Message: " + command.getMessage());
PlexLog.debug("===================="); PlexLog.debug("====================");*/
} else if (s.toLowerCase(Locale.ROOT).startsWith("m")) } else if (s.toLowerCase(Locale.ROOT).startsWith("m"))
{ {
command.setRequiredLevel(args[1]); command.setRequiredLevel(args[1]);
@ -56,12 +56,12 @@ public class CommandBlockerService extends AbstractService
} }
command.setCommandAliases(cmd.getAliases()); command.setCommandAliases(cmd.getAliases());
command.getCommandAliases().add(command.getCommand().split(" ")[0]); command.getCommandAliases().add(command.getCommand().split(" ")[0]);
PlexLog.debug("=Found command blocked="); /*PlexLog.debug("=Found command blocked=");
PlexLog.debug(" Required Level: " + command.getRequiredLevel()); PlexLog.debug(" Required Level: " + command.getRequiredLevel());
PlexLog.debug(" Command: " + command.getCommand()); PlexLog.debug(" Command: " + command.getCommand());
PlexLog.debug(" Message: " + command.getMessage()); PlexLog.debug(" Message: " + command.getMessage());
PlexLog.debug(" Aliases: " + Arrays.toString(command.getCommandAliases().toArray(new String[0]))); PlexLog.debug(" Aliases: " + Arrays.toString(command.getCommandAliases().toArray(new String[0])));
PlexLog.debug("===================="); PlexLog.debug("====================");*/
} }
if (command.getMessage().equalsIgnoreCase("_")) if (command.getMessage().equalsIgnoreCase("_"))
{ {
@ -69,6 +69,7 @@ public class CommandBlockerService extends AbstractService
} }
BLOCKED_COMMANDS.add(command); BLOCKED_COMMANDS.add(command);
}); });
PlexLog.log("Command Blocker has loaded {0} entries!", BLOCKED_COMMANDS.size());
} }
@Override @Override

View File

@ -167,8 +167,7 @@ removedMobs: "<red>{0} - Removed {1} mobs"
autoWipeDisabled: "<gray>Item wiping is currently disabled in the config!" autoWipeDisabled: "<gray>Item wiping is currently disabled in the config!"
allowDropsDisabled: "<gray>No longer allowing drops from players." allowDropsDisabled: "<gray>No longer allowing drops from players."
allowDropsEnabled: "<gray>Now allowing drops from players." allowDropsEnabled: "<gray>Now allowing drops from players."
blockedCommandColor: "<gray>" commandBlocked: "<gray>That command is blocked."
commandBlocked: "That command is blocked."
# 0 - The command sender # 0 - The command sender
# 1 - The message being said # 1 - The message being said
sayCommand: "[Server: {0}] {1}" sayCommand: "[Server: {0}] {1}"