commandspy/src/main/java/pw/kaboom/commandspy/Main.java

121 lines
4 KiB
Java
Raw Normal View History

2018-05-13 15:28:53 +00:00
package pw.kaboom.commandspy;
import java.util.Set;
2020-04-16 16:43:57 +00:00
import java.util.UUID;
2018-05-13 15:28:53 +00:00
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
2019-09-22 02:44:11 +00:00
import org.bukkit.command.ConsoleCommandSender;
2020-04-16 16:43:57 +00:00
import org.bukkit.configuration.file.FileConfiguration;
2018-05-13 15:28:53 +00:00
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
2019-04-05 08:36:48 +00:00
import org.bukkit.event.block.SignChangeEvent;
2018-05-13 15:28:53 +00:00
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
2020-04-16 17:18:44 +00:00
import org.bukkit.plugin.Plugin;
2018-05-13 15:28:53 +00:00
import org.bukkit.plugin.java.JavaPlugin;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
2018-05-13 15:28:53 +00:00
2019-12-17 16:00:31 +00:00
public final class Main extends JavaPlugin implements CommandExecutor, Listener {
2022-05-20 02:10:07 +00:00
private FileConfiguration config;
@Override
public void onEnable() {
config = getConfig();
this.getCommand("commandspy").setExecutor(this);
this.getServer().getPluginManager().registerEvents(this, this);
}
2022-05-20 19:06:26 +00:00
private void enableCommandSpy(final Player player) {
config.set(player.getUniqueId().toString(), true);
saveConfig();
player.sendMessage(Component.text("Successfully enabled CommandSpy"));
2022-05-20 02:10:07 +00:00
}
2022-05-20 19:06:26 +00:00
private void disableCommandSpy(final Player player) {
config.set(player.getUniqueId().toString(), null);
saveConfig();
player.sendMessage(Component.text("Successfully disabled CommandSpy"));
2022-05-20 02:10:07 +00:00
}
private NamedTextColor getTextColor(final Player player) {
2022-05-21 00:16:22 +00:00
if (config.contains(player.getUniqueId().toString())) {
return NamedTextColor.YELLOW;
2022-05-21 00:16:22 +00:00
}
return NamedTextColor.AQUA;
2022-05-21 00:16:22 +00:00
}
2022-05-20 02:10:07 +00:00
@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label,
final String[] args) {
2022-05-20 02:10:07 +00:00
if (sender instanceof ConsoleCommandSender) {
sender.sendMessage(Component.text("Command has to be run by a player"));
2022-05-20 02:10:07 +00:00
return true;
}
final Player player = (Player) sender;
2022-05-21 00:16:22 +00:00
if (args.length >= 1 && "on".equalsIgnoreCase(args[0])) {
2022-05-20 19:06:26 +00:00
enableCommandSpy(player);
2022-05-21 00:16:22 +00:00
return true;
}
if ((args.length >= 1 && "off".equalsIgnoreCase(args[0]))
|| config.contains(player.getUniqueId().toString())) {
2022-05-20 19:06:26 +00:00
disableCommandSpy(player);
2022-05-21 00:16:22 +00:00
return true;
2022-05-20 02:10:07 +00:00
}
2022-05-21 00:16:22 +00:00
enableCommandSpy(player);
2022-05-20 02:10:07 +00:00
return true;
}
@EventHandler
void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
if (event.isCancelled()) {
return;
}
2022-05-21 00:16:22 +00:00
final Player player = event.getPlayer();
final NamedTextColor color = getTextColor(player);
final Component message = Component.text(player.getName(), color)
.append(Component.text(": "))
.append(Component.text(event.getMessage()));
2022-05-20 02:10:07 +00:00
2022-05-21 00:16:22 +00:00
for (String uuidString : config.getKeys(false)) {
final UUID uuid = UUID.fromString(uuidString);
final Player recipient = Bukkit.getPlayer(uuid);
2022-05-20 02:10:07 +00:00
2022-05-21 00:16:22 +00:00
if (recipient == null) {
continue;
2022-05-20 02:10:07 +00:00
}
2022-05-21 00:16:22 +00:00
recipient.sendMessage(message);
2022-05-20 02:10:07 +00:00
}
}
@EventHandler
void onSignChange(final SignChangeEvent event) {
2022-05-21 00:16:22 +00:00
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:"));
for (Component line : event.lines()) {
message = message
.append(Component.text("\n "))
.append(line);
}
2022-05-21 00:16:22 +00:00
for (String uuidString : config.getKeys(false)) {
final UUID uuid = UUID.fromString(uuidString);
final Player recipient = Bukkit.getPlayer(uuid);
if (recipient == null) {
continue;
}
recipient.sendMessage(message);
2022-05-20 02:10:07 +00:00
}
}
2018-05-13 15:28:53 +00:00
}