Refactoring Pandalib-bungee + added some source files from Pandacube sources

This commit is contained in:
Marc Baloup 2022-12-21 16:31:21 +01:00
parent a7aa012fa4
commit ebb7b5b4c5
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
9 changed files with 211 additions and 64 deletions

View File

@ -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-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` 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-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-netapi` A poorly designed, but working TCP network library;
- `pandalib-net` A better-designed, packet-based TCP network library (_still in development_); - `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); - `pandalib-commands` An abstract command manager working on top of [Brigadier](https://github.com/Mojang/brigadier);

View File

@ -1,56 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pandalib-parent</artifactId>
<groupId>fr.pandacube.lib</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pandalib-bungee-players</artifactId>
<packaging>jar</packaging>
<repositories>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-players</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-reflect</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>${bungeecord.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-protocol</artifactId>
<version>${bungeecord.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -21,17 +21,44 @@
</repositories> </repositories>
<dependencies> <dependencies>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-players</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-reflect</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-protocol</artifactId>
<version>${bungeecord.version}</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>net.md-5</groupId> <groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId> <artifactId>bungeecord-api</artifactId>
<version>${bungeecord.version}</version> <version>${bungeecord.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>fr.pandacube.lib</groupId> <groupId>net.md-5</groupId>
<artifactId>pandalib-core</artifactId> <artifactId>bungeecord-log</artifactId>
<version>${project.version}</version> <version>${bungeecord.version}</version>
<scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -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;
}
}

View File

@ -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;
}
}
}

View File

@ -1,4 +1,4 @@
package fr.pandacube.lib.bungee; package fr.pandacube.lib.bungee.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;

View File

@ -64,7 +64,6 @@
<module>pandalib-bungee</module> <module>pandalib-bungee</module>
<module>pandalib-bungee-commands</module> <module>pandalib-bungee-commands</module>
<module>pandalib-bungee-permissions</module> <module>pandalib-bungee-permissions</module>
<module>pandalib-bungee-players</module>
<module>pandalib-chat</module> <module>pandalib-chat</module>
<module>pandalib-cli</module> <module>pandalib-cli</module>
<module>pandalib-commands</module> <module>pandalib-commands</module>