This commit is contained in:
iTakinn
2025-03-16 19:56:21 -03:00
parent 4de0018c7d
commit 0ddc4ef324
2 changed files with 51 additions and 38 deletions
@@ -13,69 +13,81 @@ import static java.nio.file.StandardWatchEventKinds.*;
public class LoggerReader { public class LoggerReader {
private final JavaPlugin plugin; private final TKNLogs plugin;
private Path logDir; private long lastPosition = 0;
private Path logFilePath;
private WatchService watchService; private WatchService watchService;
private long lastReadPosition;
public LoggerReader(TKNLogs pl) { public LoggerReader(TKNLogs plugin) {
this.plugin = pl; this.plugin = plugin;
Path logDir = Paths.get(Bukkit.getServer().getWorldContainer().getAbsolutePath(), "logs"); File serverDir = Bukkit.getServer().getWorldContainer();
this.lastReadPosition = 0; this.logFilePath = Paths.get(serverDir.getAbsolutePath(), "logs", "latest.log");
} }
public void start() { public void start() {
try { if (!Files.exists(logFilePath)) {
plugin.getLogger().severe("Arquivo latest.log não encontrado!");
return;
}
try {
watchService = FileSystems.getDefault().newWatchService(); watchService = FileSystems.getDefault().newWatchService();
logDir.register(watchService, ENTRY_MODIFY); Path logDir = logFilePath.getParent();
logDir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
while (!Thread.currentThread().isInterrupted()) { try {
WatchKey key; while (!Thread.currentThread().isInterrupted()) {
try { WatchKey key = watchService.take();
key = watchService.take(); for (WatchEvent<?> event : key.pollEvents()) {
} catch (InterruptedException e) { if (event.context().toString().equals("latest.log")) {
Thread.currentThread().interrupt(); readNewLines();
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();
} }
key.reset(); } catch (InterruptedException e) {
Thread.currentThread().interrupt();
} }
}); });
} catch (IOException e) { } catch (IOException e) {
plugin.getLogger().severe("Erro ao iniciar monitor de logs: " + e.getMessage()); plugin.getLogger().severe("Erro ao iniciar monitor: " + e.getMessage());
} }
} }
private void processLogChanges(File logFile) { private void readNewLines() {
try (RandomAccessFile raf = new RandomAccessFile(logFile, "r")) { try (RandomAccessFile raf = new RandomAccessFile(logFilePath.toFile(), "r")) {
if (raf.length() < lastPosition) {
if (raf.length() < lastReadPosition) { lastPosition = 0;
lastReadPosition = 0;
} }
raf.seek(lastReadPosition);
raf.seek(lastPosition);
String line; String line;
while ((line = raf.readLine()) != null) { while ((line = raf.readLine()) != null) {
line = new String(line.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); String utf8Line = new String(line.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
TKNLogs.getDiscord().sendMessage(line); sendToDiscord(utf8Line);
} }
lastPosition = raf.getFilePointer();
lastReadPosition = raf.getFilePointer();
} catch (IOException e) { } catch (IOException e) {
plugin.getLogger().severe("Erro ao ler latest.log: " + e.getMessage()); plugin.getLogger().severe("Erro ao ler latest.log: " + e.getMessage());
} }
} }
}
private void sendToDiscord(String message) {
if (TKNLogs.getDiscord() != null) {
TKNLogs.getDiscord().sendMessage("```" + message + "```");
}
}
public void stop() {
try {
if (watchService != null) {
watchService.close();
}
} catch (IOException e) {
plugin.getLogger().severe("Erro ao parar monitor: " + e.getMessage());
}
}
}
@@ -12,15 +12,16 @@ public final class TKNLogs extends JavaPlugin {
public void onEnable() { public void onEnable() {
saveDefaultConfig(); saveDefaultConfig();
reloadConfig(); reloadConfig();
discord = new DiscordNotifier();
Bukkit.getPluginManager().registerEvents(new ChatListener(), this); Bukkit.getPluginManager().registerEvents(new ChatListener(), this);
Bukkit.getPluginManager().registerEvents(new CommandListener(), this); Bukkit.getPluginManager().registerEvents(new CommandListener(), this);
Bukkit.getPluginManager().registerEvents(new JoinQuitListener(), this); Bukkit.getPluginManager().registerEvents(new JoinQuitListener(), this);
logMonitor = new LoggerReader(this); logMonitor = new LoggerReader(this);
logMonitor.start(); logMonitor.start();
plugin = this;
Logger logger = Bukkit.getLogger(); Logger logger = Bukkit.getLogger();
logger.addHandler(new ErrorCatcher()); logger.addHandler(new ErrorCatcher());
ErrorCatcher.register(); ErrorCatcher.register();
discord = new DiscordNotifier();
getLogger().info("PLUGIN STARTED"); getLogger().info("PLUGIN STARTED");
} }
public static JavaPlugin getPlugin(){ public static JavaPlugin getPlugin(){