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

87 lines
2.6 KiB
Java
Raw Normal View History

2018-05-13 15:28:53 +00:00
package pw.kaboom.commandspy;
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.ChatColor;
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;
import org.bukkit.plugin.java.JavaPlugin;
2019-12-17 16:00:31 +00:00
public final class Main extends JavaPlugin implements CommandExecutor, Listener {
2020-04-16 16:43:57 +00:00
private FileConfiguration config;
2019-12-17 16:00:31 +00:00
@Override
2018-05-13 15:28:53 +00:00
public void onEnable() {
2020-04-16 16:43:57 +00:00
config = getConfig();
2019-12-17 16:00:31 +00:00
this.getCommand("commandspy").setExecutor(this);
2018-05-13 15:28:53 +00:00
this.getServer().getPluginManager().registerEvents(this, this);
}
2019-12-17 17:39:53 +00:00
@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
2019-12-17 16:00:31 +00:00
if (sender instanceof ConsoleCommandSender) {
sender.sendMessage("Command has to be run by a player");
2020-04-16 17:05:32 +00:00
return true;
}
2019-12-17 16:00:31 +00:00
2020-04-16 17:05:32 +00:00
final Player player = (Player) sender;
final JavaPlugin plugin = JavaPlugin.getPlugin(Main.class);
if (plugin.getConfig().contains(player.getUniqueId().toString())) {
plugin.getConfig().set(player.getUniqueId().toString(), null);
plugin.saveConfig();
player.sendMessage("Successfully disabled CommandSpy");
} else {
plugin.getConfig().set(player.getUniqueId().toString(), true);
plugin.saveConfig();
player.sendMessage("Successfully enabled CommandSpy");
2019-12-17 16:00:31 +00:00
}
2020-04-16 17:05:32 +00:00
config = plugin.getConfig();
2019-12-17 16:00:31 +00:00
return true;
}
2018-05-13 15:28:53 +00:00
@EventHandler
2019-12-17 17:39:53 +00:00
void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
2020-04-16 16:43:57 +00:00
for (String uuidString : config.getKeys(false)) {
UUID uuid = UUID.fromString(uuidString);
2020-04-16 16:43:57 +00:00
if (Bukkit.getPlayer(uuid) != null) {
2020-04-16 17:05:32 +00:00
Bukkit.getPlayer(uuid).sendMessage(
2020-04-16 17:12:44 +00:00
ChatColor.AQUA + ""
2020-04-16 17:05:32 +00:00
+ event.getPlayer().getName() + ""
2020-04-16 17:12:44 +00:00
+ ChatColor.AQUA + ": "
2020-04-16 17:05:32 +00:00
+ event.getMessage()
);
2019-04-05 08:36:48 +00:00
}
}
}
@EventHandler
2019-12-17 16:00:31 +00:00
void onSignChange(final SignChangeEvent event) {
2020-04-16 16:43:57 +00:00
for (String uuidString : config.getKeys(false)) {
UUID uuid = UUID.fromString(uuidString);
2020-04-16 16:43:57 +00:00
if (Bukkit.getPlayer(uuid) != null) {
2020-04-16 17:05:32 +00:00
Bukkit.getPlayer(uuid).sendMessage(
2020-04-16 17:12:44 +00:00
ChatColor.AQUA + ""
2020-04-16 17:05:32 +00:00
+ event.getPlayer().getName() + ""
2020-04-16 17:12:44 +00:00
+ ChatColor.AQUA
2020-04-16 17:05:32 +00:00
+ " created a sign with contents:"
);
2019-04-05 08:36:48 +00:00
for (String line: event.getLines()) {
2020-04-16 17:12:44 +00:00
Bukkit.getPlayer(uuid).sendMessage(ChatColor.AQUA + " " + line);
2018-05-13 15:28:53 +00:00
}
}
}
}
}