Implemented basic command spy

This commit is contained in:
FlorianMichael 2023-07-31 12:53:20 +02:00
parent 2ac955cc0a
commit 722b1b72a4
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
3 changed files with 124 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package me.totalfreedom.datura;
import me.totalfreedom.base.Patchwork;
import me.totalfreedom.datura.features.CommandSpy;
import me.totalfreedom.datura.punishment.Cager;
import me.totalfreedom.datura.punishment.Halter;
import me.totalfreedom.datura.punishment.Locker;
@ -12,10 +13,14 @@ import org.bukkit.plugin.java.JavaPlugin;
public class Datura extends JavaPlugin
{
private final MySQL sql = new MySQL("localhost", 3011, "master");
// Punishment
private final Halter halter = new Halter();
private final Locker locker = new Locker();
private final Cager cager = new Cager();
// Features
private final CommandSpy commandSpy = new CommandSpy();
@Override
public void onEnable()
@ -36,6 +41,8 @@ public class Datura extends JavaPlugin
Bukkit.getPluginManager()
.registerEvents(halter, this);
Bukkit.getPluginManager()
.registerEvents(commandSpy, this);
}
public MySQL getSQL()
@ -57,4 +64,8 @@ public class Datura extends JavaPlugin
{
return cager;
}
public CommandSpy getCommandSpy() {
return commandSpy;
}
}

View File

@ -0,0 +1,58 @@
package me.totalfreedom.datura.cmd;
import me.totalfreedom.command.Commander;
import me.totalfreedom.command.annotation.Base;
import me.totalfreedom.command.annotation.Info;
import me.totalfreedom.command.annotation.Permissive;
import me.totalfreedom.datura.Datura;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
@Info(name = "commandspy", description = "Spy on commands executed by players.", usage = "/commandspy")
@Permissive(perm = "datura.commandspy")
public class CommandSpyCommand extends Commander
{
/**
* Initializes this command object. The provided {@link JavaPlugin} should be the plugin which contains the
* command.
* <p>
* This constructor will automatically register all subcommands and completions for this command. It will also
* automatically infer all required information from the provided {@link Info} and {@link Permissive} annotations.
*
* @param plugin The plugin which contains this command.
*/
protected CommandSpyCommand(@NotNull JavaPlugin plugin)
{
super(plugin);
}
@Base
public void commandSpy(final CommandSender sender)
{
if (!(sender instanceof Player))
{
sender.sendPlainMessage("You have to be a player to perform this command.");
return;
}
final var commandSpy = ((Datura) getPlugin()).
getCommandSpy();
final var uuid = ((Player) sender).
getUniqueId();
if (commandSpy.isSpying(uuid))
{
commandSpy.stop(uuid);
sender.sendPlainMessage("CommandSpy disabled.");
}
else
{
commandSpy.spy(uuid);
sender.sendPlainMessage("CommandSpy enabled.");
}
}
}

View File

@ -0,0 +1,55 @@
package me.totalfreedom.datura.features;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class CommandSpy implements Listener
{
private final Set<UUID> commandWatchers;
public CommandSpy()
{
this.commandWatchers = new HashSet<>();
}
public void spy(final UUID uuid)
{
this.commandWatchers.add(uuid);
}
public void stop(final UUID uuid)
{
this.commandWatchers.remove(uuid);
}
public void clear()
{
this.commandWatchers.clear();
}
public boolean isSpying(final UUID uuid)
{
return this.commandWatchers.contains(uuid);
}
@EventHandler
public void commandProcess(final PlayerCommandPreprocessEvent event)
{
Bukkit.getOnlinePlayers().stream()
.filter(player -> isSpying(player.getUniqueId()))
.forEach(player -> player.sendMessage(Component.text(event.getPlayer().getName(), NamedTextColor.GRAY)
.append(Component.text(": ", NamedTextColor.GRAY))
.append(Component.text(event.getMessage(), NamedTextColor.GRAY))
)
);
}
}