Merge pull request #13 from FlorianMichael/command-spy

Implemented basic command spy
This commit is contained in:
Paldiu 2023-08-01 09:09:42 -05:00 committed by GitHub
commit dd4e1bbda0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 0 deletions

View File

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

View File

@ -0,0 +1,52 @@
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", onlyPlayers = true)
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.
*/
public CommandSpyCommand(@NotNull JavaPlugin plugin)
{
super(plugin);
}
@Base
public void commandSpy(final Player sender)
{
final var commandSpy = ((Datura) getPlugin()).
getCommandSpy();
final var uuid = 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))
)
);
}
}