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 1/3] 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)) + ) + ); + } +} From 1ca7b9edc7c9a3ea3f668d27d8a258b24170fecb Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Mon, 31 Jul 2023 12:58:44 +0200 Subject: [PATCH 2/3] Make CommandSpyCommand constructor public --- .../main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java b/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java index 32f1472..1773d46 100644 --- a/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java +++ b/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java @@ -24,7 +24,7 @@ public class CommandSpyCommand extends Commander * * @param plugin The plugin which contains this command. */ - protected CommandSpyCommand(@NotNull JavaPlugin plugin) + public CommandSpyCommand(@NotNull JavaPlugin plugin) { super(plugin); } From 58bcc1c2d604cbc1e8ac3688defc02a31a7f0e96 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:12:43 +0200 Subject: [PATCH 3/3] Use command API properly --- .../totalfreedom/datura/cmd/CommandSpyCommand.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java b/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java index 1773d46..d099696 100644 --- a/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java +++ b/Datura/src/main/java/me/totalfreedom/datura/cmd/CommandSpyCommand.java @@ -11,7 +11,7 @@ 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") +@Permissive(perm = "datura.commandspy", onlyPlayers = true) public class CommandSpyCommand extends Commander { @@ -30,18 +30,12 @@ public class CommandSpyCommand extends Commander } @Base - public void commandSpy(final CommandSender sender) + public void commandSpy(final Player 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). + final var uuid = sender. getUniqueId(); if (commandSpy.isSpying(uuid))