This commit is contained in:
ayunami2000 2022-04-08 01:42:05 -04:00
parent 583394d22e
commit dd94613b99
7 changed files with 59 additions and 7 deletions

View File

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

View File

@ -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<BaseCommand> 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<String> aliases = pluginCommand.getAliases();
for (String alias : aliases)
{
blockedCommands.add(new MatchCommand(alias + " " + blockedArgs, rank, pieces.get(3)));
}
}
}
}
}

View File

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

View File

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

View File

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

View File

@ -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"
- "r:e:[^ ]+:.*:Plugin specific commands are disabled"

View File

@ -167,4 +167,5 @@ removedMobs: "<red>{0} - Removed {1} mobs"
autoWipeDisabled: "<gray>Item wiping is currently disabled in the config!"
allowDropsDisabled: "<gray>No longer allowing drops from players."
allowDropsEnabled: "<gray>Now allowing drops from players."
commandBlocked: "<gray>This command is blocked"
blockedCommandColor: "<gray>"
commandBlocked: "This command is blocked"