merge master branch and add ayunami's delimiter code, with of course, credit to him added in the code, and fix a bug

This commit is contained in:
Taah 2022-04-10 00:25:14 -07:00
parent 09bc47f3fb
commit 9f7ac310d9
8 changed files with 30 additions and 204 deletions

View File

@ -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 + ")";
}
}

View File

@ -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<BaseCommand> 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<String> 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<Command> 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<String> 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;
}
}

View File

@ -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() + ")";
}
}

View File

@ -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() + ")";
}
}

View File

@ -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");

View File

@ -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<BlockedCommand> 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)))
{

View File

@ -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);
});
}

View File

@ -1,20 +1,25 @@
# Plex Command Blocking File
# For documentation, please visit: https://plex.us.org
#
# Command Blocker
#
# Format:
# - "<regex or match>:<rank>: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'
- "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:_"