Implement chat log handler with database support
The code changes introduce the ability to log chat messages. A new ChatLogHandler class has been added that manages the queue of chat log messages, both storing them in memory and writing them to a database. New columns have been added to the database and the interactivity with the database is handled using prepared statements to improve security and performance. The chat messages are deleted from the database after a certain period, which can be configured.
This commit is contained in:
@@ -5,14 +5,12 @@ import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.config.ServerConfig;
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import com.alttd.chat.handler.ChatHandler;
|
||||
import com.alttd.chat.listeners.BookListener;
|
||||
import com.alttd.chat.listeners.ChatListener;
|
||||
import com.alttd.chat.listeners.PlayerListener;
|
||||
import com.alttd.chat.listeners.PluginMessage;
|
||||
import com.alttd.chat.listeners.*;
|
||||
import com.alttd.chat.nicknames.Nicknames;
|
||||
import com.alttd.chat.nicknames.NicknamesEvents;
|
||||
import com.alttd.chat.objects.channels.Channel;
|
||||
import com.alttd.chat.objects.channels.CustomChannel;
|
||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import com.alttd.chat.util.Utility;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -41,7 +39,8 @@ public class ChatPlugin extends JavaPlugin {
|
||||
chatHandler = new ChatHandler();
|
||||
DatabaseConnection.initialize();
|
||||
serverConfig = new ServerConfig(Bukkit.getServerName());
|
||||
registerListener(new PlayerListener(serverConfig), new ChatListener(), new BookListener());
|
||||
ChatLogHandler chatLogHandler = ChatLogHandler.getInstance();
|
||||
registerListener(new PlayerListener(serverConfig), new ChatListener(chatLogHandler), new BookListener(), new ShutdownListener(chatLogHandler));
|
||||
if(serverConfig.GLOBALCHAT) {
|
||||
registerCommand("globalchat", new GlobalChat());
|
||||
registerCommand("toggleglobalchat", new ToggleGlobalChat());
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.alttd.chat.handler.ChatHandler;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.managers.RegexManager;
|
||||
import com.alttd.chat.objects.*;
|
||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import com.alttd.chat.util.GalaxyUtility;
|
||||
import com.alttd.chat.util.Utility;
|
||||
@@ -37,6 +38,11 @@ import java.util.stream.Collectors;
|
||||
public class ChatListener implements Listener {
|
||||
|
||||
private final PlainTextComponentSerializer plainTextComponentSerializer = PlainTextComponentSerializer.plainText();
|
||||
private final ChatLogHandler chatLogHandler;
|
||||
|
||||
public ChatListener(ChatLogHandler chatLogHandler) {
|
||||
this.chatLogHandler = chatLogHandler;
|
||||
}
|
||||
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
@@ -102,6 +108,7 @@ public class ChatListener implements Listener {
|
||||
GalaxyUtility.sendBlockedNotification("Language", player,
|
||||
modifiableString.component(),
|
||||
"");
|
||||
chatLogHandler.addChatLog(player.getUniqueId(), player.getServer().getServerName(), PlainTextComponentSerializer.plainText().serialize(input), true);
|
||||
return; // the message was blocked
|
||||
}
|
||||
|
||||
@@ -115,6 +122,7 @@ public class ChatListener implements Listener {
|
||||
for (Player pingPlayer : playersToPing) {
|
||||
pingPlayer.playSound(pingPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1);
|
||||
}
|
||||
chatLogHandler.addChatLog(player.getUniqueId(), player.getServer().getServerName(), modifiableString.string(), false);
|
||||
ALogger.info(PlainTextComponentSerializer.plainText().serialize(input));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.alttd.chat.listeners;
|
||||
|
||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
|
||||
public class ShutdownListener implements Listener {
|
||||
|
||||
private final ChatLogHandler chatLogHandler;
|
||||
|
||||
public ShutdownListener(ChatLogHandler chatLogHandler) {
|
||||
this.chatLogHandler = chatLogHandler;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onShutdown(PluginDisableEvent event) {
|
||||
chatLogHandler.shutDown();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user