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;
|
2022-04-04 08:36:50 +00:00
|
|
|
import dev.plex.cache.player.PlayerCache;
|
2022-04-08 07:20:17 +00:00
|
|
|
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;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
|
|
import org.apache.commons.lang.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;
|
|
|
|
|
2022-04-08 07:20:17 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2022-02-04 21:25:40 +00:00
|
|
|
public class CommandListener extends PlexListener
|
|
|
|
{
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
|
|
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
|
|
|
|
{
|
|
|
|
Bukkit.getOnlinePlayers().stream().filter(pl -> PlayerCache.getPlexPlayer(pl.getUniqueId()).isCommandSpy()).forEach(pl ->
|
|
|
|
{
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
String command = event.getMessage();
|
|
|
|
if (pl != player)
|
|
|
|
{
|
|
|
|
pl.sendMessage(ChatColor.GRAY + player.getName() + ": " + command);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-04-08 07:20:17 +00:00
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
|
|
public void onCommandBlocking(PlayerCommandPreprocessEvent event)
|
|
|
|
{
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
|
|
|
|
String commandName = event.getMessage().split(" ")[0].replace("/", "");
|
|
|
|
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 ->
|
|
|
|
{
|
|
|
|
if (event.getMessage().replace("/", "").toLowerCase(Locale.ROOT).startsWith(blockedCommand.getCommand().toLowerCase(Locale.ROOT)))
|
|
|
|
{
|
|
|
|
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 an alias.");
|
|
|
|
cmdRef.set(blockedCommand);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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-04-08 07:20:17 +00:00
|
|
|
Matcher matcher = pattern.matcher(event.getMessage().replace("/", ""));
|
|
|
|
if (matcher.find())
|
|
|
|
{
|
|
|
|
PlexLog.debug("Found blocked regexed command");
|
|
|
|
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-04-08 07:32:53 +00:00
|
|
|
case "e" -> {
|
2022-04-08 07:20:17 +00:00
|
|
|
event.setCancelled(true);
|
|
|
|
event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY));
|
|
|
|
}
|
2022-04-08 07:32:53 +00:00
|
|
|
case "a" -> {
|
|
|
|
event.setCancelled(true);
|
|
|
|
event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY));
|
|
|
|
}
|
|
|
|
case "s" -> {
|
2022-04-08 07:20:17 +00:00
|
|
|
event.setCancelled(true);
|
|
|
|
event.getPlayer().sendMessage(Component.text(cmd.getMessage()).color(NamedTextColor.GRAY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-02-04 21:25:40 +00:00
|
|
|
}
|