From ebb7b5b4c5357b0f10c72acc7b9f5c99b00caf2a Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Wed, 21 Dec 2022 16:31:21 +0100 Subject: [PATCH] Refactoring Pandalib-bungee + added some source files from Pandacube sources --- Readme.md | 2 - pandalib-bungee-players/pom.xml | 56 ------- pandalib-bungee/pom.xml | 35 +++- .../pandacube/lib/bungee/PandaLibBungee.java | 29 ++++ .../lib/bungee/players/BungeeOffPlayer.java | 0 .../bungee/players/BungeeOnlinePlayer.java | 0 .../util/DailyLogRotateFileHandler.java | 150 ++++++++++++++++++ .../{ => util}/PluginMessagePassthrough.java | 2 +- pom.xml | 1 - 9 files changed, 211 insertions(+), 64 deletions(-) delete mode 100644 pandalib-bungee-players/pom.xml create mode 100644 pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/PandaLibBungee.java rename {pandalib-bungee-players => pandalib-bungee}/src/main/java/fr/pandacube/lib/bungee/players/BungeeOffPlayer.java (100%) rename {pandalib-bungee-players => pandalib-bungee}/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java (100%) create mode 100644 pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/util/DailyLogRotateFileHandler.java rename pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/{ => util}/PluginMessagePassthrough.java (98%) diff --git a/Readme.md b/Readme.md index 40a7204..e3d1dd4 100644 --- a/Readme.md +++ b/Readme.md @@ -18,8 +18,6 @@ that are detailed in their respective Readme file (if any). - `pandalib-paper-permissions` Integration of the permission system `pandalib-permissions` into Bukkit, Vault and WEPIF permission systems; - `pandalib-players` A library to handle classes representing online or offline players; - `pandalib-players-permissible` An extension of `pandalib-players` with support for the permission system `pandalib-permissions`; -- `pandalib-bungee-players` A partial extension and implementation of `pandalib-players` for Bungeecord plugin; -- `pandalib-paper-players` A partial extension and implementation of `pandalib-players` for Paper plugin; - `pandalib-netapi` A poorly designed, but working TCP network library; - `pandalib-net` A better-designed, packet-based TCP network library (_still in development_); - `pandalib-commands` An abstract command manager working on top of [Brigadier](https://github.com/Mojang/brigadier); diff --git a/pandalib-bungee-players/pom.xml b/pandalib-bungee-players/pom.xml deleted file mode 100644 index 6269ba6..0000000 --- a/pandalib-bungee-players/pom.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - - pandalib-parent - fr.pandacube.lib - 1.0-SNAPSHOT - ../pom.xml - - 4.0.0 - - pandalib-bungee-players - jar - - - - bungeecord-repo - https://oss.sonatype.org/content/repositories/snapshots - - - - - - fr.pandacube.lib - pandalib-util - ${project.version} - - - fr.pandacube.lib - pandalib-players - ${project.version} - - - fr.pandacube.lib - pandalib-reflect - ${project.version} - - - - net.md-5 - bungeecord-api - ${bungeecord.version} - provided - - - - net.md-5 - bungeecord-protocol - ${bungeecord.version} - provided - - - - - \ No newline at end of file diff --git a/pandalib-bungee/pom.xml b/pandalib-bungee/pom.xml index a090412..738f22e 100644 --- a/pandalib-bungee/pom.xml +++ b/pandalib-bungee/pom.xml @@ -21,17 +21,44 @@ + + fr.pandacube.lib + pandalib-core + ${project.version} + + + fr.pandacube.lib + pandalib-util + ${project.version} + + + fr.pandacube.lib + pandalib-players + ${project.version} + + + fr.pandacube.lib + pandalib-reflect + ${project.version} + + + + net.md-5 + bungeecord-protocol + ${bungeecord.version} + provided + net.md-5 bungeecord-api ${bungeecord.version} provided - - fr.pandacube.lib - pandalib-core - ${project.version} + net.md-5 + bungeecord-log + ${bungeecord.version} + provided diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/PandaLibBungee.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/PandaLibBungee.java new file mode 100644 index 0000000..8214fe1 --- /dev/null +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/PandaLibBungee.java @@ -0,0 +1,29 @@ +package fr.pandacube.lib.bungee; + +import fr.pandacube.lib.bungee.util.DailyLogRotateFileHandler; +import fr.pandacube.lib.bungee.util.PluginMessagePassthrough; +import net.md_5.bungee.api.plugin.Plugin; + +public class PandaLibBungee { + + private static Plugin plugin; + + public static void onLoad(Plugin plugin) { + PandaLibBungee.plugin = plugin; + + } + + public static void onEnable() { + PluginMessagePassthrough.init(plugin); + DailyLogRotateFileHandler.init(true); + } + + + public static void disable() { + + } + + public static Plugin getPlugin() { + return plugin; + } +} diff --git a/pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOffPlayer.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/players/BungeeOffPlayer.java similarity index 100% rename from pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOffPlayer.java rename to pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/players/BungeeOffPlayer.java diff --git a/pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java similarity index 100% rename from pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java rename to pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/util/DailyLogRotateFileHandler.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/util/DailyLogRotateFileHandler.java new file mode 100644 index 0000000..543344e --- /dev/null +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/util/DailyLogRotateFileHandler.java @@ -0,0 +1,150 @@ +package fr.pandacube.lib.bungee.util; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.ThreadFactory; +import java.util.logging.ErrorManager; +import java.util.logging.Filter; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.zip.GZIPOutputStream; + +import com.google.common.io.Files; +import fr.pandacube.lib.bungee.PandaLibBungee; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.log.ConciseFormatter; + +public class DailyLogRotateFileHandler extends Handler { + + private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + + public static void init(boolean hideInitialHandlerLogEntries) { + ProxyServer.getInstance().getLogger().addHandler(new DailyLogRotateFileHandler(hideInitialHandlerLogEntries)); + } + + + private BufferedWriter currentFile = null; + private String currentFileDate = getCurrentDay(); + private boolean closed = false; + + private DailyLogRotateFileHandler(boolean hideInitialHandlerLogEntries) { + if (hideInitialHandlerLogEntries) + setFilter(new InitialHandlerLogRemover()); + setFormatter(new ConciseFormatter(false)); + setLevel(Level.parse(System.getProperty("net.md_5.bungee.file-log-level", "INFO"))); + } + + @Override + public synchronized void close() throws SecurityException { + closed = true; + if (currentFile != null) try { + currentFile.close(); + } catch (IOException ignored) { + } + } + + @Override + public synchronized void flush() { + if (closed) return; + if (currentFile == null) return; + try { + currentFile.flush(); + } catch (IOException ignored) { + } + } + + @Override + public synchronized void publish(LogRecord record) { + if (closed || !isLoggable(record)) + return; + + if (currentFile == null || !currentFileDate.equals(getCurrentDay())) + changeFile(); + + if (currentFile == null) + return; + + String formattedMessage; + + try { + formattedMessage = getFormatter().format(record); + } catch (Exception ex) { + reportError(null, ex, ErrorManager.FORMAT_FAILURE); + return; + } + + try { + currentFile.write(formattedMessage); + currentFile.flush(); + } catch (Exception ex) { + reportError(null, ex, ErrorManager.WRITE_FAILURE); + } + } + + private void changeFile() { + if (currentFile != null) { + try { + currentFile.flush(); + currentFile.close(); + } catch (IOException ignored) { + } + File fileNewName = new File("logs/" + currentFileDate + ".log"); + new File("logs/latest.log").renameTo(fileNewName); + ProxyServer.getInstance().getScheduler().runAsync(PandaLibBungee.getPlugin(), () -> compress(fileNewName)); + } + + currentFileDate = getCurrentDay(); + try { + File logDir = new File("logs"); + logDir.mkdir(); + currentFile = new BufferedWriter(new FileWriter("logs/latest.log", true)); + } catch (SecurityException | IOException e) { + reportError("Erreur lors de l'initialisation d'un fichier log", e, ErrorManager.OPEN_FAILURE); + currentFile = null; + } + } + + private String getCurrentDay() { + return dateFormat.format(new Date()); + } + + + private void compress(File sourceFile) { + File destFile = new File(sourceFile.getParentFile(), sourceFile.getName() + ".gz"); + if (destFile.exists()) + destFile.delete(); + try (GZIPOutputStream os = new GZIPOutputStream(new FileOutputStream(destFile))) { + Files.copy(sourceFile, os); + } catch (IOException e) { + if (destFile.exists()) + destFile.delete(); + throw new RuntimeException(e); + } + sourceFile.delete(); + + } + + + + private class InitialHandlerLogRemover implements Filter { + + @Override + public boolean isLoggable(LogRecord record) { + String formattedRecord = getFormatter().format(record); + if (formattedRecord.contains("<-> InitialHandler has connected")) return false; + if (formattedRecord.contains("<-> InitialHandler has pinged")) return false; + return true; + } + + } +} diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/PluginMessagePassthrough.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java similarity index 98% rename from pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/PluginMessagePassthrough.java rename to pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java index fa40ab7..1495aa6 100644 --- a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/PluginMessagePassthrough.java +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java @@ -1,4 +1,4 @@ -package fr.pandacube.lib.bungee; +package fr.pandacube.lib.bungee.util; import java.util.ArrayList; import java.util.Collections; diff --git a/pom.xml b/pom.xml index 6a8412d..7b0c8bf 100644 --- a/pom.xml +++ b/pom.xml @@ -64,7 +64,6 @@ pandalib-bungee pandalib-bungee-commands pandalib-bungee-permissions - pandalib-bungee-players pandalib-chat pandalib-cli pandalib-commands