2022-02-04 21:25:40 +00:00
|
|
|
package dev.plex.listener.impl;
|
|
|
|
|
2022-04-08 07:20:17 +00:00
|
|
|
import dev.plex.cache.DataUtils;
|
|
|
|
import dev.plex.command.blocking.BlockedCommand;
|
2022-02-04 21:25:40 +00:00
|
|
|
import dev.plex.listener.PlexListener;
|
2022-04-08 07:20:17 +00:00
|
|
|
import dev.plex.player.PlexPlayer;
|
|
|
|
import dev.plex.rank.enums.Rank;
|
|
|
|
import dev.plex.services.impl.CommandBlockerService;
|
|
|
|
import dev.plex.util.PlexLog;
|
2022-04-13 02:22:17 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2022-06-08 20:09:42 +00:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2022-02-04 21:25:40 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
|
|
|
|
|
|
|
public class CommandListener extends PlexListener
|
|
|
|
{
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
|
|
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
|
|
|
|
{
|
2022-08-02 22:23:54 +00:00
|
|
|
Bukkit.getOnlinePlayers().stream().filter(pl -> {
|
|
|
|
PlexPlayer player = plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId());
|
|
|
|
return player.isCommandSpy() && hasCommandSpy(plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId()));
|
|
|
|
}).forEach(pl ->
|
2022-02-04 21:25:40 +00:00
|
|
|
{
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
String command = event.getMessage();
|
2022-08-02 22:23:54 +00:00
|
|
|
if (!pl.getUniqueId().equals(player.getUniqueId()))
|
2022-02-04 21:25:40 +00:00
|
|
|
{
|
|
|
|
pl.sendMessage(ChatColor.GRAY + player.getName() + ": " + command);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-04-08 07:20:17 +00:00
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
|
|
public void onCommandBlocking(PlayerCommandPreprocessEvent event)
|
|
|
|
{
|
2022-07-24 02:44:18 +00:00
|
|
|
String command = "/" + event.getMessage().replaceFirst("/", "").trim();
|
2022-04-08 07:20:17 +00:00
|
|
|
Player player = event.getPlayer();
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
|
2022-07-24 02:44:18 +00:00
|
|
|
String commandName = StringUtils.normalizeSpace(command).split(" ")[0].replaceFirst("/", "");
|
|
|
|
String arguments = StringUtils.normalizeSpace(StringUtils.normalizeSpace(command).replace(command.split(" ")[0], ""));
|
2022-04-08 07:20:17 +00:00
|
|
|
PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments);
|
|
|
|
AtomicReference<BlockedCommand> cmdRef = new AtomicReference<>();
|
2022-04-10 07:25:14 +00:00
|
|
|
PlexLog.debug("Blocked Commands List: " + CommandBlockerService.getBLOCKED_COMMANDS().size());
|
|
|
|
CommandBlockerService.getBLOCKED_COMMANDS().stream().filter(blockedCommand -> blockedCommand.getCommand() != null).forEach(blockedCommand ->
|
2022-04-08 07:20:17 +00:00
|
|
|
{
|
2022-04-10 07:42:53 +00:00
|
|
|
boolean matches = true;
|
|
|
|
String[] args = blockedCommand.getCommand().split(" ");
|
2022-07-24 02:44:18 +00:00
|
|
|
String[] cmdArgs = command.replaceFirst("/", "").split(" ");
|
2022-04-10 07:42:53 +00:00
|
|
|
for (int i = 0; i < args.length; i++)
|
|
|
|
{
|
2022-04-13 02:22:17 +00:00
|
|
|
if (i + 1 > cmdArgs.length)
|
2022-04-10 07:42:53 +00:00
|
|
|
{
|
|
|
|
matches = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!args[i].equalsIgnoreCase(cmdArgs[i]))
|
|
|
|
{
|
|
|
|
matches = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (matches)
|
2022-04-08 07:20:17 +00:00
|
|
|
{
|
|
|
|
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)))
|
|
|
|
{
|
2022-05-26 04:10:20 +00:00
|
|
|
PlexLog.debug("Player attempted to use a blocked command with alias of normal command: " + blockedCommand.getCommand());
|
2022-04-08 07:20:17 +00:00
|
|
|
cmdRef.set(blockedCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (cmdRef.get() == null)
|
|
|
|
{
|
|
|
|
CommandBlockerService.getBLOCKED_COMMANDS().forEach(blockedCommand ->
|
|
|
|
{
|
|
|
|
if (blockedCommand.getRegex() != null)
|
|
|
|
{
|
2022-04-08 07:24:46 +00:00
|
|
|
Pattern pattern = Pattern.compile(blockedCommand.getRegex(), Pattern.CASE_INSENSITIVE);
|
2022-07-24 02:44:18 +00:00
|
|
|
Matcher matcher = pattern.matcher(command.replaceFirst("/", ""));
|
2022-04-08 07:20:17 +00:00
|
|
|
if (matcher.find())
|
|
|
|
{
|
2022-04-10 22:02:50 +00:00
|
|
|
PlexLog.debug("Player attempted to use a blocked regex");
|
2022-04-08 07:20:17 +00:00
|
|
|
cmdRef.set(blockedCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (cmdRef.get() != null)
|
|
|
|
{
|
|
|
|
BlockedCommand cmd = cmdRef.get();
|
2022-04-08 07:32:53 +00:00
|
|
|
switch (cmd.getRequiredLevel().toLowerCase(Locale.ROOT))
|
2022-04-08 07:20:17 +00:00
|
|
|
{
|
2022-05-19 18:59:16 +00:00
|
|
|
case "e" ->
|
|
|
|
{
|
2022-04-08 07:20:17 +00:00
|
|
|
event.setCancelled(true);
|
2022-04-10 22:02:50 +00:00
|
|
|
event.getPlayer().sendMessage(cmd.getMessage());
|
2022-04-08 07:20:17 +00:00
|
|
|
}
|
2022-05-19 18:59:16 +00:00
|
|
|
case "a" ->
|
|
|
|
{
|
2022-04-10 22:02:50 +00:00
|
|
|
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-04-08 07:32:53 +00:00
|
|
|
event.setCancelled(true);
|
2022-04-10 22:02:50 +00:00
|
|
|
event.getPlayer().sendMessage(cmd.getMessage());
|
2022-04-08 07:32:53 +00:00
|
|
|
}
|
2022-05-19 18:59:16 +00:00
|
|
|
case "s" ->
|
|
|
|
{
|
2022-04-10 22:02:50 +00:00
|
|
|
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.SENIOR_ADMIN))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-04-08 07:20:17 +00:00
|
|
|
event.setCancelled(true);
|
2022-04-10 22:02:50 +00:00
|
|
|
event.getPlayer().sendMessage(cmd.getMessage());
|
2022-04-08 07:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 03:16:31 +00:00
|
|
|
}
|
2022-04-08 07:20:17 +00:00
|
|
|
|
2022-05-26 03:16:31 +00:00
|
|
|
private boolean hasCommandSpy(PlexPlayer plexPlayer)
|
|
|
|
{
|
|
|
|
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
|
|
|
{
|
|
|
|
return plexPlayer.isAdminActive();
|
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
|
|
|
{
|
|
|
|
return plexPlayer.getPlayer().hasPermission("plex.commandspy");
|
|
|
|
}
|
|
|
|
return false;
|
2022-04-08 07:20:17 +00:00
|
|
|
}
|
2022-02-04 21:25:40 +00:00
|
|
|
}
|