From 722b1b72a4e34f727ad49191631826889e31e882 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Mon, 31 Jul 2023 12:53:20 +0200 Subject: [PATCH] Implemented basic command spy --- .../java/me/totalfreedom/datura/Datura.java | 11 ++++ .../datura/cmd/CommandSpyCommand.java | 58 +++++++++++++++++++ .../datura/features/CommandSpy.java | 55 ++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java create mode 100644 Datura/src/main/java/me/totalfreedom/datura/features/CommandSpy.java diff --git a/Datura/src/main/java/me/totalfreedom/datura/Datura.java b/Datura/src/main/java/me/totalfreedom/datura/Datura.java index 4680a03..60e39a1 100644 --- a/Datura/src/main/java/me/totalfreedom/datura/Datura.java +++ b/Datura/src/main/java/me/totalfreedom/datura/Datura.java @@ -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; + } } diff --git a/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java b/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java new file mode 100644 index 0000000..32f1472 --- /dev/null +++ b/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java @@ -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. + *

+ * 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."); + } + } +} diff --git a/Datura/src/main/java/me/totalfreedom/datura/features/CommandSpy.java b/Datura/src/main/java/me/totalfreedom/datura/features/CommandSpy.java new file mode 100644 index 0000000..00ba408 --- /dev/null +++ b/Datura/src/main/java/me/totalfreedom/datura/features/CommandSpy.java @@ -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 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)) + ) + ); + } +}