From 06fcf531af075ec4f82ce5c147ea5e6432a3cd90 Mon Sep 17 00:00:00 2001 From: Taah Date: Sun, 10 Apr 2022 00:42:53 -0700 Subject: [PATCH] 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 --- .../plex/listener/impl/CommandListener.java | 35 +++++++++++++++---- .../services/impl/CommandBlockerService.java | 9 ++--- src/main/resources/messages.yml | 3 +- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main/java/dev/plex/listener/impl/CommandListener.java b/src/main/java/dev/plex/listener/impl/CommandListener.java index b376042..4c7e91a 100644 --- a/src/main/java/dev/plex/listener/impl/CommandListener.java +++ b/src/main/java/dev/plex/listener/impl/CommandListener.java @@ -8,6 +8,7 @@ import dev.plex.player.PlexPlayer; import dev.plex.rank.enums.Rank; import dev.plex.services.impl.CommandBlockerService; import dev.plex.util.PlexLog; +import dev.plex.util.PlexUtils; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.lang.StringUtils; @@ -44,14 +45,36 @@ public class CommandListener extends PlexListener { Player player = event.getPlayer(); PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); - String commandName = event.getMessage().split(" ")[0].replace("/", ""); - String arguments = StringUtils.normalizeSpace(event.getMessage().replace(event.getMessage().split(" ")[0], "")); + String commandName = StringUtils.normalizeSpace(event.getMessage()).split(" ")[0].replace("/", ""); + String arguments = StringUtils.normalizeSpace(StringUtils.normalizeSpace(event.getMessage()).replace(event.getMessage().split(" ")[0], "")); PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments); AtomicReference cmdRef = new AtomicReference<>(); PlexLog.debug("Blocked Commands List: " + CommandBlockerService.getBLOCKED_COMMANDS().size()); 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"); cmdRef.set(blockedCommand); @@ -92,15 +115,15 @@ public class CommandListener extends PlexListener { case "e" -> { event.setCancelled(true); - event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY)); + event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked")); } case "a" -> { event.setCancelled(true); - event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY)); + event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked")); } case "s" -> { event.setCancelled(true); - event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY)); + event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked")); } } } diff --git a/src/main/java/dev/plex/services/impl/CommandBlockerService.java b/src/main/java/dev/plex/services/impl/CommandBlockerService.java index af641bf..aec0586 100644 --- a/src/main/java/dev/plex/services/impl/CommandBlockerService.java +++ b/src/main/java/dev/plex/services/impl/CommandBlockerService.java @@ -39,10 +39,10 @@ public class CommandBlockerService extends AbstractService command.setRequiredLevel(args[1]); command.setRegex(args[2]); command.setMessage(s.substring(lastDelim + 1)); - PlexLog.debug("=Found regex blocked="); + /*PlexLog.debug("=Found regex blocked="); PlexLog.debug(" Regex: " + command.getRegex()); PlexLog.debug(" Message: " + command.getMessage()); - PlexLog.debug("===================="); + PlexLog.debug("====================");*/ } else if (s.toLowerCase(Locale.ROOT).startsWith("m")) { command.setRequiredLevel(args[1]); @@ -56,12 +56,12 @@ public class CommandBlockerService extends AbstractService } command.setCommandAliases(cmd.getAliases()); 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(" Command: " + command.getCommand()); PlexLog.debug(" Message: " + command.getMessage()); PlexLog.debug(" Aliases: " + Arrays.toString(command.getCommandAliases().toArray(new String[0]))); - PlexLog.debug("===================="); + PlexLog.debug("====================");*/ } if (command.getMessage().equalsIgnoreCase("_")) { @@ -69,6 +69,7 @@ public class CommandBlockerService extends AbstractService } BLOCKED_COMMANDS.add(command); }); + PlexLog.log("Command Blocker has loaded {0} entries!", BLOCKED_COMMANDS.size()); } @Override diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 2894798..c6e410c 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -167,8 +167,7 @@ removedMobs: "{0} - Removed {1} mobs" autoWipeDisabled: "Item wiping is currently disabled in the config!" allowDropsDisabled: "No longer allowing drops from players." allowDropsEnabled: "Now allowing drops from players." -blockedCommandColor: "" -commandBlocked: "That command is blocked." +commandBlocked: "That command is blocked." # 0 - The command sender # 1 - The message being said sayCommand: "[Server: {0}] {1}" \ No newline at end of file