mirror of
https://github.com/kaboomserver/commandspy.git
synced 2025-07-31 10:11:34 +00:00
feat: allow players to disable/enable cspy for others
This commit is contained in:
parent
7635411849
commit
c6351f3f54
2 changed files with 101 additions and 24 deletions
|
@ -1,6 +1,7 @@
|
|||
package pw.kaboom.commandspy;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
|
@ -27,7 +28,7 @@ public final class Main extends JavaPlugin implements CommandExecutor, Listener
|
|||
this.getCommand("commandspy").setExecutor(this);
|
||||
this.getServer().getPluginManager().registerEvents(this, this);
|
||||
|
||||
// Save the config every 30 seconds
|
||||
// Save the state every 30 seconds
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this.config::trySave, 600L, 600L);
|
||||
}
|
||||
|
||||
|
@ -36,14 +37,26 @@ public final class Main extends JavaPlugin implements CommandExecutor, Listener
|
|||
this.config.trySave();
|
||||
}
|
||||
|
||||
private void enableCommandSpy(final Player player) {
|
||||
this.config.setCommandSpyState(player.getUniqueId(), true);
|
||||
player.sendMessage(Component.text("Successfully enabled CommandSpy"));
|
||||
}
|
||||
private void updateCommandSpyState(final @NotNull Player target,
|
||||
final @NotNull CommandSender source, final boolean state) {
|
||||
this.config.setCommandSpyState(target.getUniqueId(), state);
|
||||
|
||||
private void disableCommandSpy(final Player player) {
|
||||
this.config.setCommandSpyState(player.getUniqueId(), false);
|
||||
player.sendMessage(Component.text("Successfully disabled CommandSpy"));
|
||||
final Component stateString = Component.text(state ? "enabled" : "disabled");
|
||||
|
||||
target.sendMessage(Component.empty()
|
||||
.append(Component.text("Successfully "))
|
||||
.append(stateString)
|
||||
.append(Component.text(" CommandSpy.")));
|
||||
|
||||
if (source != target) {
|
||||
source.sendMessage(Component.empty()
|
||||
.append(Component.text("Successfully "))
|
||||
.append(stateString)
|
||||
.append(Component.text(" CommandSpy for "))
|
||||
.append(target.name())
|
||||
.append(Component.text("."))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private NamedTextColor getTextColor(final Player player) {
|
||||
|
@ -57,21 +70,54 @@ public final class Main extends JavaPlugin implements CommandExecutor, Listener
|
|||
@Override
|
||||
public boolean onCommand(final @NotNull CommandSender sender, final @NotNull Command cmd,
|
||||
final @NotNull String label, final String[] args) {
|
||||
if (!(sender instanceof final Player player)) {
|
||||
sender.sendMessage(Component.text("Command has to be run by a player"));
|
||||
return true;
|
||||
|
||||
Player target = null;
|
||||
Boolean state = null;
|
||||
|
||||
switch (args.length) {
|
||||
case 0 -> {}
|
||||
case 1, 2 -> {
|
||||
// Get the last argument as a state. Fail if we have 2 arguments.
|
||||
state = getState(args[args.length - 1]);
|
||||
if (state != null && args.length == 1) {
|
||||
break;
|
||||
} else if (state == null && args.length == 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the first argument as a player. Fail if it can't be found.
|
||||
target = getPlayer(args[0]);
|
||||
if (target != null) {
|
||||
break;
|
||||
}
|
||||
|
||||
sender.sendMessage(Component.empty()
|
||||
.append(Component.text("Player \""))
|
||||
.append(Component.text(args[0]))
|
||||
.append(Component.text("\" not found"))
|
||||
);
|
||||
return true;
|
||||
}
|
||||
default -> {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length >= 1 && "on".equalsIgnoreCase(args[0])) {
|
||||
enableCommandSpy(player);
|
||||
return true;
|
||||
if (target == null) {
|
||||
if (!(sender instanceof final Player player)) {
|
||||
sender.sendMessage(Component.text("Command has to be run by a player"));
|
||||
return true;
|
||||
}
|
||||
|
||||
target = player;
|
||||
}
|
||||
if ((args.length >= 1 && "off".equalsIgnoreCase(args[0]))
|
||||
|| this.config.getCommandSpyState(player.getUniqueId())) {
|
||||
disableCommandSpy(player);
|
||||
return true;
|
||||
|
||||
if (state == null) {
|
||||
state = !this.config.getCommandSpyState(target.getUniqueId());
|
||||
}
|
||||
enableCommandSpy(player);
|
||||
|
||||
this.updateCommandSpyState(target, sender, state);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -81,8 +127,8 @@ public final class Main extends JavaPlugin implements CommandExecutor, Listener
|
|||
final NamedTextColor color = getTextColor(player);
|
||||
|
||||
final Component message = Component.text(player.getName(), color)
|
||||
.append(Component.text(": "))
|
||||
.append(Component.text(event.getMessage()));
|
||||
.append(Component.text(": "))
|
||||
.append(Component.text(event.getMessage()));
|
||||
|
||||
this.config.broadcastSpyMessage(message);
|
||||
}
|
||||
|
@ -92,14 +138,44 @@ public final class Main extends JavaPlugin implements CommandExecutor, Listener
|
|||
final Player player = event.getPlayer();
|
||||
final NamedTextColor color = getTextColor(player);
|
||||
Component message = Component.text(player.getName(), color)
|
||||
.append(Component.text(" created a sign with contents:"));
|
||||
.append(Component.text(" created a sign with contents:"));
|
||||
|
||||
for (final Component line : event.lines()) {
|
||||
message = message
|
||||
.append(Component.text("\n "))
|
||||
.append(line);
|
||||
.append(Component.text("\n "))
|
||||
.append(line);
|
||||
}
|
||||
|
||||
this.config.broadcastSpyMessage(message);
|
||||
}
|
||||
|
||||
private static Player getPlayer(final String arg) {
|
||||
final Player player = Bukkit.getPlayer(arg);
|
||||
if (player != null) {
|
||||
return player;
|
||||
}
|
||||
|
||||
final UUID uuid;
|
||||
try {
|
||||
uuid = UUID.fromString(arg);
|
||||
} catch (final IllegalArgumentException ignored) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Bukkit.getPlayer(uuid);
|
||||
}
|
||||
|
||||
private static Boolean getState(final String arg) {
|
||||
switch (arg) {
|
||||
case "on", "enable" -> {
|
||||
return true;
|
||||
}
|
||||
case "off", "disable" -> {
|
||||
return false;
|
||||
}
|
||||
default -> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,3 +10,4 @@ commands:
|
|||
aliases: [ c, cs, cspy ]
|
||||
description: Allows you to spy on players' commands
|
||||
permission: commandspy.command
|
||||
usage: 'Usage: /<command> [player] [on|enable|off|disable]'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue