diff --git a/src/main/java/dev/plex/cmdblocker/BaseCommand.java b/src/main/java/dev/plex/cmdblocker/BaseCommand.java index 21d148a..95bc913 100644 --- a/src/main/java/dev/plex/cmdblocker/BaseCommand.java +++ b/src/main/java/dev/plex/cmdblocker/BaseCommand.java @@ -6,8 +6,8 @@ import lombok.Getter; @Getter public class BaseCommand { - public final Rank rank; - public final String message; + private final Rank rank; + private final String message; public BaseCommand(Rank r, String m) { diff --git a/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java b/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java index 5d56df2..10a13a9 100644 --- a/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java +++ b/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java @@ -2,11 +2,16 @@ package dev.plex.cmdblocker; import dev.plex.Plex; import dev.plex.rank.enums.Rank; +import dev.plex.util.PlexUtils; +import lombok.Getter; +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.command.PluginCommand; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; +@Getter public class CommandBlockerManager { private List blockedCommands = new ArrayList<>(); @@ -31,15 +36,30 @@ public class CommandBlockerManager pieces.add(rawPieces[2]); // RegEx or match pieces.add(cmd.substring(lastDelim + 1)); // Message (w/o : in it) + if (pieces.get(3).equals("_")) + { + pieces.set(3, PlexUtils.messageString("commandBlocked")); + } + Rank rank = Plex.get().getRankManager().getRankFromString(pieces.get(1)); if (pieces.get(0).equals("r")) { - blockedCommands.add(new RegexCommand(Pattern.compile(pieces.get(2)), rank, pieces.get(3))); + blockedCommands.add(new RegexCommand(Pattern.compile(pieces.get(2), Pattern.CASE_INSENSITIVE), rank, pieces.get(3))); } else if (pieces.get(0).equals("m")) { blockedCommands.add(new MatchCommand(pieces.get(2), rank, pieces.get(3))); + String blockedArgs = pieces.get(2).substring(pieces.get(2).indexOf(' ') + 1); + PluginCommand pluginCommand = Plex.get().getServer().getPluginCommand(pieces.get(2).substring(0, pieces.get(2).indexOf(' '))); + if (pluginCommand != null) + { + List aliases = pluginCommand.getAliases(); + for (String alias : aliases) + { + blockedCommands.add(new MatchCommand(alias + " " + blockedArgs, rank, pieces.get(3))); + } + } } } } diff --git a/src/main/java/dev/plex/cmdblocker/MatchCommand.java b/src/main/java/dev/plex/cmdblocker/MatchCommand.java index 96ec6cf..7d98ed9 100644 --- a/src/main/java/dev/plex/cmdblocker/MatchCommand.java +++ b/src/main/java/dev/plex/cmdblocker/MatchCommand.java @@ -6,7 +6,7 @@ import lombok.Getter; @Getter public class MatchCommand extends BaseCommand { - public final String match; + private final String match; public MatchCommand(String r1, Rank r2, String m1) { diff --git a/src/main/java/dev/plex/cmdblocker/RegexCommand.java b/src/main/java/dev/plex/cmdblocker/RegexCommand.java index fd2c027..84d2e30 100644 --- a/src/main/java/dev/plex/cmdblocker/RegexCommand.java +++ b/src/main/java/dev/plex/cmdblocker/RegexCommand.java @@ -8,7 +8,7 @@ import java.util.regex.Pattern; @Getter public class RegexCommand extends BaseCommand { - public final Pattern regex; + private final Pattern regex; public RegexCommand(Pattern r1, Rank r2, String m1) { diff --git a/src/main/java/dev/plex/listener/impl/CmdBlockerListener.java b/src/main/java/dev/plex/listener/impl/CmdBlockerListener.java index 831ab4b..b7b1723 100644 --- a/src/main/java/dev/plex/listener/impl/CmdBlockerListener.java +++ b/src/main/java/dev/plex/listener/impl/CmdBlockerListener.java @@ -1,6 +1,11 @@ package dev.plex.listener.impl; +import dev.plex.cmdblocker.BaseCommand; +import dev.plex.cmdblocker.MatchCommand; +import dev.plex.cmdblocker.RegexCommand; import dev.plex.listener.PlexListener; +import dev.plex.util.PlexUtils; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -15,5 +20,31 @@ public class CmdBlockerListener extends PlexListener String message = event.getMessage().substring(1); // check if commands are blocked here // DEFAULT message is named "commandBlocked" + for (BaseCommand blockedCommand : plugin.getCommandBlockerManager().getBlockedCommands()) { + //first check rank for if it applies + //then check if command is blocked + boolean isBlocked = false; + if (blockedCommand instanceof RegexCommand regexCommand) + { + // regex + if (regexCommand.getRegex().matcher(message).matches()) + { + isBlocked = true; + } + } + else if(blockedCommand instanceof MatchCommand matchCommand) + { + // match command + if (message.equalsIgnoreCase(matchCommand.getMessage()) || message.toLowerCase().startsWith(matchCommand.getMessage().toLowerCase() + " ")) + { + isBlocked = true; + } + } + if (isBlocked) + { + event.setCancelled(true); + player.sendMessage(MiniMessage.miniMessage().deserialize(PlexUtils.messageString("blockedCommandColor") + blockedCommand.getMessage())); + } + } } } diff --git a/src/main/resources/commands.yml b/src/main/resources/commands.yml index f095f74..1e0d279 100644 --- a/src/main/resources/commands.yml +++ b/src/main/resources/commands.yml @@ -16,4 +16,4 @@ # - "r:e:(.*:):Plugin specific commands are disabled" blockedCommands: - "m:e:mail sendall:You cannot send messages to everyone on the server" - - "r:e:(.*:):Plugin specific commands are disabled" \ No newline at end of file + - "r:e:[^ ]+:.*:Plugin specific commands are disabled" \ No newline at end of file diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index ac87e6f..8fba7d1 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -167,4 +167,5 @@ 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." -commandBlocked: "This command is blocked" \ No newline at end of file +blockedCommandColor: "" +commandBlocked: "This command is blocked" \ No newline at end of file