Refactoring Pandalib-bungee + added some source files from Pandacube sources
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,113 @@
|
||||
package fr.pandacube.lib.bungee.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.connection.Server;
|
||||
import net.md_5.bungee.api.event.PluginMessageEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.api.plugin.PluginManager;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
/**
|
||||
* Utility class for Bungee plugins that pass the requested plugin message channels through the bungeecord instance.
|
||||
* By default, plugin messages that are not registered in BungeeCord are dropped.
|
||||
* <p>
|
||||
* Usage example, in your plugin init code:
|
||||
* <pre>{@code
|
||||
* PluginMessagePassthrough.init(yourPluginInstance);
|
||||
* PluginMessagePassthrough.register("worldedit:cui"); // plugin message used by WorldEdit
|
||||
* }</pre>
|
||||
*/
|
||||
public class PluginMessagePassthrough implements Listener {
|
||||
private static final List<String> channels = Collections.synchronizedList(new ArrayList<>());
|
||||
private static final PluginMessagePassthrough instance = new PluginMessagePassthrough();
|
||||
|
||||
/**
|
||||
* Initialize the {@link PluginMessagePassthrough}.
|
||||
* It registers the required event listener.
|
||||
* @param plugin the plugin instance.
|
||||
*/
|
||||
public static void init(Plugin plugin) {
|
||||
PluginManager pm = ProxyServer.getInstance().getPluginManager();
|
||||
pm.unregisterListener(instance);
|
||||
pm.registerListener(plugin, instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the list of passed through plugin message channels.
|
||||
*/
|
||||
public static void clear() {
|
||||
synchronized (channels) {
|
||||
unregisterAll(channels.toArray(new String[0]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds all the provided plugin message channels to pass through.
|
||||
* @param channels the channels to register.
|
||||
*/
|
||||
public static void registerAll(String... channels) {
|
||||
for (String channel : channels)
|
||||
register(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all the provided plugin message channels from pass through.
|
||||
* @param channels the channels to unregister.
|
||||
*/
|
||||
public static void unregisterAll(String... channels) {
|
||||
for (String channel : channels)
|
||||
unregister(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the provided plugin message to pass through.
|
||||
* @param channel the channel to register.
|
||||
*/
|
||||
public static void register(String channel) {
|
||||
synchronized (channels) {
|
||||
if (channels.contains(channel))
|
||||
return;
|
||||
ProxyServer.getInstance().registerChannel(channel);
|
||||
channels.add(channel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the provided plugin message channel from pass through.
|
||||
* @param channel the channel to unregister.
|
||||
*/
|
||||
public static void unregister(String channel) {
|
||||
synchronized (channels) {
|
||||
if (!channels.contains(channel))
|
||||
return;
|
||||
ProxyServer.getInstance().unregisterChannel(channel);
|
||||
channels.remove(channel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private PluginMessagePassthrough() { }
|
||||
|
||||
|
||||
/**
|
||||
* Event handler for {@link PluginMessageEvent}.
|
||||
* @param event the event.
|
||||
*/
|
||||
@EventHandler
|
||||
public void onPluginMessage(PluginMessageEvent event) {
|
||||
String channel = event.getTag();
|
||||
if (!channels.contains(channel))
|
||||
return;
|
||||
if (event.getReceiver() instanceof ProxiedPlayer pp) {
|
||||
pp.sendData(channel, event.getData());
|
||||
} else if (event.getReceiver() instanceof Server sv) {
|
||||
sv.sendData(channel, event.getData());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user