Remove the command blocker system

This commit is contained in:
2025-12-25 18:54:53 -05:00
parent 9cda761a0f
commit 39f79a931f
5 changed files with 1 additions and 258 deletions
@@ -1,16 +0,0 @@
package dev.plex.command.blocking;
import com.google.common.collect.Lists;
import java.util.List;
import lombok.Data;
import net.kyori.adventure.text.Component;
@Data
public class BlockedCommand
{
private Component message;
private String requiredLevel;
private String regex;
private String command;
private List<String> commandAliases = Lists.newArrayList();
}
@@ -1,19 +1,9 @@
package dev.plex.listener.impl;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.command.blocking.BlockedCommand;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.services.impl.CommandBlockerService;
import dev.plex.util.PlexLog;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -40,104 +30,6 @@ public class CommandListener extends PlexListener
});
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onCommandBlocking(PlayerCommandPreprocessEvent event)
{
String command = "/" + event.getMessage().replaceFirst("/", "").trim();
Player player = event.getPlayer();
if (Plex.get().getPermissions() != null && Plex.get().getPermissions().has(player, "plex.commandblocker.bypass")) return;
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
String commandName = StringUtils.normalizeSpace(command).split(" ")[0].replaceFirst("/", "");
String arguments = StringUtils.normalizeSpace(StringUtils.normalizeSpace(command).replace(command.split(" ")[0], ""));
PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments);
AtomicReference<BlockedCommand> cmdRef = new AtomicReference<>();
PlexLog.debug("Blocked Commands List: " + CommandBlockerService.getBLOCKED_COMMANDS().size());
CommandBlockerService.getBLOCKED_COMMANDS().stream().filter(blockedCommand -> blockedCommand.getCommand() != null).forEach(blockedCommand ->
{
boolean matches = true;
String[] args = blockedCommand.getCommand().split(" ");
String[] cmdArgs = command.replaceFirst("/", "").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);
return;
}
if (blockedCommand.getCommandAliases().stream().anyMatch(s -> s.equalsIgnoreCase(commandName)))
{
PlexLog.debug("Found a command name in a blocked command alias, checking arguments now.");
String[] commandArgs = blockedCommand.getCommand().split(" ");
if (arguments.toLowerCase(Locale.ROOT).startsWith(StringUtils.join(commandArgs, " ", 1, commandArgs.length).toLowerCase(Locale.ROOT)))
{
PlexLog.debug("Player attempted to use a blocked command with alias of normal command: " + blockedCommand.getCommand());
cmdRef.set(blockedCommand);
}
}
});
if (cmdRef.get() == null)
{
CommandBlockerService.getBLOCKED_COMMANDS().forEach(blockedCommand ->
{
if (blockedCommand.getRegex() != null)
{
Pattern pattern = Pattern.compile(blockedCommand.getRegex(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(command.replaceFirst("/", ""));
if (matcher.find())
{
PlexLog.debug("Player attempted to use a blocked regex");
cmdRef.set(blockedCommand);
}
}
});
}
if (cmdRef.get() != null)
{
BlockedCommand cmd = cmdRef.get();
event.setCancelled(true);
event.getPlayer().sendMessage(cmd.getMessage());
//TODO: Look into removing this or fixing it so they require permissions instead
/*switch (cmd.getRequiredLevel().toLowerCase(Locale.ROOT))
{
case "e" ->
{
event.setCancelled(true);
event.getPlayer().sendMessage(cmd.getMessage());
}
case "a" ->
{
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN))
{
return;
}
event.setCancelled(true);
event.getPlayer().sendMessage(cmd.getMessage());
}
case "s" ->
{
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.SENIOR_ADMIN))
{
return;
}
event.setCancelled(true);
event.getPlayer().sendMessage(cmd.getMessage());
}
}*/
}
}
private boolean hasCommandSpy(PlexPlayer plexPlayer)
{
return plexPlayer.getPlayer().hasPermission("plex.commandspy");
@@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.services.impl.AutoWipeService;
import dev.plex.services.impl.BanService;
import dev.plex.services.impl.CommandBlockerService;
import dev.plex.services.impl.GameRuleService;
import dev.plex.services.impl.TimingService;
import dev.plex.services.impl.UpdateCheckerService;
@@ -20,7 +19,6 @@ public class ServiceManager
{
registerService(new AutoWipeService());
registerService(new BanService());
registerService(new CommandBlockerService());
registerService(new GameRuleService());
registerService(new TimingService());
registerService(new UpdateCheckerService());
@@ -1,75 +0,0 @@
package dev.plex.services.impl;
import com.google.common.collect.Lists;
import dev.plex.command.blocking.BlockedCommand;
import dev.plex.services.AbstractService;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import java.util.List;
import java.util.Locale;
import lombok.Getter;
import org.bukkit.command.Command;
public class CommandBlockerService extends AbstractService
{
@Getter
private static final List<BlockedCommand> BLOCKED_COMMANDS = Lists.newArrayList();
public CommandBlockerService()
{
super(false, false);
}
@Override
public void run(ScheduledTask task)
{
BLOCKED_COMMANDS.clear();
plugin.commands.getStringList("commands").forEach(s ->
{
BlockedCommand command = new BlockedCommand();
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(s.substring(lastDelim + 1).equalsIgnoreCase("_") ? PlexUtils.messageComponent("commandBlocked") : PlexUtils.mmDeserialize(s.substring(lastDelim + 1)));
/*PlexLog.debug("=Found regex blocked=");
PlexLog.debug(" Regex: " + command.getRegex());
PlexLog.debug(" Message: " + command.getMessage());
PlexLog.debug("====================");*/
}
else if (s.toLowerCase(Locale.ROOT).startsWith("m"))
{
command.setRequiredLevel(args[1]);
command.setCommand(args[2]);
command.setMessage(s.substring(lastDelim + 1).equalsIgnoreCase("_") ? PlexUtils.messageComponent("commandBlocked") : PlexUtils.mmDeserialize(s.substring(lastDelim + 1)));
Command cmd = plugin.getServer().getCommandMap().getCommand(command.getCommand().split(" ")[0]);
if (cmd == null)
{
PlexLog.error("Command '{0}' does not belong to any plugin!", command.getCommand().split(" ")[0]);
return;
}
command.setCommandAliases(cmd.getAliases());
command.getCommandAliases().add(command.getCommand().split(" ")[0]);
/*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("====================");*/
}
BLOCKED_COMMANDS.add(command);
});
PlexLog.log("Command Blocker has loaded {0} entries!", BLOCKED_COMMANDS.size());
}
@Override
public int repeatInSeconds()
{
return 0;
}
}