From 9f7ac310d9289fcd79cf7013d9f572cea3dd73ca Mon Sep 17 00:00:00 2001 From: Taah Date: Sun, 10 Apr 2022 00:25:14 -0700 Subject: [PATCH] merge master branch and add ayunami's delimiter code, with of course, credit to him added in the code, and fix a bug --- .../dev/plex/command/blocker/BaseCommand.java | 22 ---- .../blocker/CommandBlockerManager.java | 122 ------------------ .../plex/command/blocker/MatchCommand.java | 21 --- .../plex/command/blocker/RegexCommand.java | 23 ---- .../java/dev/plex/command/impl/PlexCMD.java | 4 +- .../plex/listener/impl/CommandListener.java | 3 +- .../services/impl/CommandBlockerService.java | 16 ++- src/main/resources/commands.yml | 23 ++-- 8 files changed, 30 insertions(+), 204 deletions(-) delete mode 100644 src/main/java/dev/plex/command/blocker/BaseCommand.java delete mode 100644 src/main/java/dev/plex/command/blocker/CommandBlockerManager.java delete mode 100644 src/main/java/dev/plex/command/blocker/MatchCommand.java delete mode 100644 src/main/java/dev/plex/command/blocker/RegexCommand.java diff --git a/src/main/java/dev/plex/command/blocker/BaseCommand.java b/src/main/java/dev/plex/command/blocker/BaseCommand.java deleted file mode 100644 index ae5850c..0000000 --- a/src/main/java/dev/plex/command/blocker/BaseCommand.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.plex.command.blocker; - -import dev.plex.rank.enums.Rank; -import lombok.Getter; - -@Getter -public class BaseCommand -{ - private final Rank rank; - private final String message; - - public BaseCommand(Rank r, String m) - { - rank = r; - message = m; - } - - public String toString() - { - return "BaseCommand (Rank: " + (rank == null ? "ALL" : rank.name()) + ", Message: " + message + ")"; - } -} diff --git a/src/main/java/dev/plex/command/blocker/CommandBlockerManager.java b/src/main/java/dev/plex/command/blocker/CommandBlockerManager.java deleted file mode 100644 index f44feab..0000000 --- a/src/main/java/dev/plex/command/blocker/CommandBlockerManager.java +++ /dev/null @@ -1,122 +0,0 @@ -package dev.plex.command.blocker; - -import dev.plex.PlexBase; -import dev.plex.rank.enums.Rank; -import dev.plex.util.PlexLog; -import dev.plex.util.PlexUtils; -import lombok.Getter; -import org.bukkit.Bukkit; -import org.bukkit.command.*; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.SimplePluginManager; - -import java.lang.reflect.Field; -import java.util.*; -import java.util.regex.Pattern; - -@Getter -public class CommandBlockerManager extends PlexBase -{ - private Set blockedCommands = new HashSet<>(); - - public boolean loadedYet; - - private static CommandMap getCommandMap() - { - return plugin.getServer().getCommandMap(); - } - - public void syncCommands() - { - loadedYet = false; - blockedCommands.clear(); - - CommandMap commandMap = getCommandMap(); - - List raw = plugin.blockedCommands.getStringList("blockedCommands"); - - for (String cmd : raw) - { - int lastDelim = cmd.lastIndexOf(':'); - - String cmdWithoutMsg = cmd.substring(0, lastDelim); - String[] rawPieces = cmdWithoutMsg.split(":", 3); - - String rawType = rawPieces[0].toLowerCase(); - String rawRank = rawPieces[1].toLowerCase(); - String regexOrMatch = rawPieces[2]; - String message = cmd.substring(lastDelim + 1); - - if (message.equals("_")) - { - message = PlexUtils.messageString("commandBlocked"); - } - - Rank rank = switch (rawRank) - { - case "e" -> null; - case "a" -> Rank.ADMIN; - case "s" -> Rank.SENIOR_ADMIN; - default -> null; - }; - - if (rawType.equals("r")) - { - blockedCommands.add(new RegexCommand(Pattern.compile(regexOrMatch, Pattern.CASE_INSENSITIVE), rank, message)); - } - else if (rawType.equals("m")) - { - int ind = regexOrMatch.indexOf(' '); - if (ind == -1 && regexOrMatch.endsWith(":")) - { - String pluginName = regexOrMatch.substring(0, regexOrMatch.length() - 1); - Plugin plugin = Arrays.stream(Bukkit.getServer().getPluginManager().getPlugins()).filter(pl -> pl.getName().equalsIgnoreCase(pluginName)).findAny().orElse(null); - if (plugin != null) - { - List commandList = PluginCommandYamlParser.parse(plugin); - for (Command command : commandList) - { - blockedCommands.add(new MatchCommand(command.getName(), rank, message)); - blockedCommands.add(new MatchCommand(pluginName + ":" + command.getName(), rank, message)); - for (String alias : command.getAliases()) - { - blockedCommands.add(new MatchCommand(alias, rank, message)); - blockedCommands.add(new MatchCommand(pluginName + ":" + alias, rank, message)); - } - } - } - } - String blockedArgs = ind == -1 ? "" : regexOrMatch.substring(ind + 1); - if (!blockedArgs.isEmpty()) - { - blockedArgs = " " + blockedArgs; // necessary in case no args - } - String cmdForSearch = ind == -1 ? regexOrMatch : regexOrMatch.substring(0, ind); - PluginCommand pluginCommand = Bukkit.getServer().getPluginCommand(cmdForSearch); - Plugin plugin = null; - if (pluginCommand != null) plugin = pluginCommand.getPlugin(); - Command command = null; - if (commandMap != null) command = commandMap.getCommand(cmdForSearch); - if (command != null) - { - String pluginName = plugin == null ? null : plugin.getName(); - blockedCommands.add(new MatchCommand(command.getName() + blockedArgs, rank, message)); - if (pluginName != null) blockedCommands.add(new MatchCommand(pluginName + ":" + command.getName() + blockedArgs, rank, message)); - List aliases = command.getAliases(); - for (String alias : aliases) - { - blockedCommands.add(new MatchCommand(alias + blockedArgs, rank, message)); - if (pluginName != null) blockedCommands.add(new MatchCommand(pluginName + ":" + alias + blockedArgs, rank, message)); - } - } - else - { - // fallback to basic blocking - blockedCommands.add(new MatchCommand(cmdForSearch + blockedArgs, rank, message)); - } - } - } - - loadedYet = true; - } -} diff --git a/src/main/java/dev/plex/command/blocker/MatchCommand.java b/src/main/java/dev/plex/command/blocker/MatchCommand.java deleted file mode 100644 index 8ad36ae..0000000 --- a/src/main/java/dev/plex/command/blocker/MatchCommand.java +++ /dev/null @@ -1,21 +0,0 @@ -package dev.plex.command.blocker; - -import dev.plex.rank.enums.Rank; -import lombok.Getter; - -@Getter -public class MatchCommand extends BaseCommand -{ - private final String match; - - public MatchCommand(String r1, Rank r2, String m1) - { - super(r2, m1); - match = r1; - } - - public String toString() - { - return "MatchCommand (Rank: " + (getRank() == null ? "ALL" : getRank().name()) + ", Match: " + match + ", Message: " + getMessage() + ")"; - } -} diff --git a/src/main/java/dev/plex/command/blocker/RegexCommand.java b/src/main/java/dev/plex/command/blocker/RegexCommand.java deleted file mode 100644 index ea75e8b..0000000 --- a/src/main/java/dev/plex/command/blocker/RegexCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -package dev.plex.command.blocker; - -import dev.plex.rank.enums.Rank; -import lombok.Getter; - -import java.util.regex.Pattern; - -@Getter -public class RegexCommand extends BaseCommand -{ - private final Pattern regex; - - public RegexCommand(Pattern r1, Rank r2, String m1) - { - super(r2, m1); - regex = r1; - } - - public String toString() - { - return "RegexCommand (Rank: " + (getRank() == null ? "ALL" : getRank().name()) + ", Regex: " + regex.toString() + ", Message: " + getMessage() + ")"; - } -} \ No newline at end of file diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index 1eef019..68795e7 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -51,8 +51,8 @@ public class PlexCMD extends PlexCommand plugin.indefBans.load(false); plugin.getPunishmentManager().mergeIndefiniteBans(); send(sender, "Reloaded indefinite bans"); - plugin.blockedCommands.load(); - plugin.getCommandBlockerManager().syncCommands(); + plugin.commands.load(); +// plugin.getCommandBlockerManager().syncCommands(); send(sender, "Reloaded blocked commands file"); plugin.getRankManager().importDefaultRanks(); send(sender, "Imported ranks"); diff --git a/src/main/java/dev/plex/listener/impl/CommandListener.java b/src/main/java/dev/plex/listener/impl/CommandListener.java index 063b3d5..b376042 100644 --- a/src/main/java/dev/plex/listener/impl/CommandListener.java +++ b/src/main/java/dev/plex/listener/impl/CommandListener.java @@ -48,7 +48,8 @@ public class CommandListener extends PlexListener String arguments = StringUtils.normalizeSpace(event.getMessage().replace(event.getMessage().split(" ")[0], "")); PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments); AtomicReference cmdRef = new AtomicReference<>(); - CommandBlockerService.getBLOCKED_COMMANDS().stream().filter(blockedCommand -> blockedCommand.getCommand() != null).findFirst().ifPresent(blockedCommand -> + 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))) { diff --git a/src/main/java/dev/plex/services/impl/CommandBlockerService.java b/src/main/java/dev/plex/services/impl/CommandBlockerService.java index 60939b8..af641bf 100644 --- a/src/main/java/dev/plex/services/impl/CommandBlockerService.java +++ b/src/main/java/dev/plex/services/impl/CommandBlockerService.java @@ -27,14 +27,18 @@ public class CommandBlockerService extends AbstractService { BLOCKED_COMMANDS.clear(); PlexLog.debug("RUNNING COMMAND BLOCKING SERVICE"); - plugin.commands.getStringList("commands").forEach(s -> { + plugin.commands.getStringList("commands").forEach(s -> + { BlockedCommand command = new BlockedCommand(); - String[] args = s.split(";"); + int lastDelim = s.lastIndexOf(':'); + + String cmdWithoutMsg = s.substring(0, lastDelim); + String[] args = cmdWithoutMsg.split(":", 3); // Delimiter code by ayunami if (s.toLowerCase(Locale.ROOT).startsWith("r")) { command.setRequiredLevel(args[1]); command.setRegex(args[2]); - command.setMessage(StringUtils.join(args, ";", 3, args.length)); + command.setMessage(s.substring(lastDelim + 1)); PlexLog.debug("=Found regex blocked="); PlexLog.debug(" Regex: " + command.getRegex()); PlexLog.debug(" Message: " + command.getMessage()); @@ -43,7 +47,7 @@ public class CommandBlockerService extends AbstractService { command.setRequiredLevel(args[1]); command.setCommand(args[2]); - command.setMessage(StringUtils.join(args, ";", 3, args.length)); + command.setMessage(s.substring(lastDelim + 1)); Command cmd = plugin.getServer().getCommandMap().getCommand(command.getCommand().split(" ")[0]); if (cmd == null) { @@ -59,6 +63,10 @@ public class CommandBlockerService extends AbstractService PlexLog.debug(" Aliases: " + Arrays.toString(command.getCommandAliases().toArray(new String[0]))); PlexLog.debug("===================="); } + if (command.getMessage().equalsIgnoreCase("_")) + { + command.setMessage("This command is blocked."); + } BLOCKED_COMMANDS.add(command); }); } diff --git a/src/main/resources/commands.yml b/src/main/resources/commands.yml index a46fd26..b6a0a3b 100644 --- a/src/main/resources/commands.yml +++ b/src/main/resources/commands.yml @@ -1,20 +1,25 @@ -# Plex Command Blocking File -# For documentation, please visit: https://plex.us.org - +# +# Command Blocker +# # Format: # - "::command name no slash:Block message" # # Symbols to use: # - r for RegEx # - m for matching -# - The ranks are "e" for everyone, "a" for admin and "s" for senior admin -# - The command is just the command without slashes. Optional arguments are specified as well +# - The ranks are "e" for everyone, "s" for senior admin, and "a" for admin +# - MATCHING MODE: The command is just the command without slashes. Optional arguments are specified as well. It also accepts full plugins via specifying the plugin name followed by a ":" (e.g. "viaversion:") +# - REGEX MODE: The command is regex that matches the desired command. It matches case insensitively. # - Finally the block message. MUST NOT CONTAIN ":". Use _ to use the default command blocked message as specified in messages.yml, or you can optionally put your own in # # So these would be valid: # - "m:e:mail sendall:You cannot send messages to everyone on the server" -# - "r:e:(.*:):Plugin specific commands are disabled" +# - "r:e:^[^ :]+::Plugin specific commands are disabled" +commands: + # Plugin specific commands (might not be necessary anymore!!) -commands: # Regexes and commands can't contain ';'. Don't try, commands can't contain ';' by default. - - "r;(^\w+:);"Plugin specific commands are disabled - - 'm;e;mail sendall;You cannot send messages to everyone on the server' \ No newline at end of file + - "r:e:^[^ :]+::Plugin specific commands are disabled." + # Disabled commands + + - "m:e:time:Server-side time changing is disabled. Please use /ptime to set your own personal time." + - "m:e:pardon:_"