mirror of
https://github.com/IdalecioSch/TKNLogs.git
synced 2026-07-28 00:30:08 -03:00
now it works, i think!
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
package org.itakinn.tknlogs;
|
||||||
|
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||||
|
|
||||||
|
public class ChatListener implements Listener {
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerChat(AsyncPlayerChatEvent e) {
|
||||||
|
String message = "[CHAT] " + e.getPlayer().getName() + ": " + e.getMessage();
|
||||||
|
TKNLogs.getDiscord().sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package org.itakinn.tknlogs;
|
||||||
|
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||||
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
|
import org.bukkit.event.server.ServerCommandEvent;
|
||||||
|
|
||||||
|
public class CommandListener implements Listener {
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerCommand(PlayerCommandPreprocessEvent e) {
|
||||||
|
String message = "[COMMAND] " + e.getPlayer().getName() + ": " + e.getMessage()+
|
||||||
|
" X: "+e.getPlayer().getLocation().getBlockX()+
|
||||||
|
" Y: "+e.getPlayer().getLocation().getBlockY()+
|
||||||
|
" Z: "+e.getPlayer().getLocation().getBlockZ()+
|
||||||
|
" World: "+e.getPlayer().getLocation().getWorld().getName();
|
||||||
|
TKNLogs.getDiscord().sendMessage(message);
|
||||||
|
}
|
||||||
|
@EventHandler
|
||||||
|
public void onConsoleCommand(ServerCommandEvent e) {
|
||||||
|
String message = "[CONSOLE] Command: " + e.getCommand();
|
||||||
|
TKNLogs.getDiscord().sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ package org.itakinn.tknlogs;
|
|||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||||
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||||
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -17,27 +19,28 @@ public class DiscordNotifier {
|
|||||||
private static final String token = config.getString("discord.token");
|
private static final String token = config.getString("discord.token");
|
||||||
public DiscordNotifier(){
|
public DiscordNotifier(){
|
||||||
if(token == null){
|
if(token == null){
|
||||||
plugin.getLogger().warning("Token não encontrado");
|
plugin.getLogger().warning("Invalid Token");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
api = JDABuilder.createDefault(token).build();
|
try {
|
||||||
|
api = JDABuilder.createDefault(token)
|
||||||
/* .addEventListeners(new SlashListener()).build();
|
.enableIntents(GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT)
|
||||||
api.upsertCommand("genimg", "gera uma img")
|
.build().awaitReady();
|
||||||
.addOption(OptionType.STRING, "resolução", "resolução da imagem", true, true)
|
channel = api.getTextChannelById(Objects.requireNonNull(config.getString("discord.channel")));
|
||||||
.addOption(OptionType.STRING, "prompt", "prompt pra gerar a imagem", true)
|
if(channel!=null){
|
||||||
.queue();*/
|
channel.sendMessage("Bot Started").queue();
|
||||||
|
}else{
|
||||||
channel = api.getTextChannelById(Objects.requireNonNull(config.getString("discord.channel-id")));
|
plugin.getLogger().warning("Channel Not Found"+channel.toString());
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public void sendMessage(String message){
|
public void sendMessage(String message){
|
||||||
channel.sendMessage(message).queue();
|
channel.sendMessage(message).queue();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public void sendShutdown(){
|
public void sendShutdown(){
|
||||||
api.shutdown();
|
api.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JDA getJDA() {return api;}
|
public static JDA getJDA() {return api;}
|
||||||
}
|
}
|
||||||
@@ -8,20 +8,18 @@ import java.util.logging.LogRecord;
|
|||||||
public class ErrorCatcher extends Handler {
|
public class ErrorCatcher extends Handler {
|
||||||
@Override
|
@Override
|
||||||
public void publish(LogRecord record) {
|
public void publish(LogRecord record) {
|
||||||
if (record.getLevel() == Level.ALL) {
|
String msg = record.getMessage();
|
||||||
String mensagem = record.getMessage();
|
Throwable exce = record.getThrown();
|
||||||
Throwable excecao = record.getThrown();
|
TKNLogs.getDiscord().sendMessage(msg);
|
||||||
TKNLogs.getDiscord().sendMessage(mensagem);
|
if (exce != null) {
|
||||||
|
TKNLogs.getDiscord().sendMessage(exce.toString());
|
||||||
//Bukkit.getConsoleSender().sendMessage("§c[ERROR] " + mensagem);
|
exce.printStackTrace();
|
||||||
|
|
||||||
if (excecao != null) {
|
|
||||||
TKNLogs.getDiscord().sendMessage(excecao.toString());
|
|
||||||
excecao.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static void register() {
|
||||||
|
java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
|
||||||
|
rootLogger.addHandler(new ErrorCatcher());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() {
|
public void flush() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package org.itakinn.tknlogs;
|
||||||
|
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
|
public class JoinQuitListener implements Listener {
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerJoin(PlayerJoinEvent e) {
|
||||||
|
String message = "[JOIN] " + e.getPlayer().getName()+
|
||||||
|
" X: "+e.getPlayer().getLocation().getBlockX()+
|
||||||
|
" Y: "+e.getPlayer().getLocation().getBlockY()+
|
||||||
|
" Z: "+e.getPlayer().getLocation().getBlockZ()+
|
||||||
|
" World: "+e.getPlayer().getLocation().getWorld().getName();
|
||||||
|
TKNLogs.getDiscord().sendMessage(message);
|
||||||
|
}
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerDeath(PlayerDeathEvent e){
|
||||||
|
String message = "[DEATH] " + e.getEntity().getName()+
|
||||||
|
" X: "+e.getEntity().getLocation().getBlockX()+
|
||||||
|
" Y: "+e.getEntity().getLocation().getBlockY()+
|
||||||
|
" Z: "+e.getEntity().getLocation().getBlockZ()+
|
||||||
|
" World: "+e.getEntity().getLocation().getWorld().getName();
|
||||||
|
TKNLogs.getDiscord().sendMessage(message);
|
||||||
|
}
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerQuit(PlayerQuitEvent e) {
|
||||||
|
String message = "[QUIT] " + e.getPlayer().getName()+
|
||||||
|
" X: "+e.getPlayer().getLocation().getBlockX()+
|
||||||
|
" Y: "+e.getPlayer().getLocation().getBlockY()+
|
||||||
|
" Z: "+e.getPlayer().getLocation().getBlockZ()+
|
||||||
|
" World: "+e.getPlayer().getLocation().getWorld().getName();
|
||||||
|
TKNLogs.getDiscord().sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package org.itakinn.tknlogs;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.*;
|
||||||
|
import static java.nio.file.StandardWatchEventKinds.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class LoggerReader {
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
private Path logDir;
|
||||||
|
private WatchService watchService;
|
||||||
|
private long lastReadPosition;
|
||||||
|
|
||||||
|
public LoggerReader() {
|
||||||
|
this.plugin = TKNLogs.getPlugin();
|
||||||
|
this.logDir = Paths.get(plugin.getDataFolder().getParentFile().getParent(), "logs");
|
||||||
|
this.lastReadPosition = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
try {
|
||||||
|
// Configura o WatchService para monitorar modificações no diretório
|
||||||
|
watchService = FileSystems.getDefault().newWatchService();
|
||||||
|
logDir.register(watchService, ENTRY_MODIFY);
|
||||||
|
|
||||||
|
// Inicia a thread de monitoramento assíncrona
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||||
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
|
WatchKey key;
|
||||||
|
try {
|
||||||
|
key = watchService.take(); // Aguarda eventos
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (WatchEvent<?> event : key.pollEvents()) {
|
||||||
|
if (event.kind() == ENTRY_MODIFY) {
|
||||||
|
Path changedFile = logDir.resolve((Path) event.context());
|
||||||
|
if (changedFile.endsWith("latest.log")) {
|
||||||
|
processLogChanges(changedFile.toFile());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
key.reset();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.getLogger().severe("Erro ao iniciar monitor de logs: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processLogChanges(File logFile) {
|
||||||
|
try (RandomAccessFile raf = new RandomAccessFile(logFile, "r")) {
|
||||||
|
// Pula para a última posição lida
|
||||||
|
if (raf.length() < lastReadPosition) {
|
||||||
|
lastReadPosition = 0; // Log foi reiniciado (ex: servidor reiniciou)
|
||||||
|
}
|
||||||
|
raf.seek(lastReadPosition);
|
||||||
|
|
||||||
|
String line;
|
||||||
|
while ((line = raf.readLine()) != null) {
|
||||||
|
line = new String(line.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
|
||||||
|
TKNLogs.getDiscord().sendMessage(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastReadPosition = raf.getFilePointer();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.getLogger().severe("Erro ao ler latest.log: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,16 +7,22 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
public final class TKNLogs extends JavaPlugin {
|
public final class TKNLogs extends JavaPlugin {
|
||||||
private static DiscordNotifier discord;
|
private static DiscordNotifier discord;
|
||||||
private static JavaPlugin plugin;
|
private static JavaPlugin plugin;
|
||||||
|
private LoggerReader logMonitor;
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
Bukkit.getPluginManager().registerEvents(new ChatListener(), this);
|
||||||
|
Bukkit.getPluginManager().registerEvents(new CommandListener(), this);
|
||||||
|
Bukkit.getPluginManager().registerEvents(new JoinQuitListener(), this);
|
||||||
|
logMonitor = new LoggerReader();
|
||||||
|
logMonitor.start();
|
||||||
plugin = this;
|
plugin = this;
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
reloadConfig();
|
reloadConfig();
|
||||||
|
|
||||||
discord = new DiscordNotifier();
|
discord = new DiscordNotifier();
|
||||||
Logger logger = Bukkit.getLogger();
|
Logger logger = Bukkit.getLogger();
|
||||||
logger.addHandler(new ErrorCatcher());
|
logger.addHandler(new ErrorCatcher());
|
||||||
|
ErrorCatcher.register();
|
||||||
|
getLogger().info("PLUGIN STARTED");
|
||||||
}
|
}
|
||||||
public static JavaPlugin getPlugin(){
|
public static JavaPlugin getPlugin(){
|
||||||
return plugin;
|
return plugin;
|
||||||
@@ -26,7 +32,7 @@ public final class TKNLogs extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
discord.sendMessage("Desligando o plugin");
|
discord.sendMessage("TURNING OFF PLUGIN");
|
||||||
discord.sendShutdown();
|
discord.sendShutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
discord:
|
discord:
|
||||||
token: ""
|
token: ""
|
||||||
channel-id: ""
|
channel: ""
|
||||||
Reference in New Issue
Block a user