mirror of
https://github.com/Aust1n46/VentureChat.git
synced 2025-05-22 18:09:06 +00:00
Refactor MySQL database logging implementation.
This commit is contained in:
parent
80cb47c03c
commit
c663c49e9c
@ -1,7 +1,3 @@
|
||||
/*
|
||||
* VentureChat plugin for Minecraft servers running Bukkit or Spigot software.
|
||||
* @author Aust1n46
|
||||
*/
|
||||
package mineverse.Aust1n46.chat;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@ -12,9 +8,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@ -86,7 +80,6 @@ import mineverse.Aust1n46.chat.command.mute.Muteall;
|
||||
import mineverse.Aust1n46.chat.command.mute.Unmute;
|
||||
import mineverse.Aust1n46.chat.command.mute.Unmuteall;
|
||||
import mineverse.Aust1n46.chat.database.Database;
|
||||
import mineverse.Aust1n46.chat.database.MySQL;
|
||||
import mineverse.Aust1n46.chat.database.PlayerData;
|
||||
import mineverse.Aust1n46.chat.gui.GuiSlotInfo;
|
||||
import mineverse.Aust1n46.chat.utilities.Format;
|
||||
@ -98,8 +91,6 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
@ -114,6 +105,10 @@ import com.comphenix.protocol.utility.MinecraftReflection;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
|
||||
/**
|
||||
* VentureChat Minecraft plugin for servers running Spigot or Paper software.
|
||||
* @author Aust1n46
|
||||
*/
|
||||
public class MineverseChat extends JavaPlugin implements PluginMessageListener {
|
||||
// Listeners --------------------------------
|
||||
private ChatListener chatListener;
|
||||
@ -136,9 +131,6 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
|
||||
private MineverseCommandExecutor commandExecutor;
|
||||
private Map<String, MineverseCommand> commands = new HashMap<String, MineverseCommand>();
|
||||
|
||||
// Database ------------------------------------
|
||||
public Database db = null;
|
||||
|
||||
// Misc --------------------------------
|
||||
public static AliasInfo aaInfo;
|
||||
public static JsonFormatInfo jfInfo;
|
||||
@ -236,16 +228,7 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
|
||||
MineverseChatAPI.addMineverseChatOnlinePlayerToMap(mcp);
|
||||
}
|
||||
|
||||
FileConfiguration config = getConfig();
|
||||
ConfigurationSection mysqlConfig = config.getConfigurationSection("mysql");
|
||||
if (this.getConfig().getConfigurationSection("mysql").getBoolean("enabled")) {
|
||||
String host = mysqlConfig.getString("host");
|
||||
int port = mysqlConfig.getInt("port");
|
||||
String database = mysqlConfig.getString("database");
|
||||
String user = mysqlConfig.getString("user");
|
||||
String password = mysqlConfig.getString("password");
|
||||
db = new MySQL(host, port, database, user, password);
|
||||
}
|
||||
Database.initializeMySQL();
|
||||
|
||||
commands.put("broadcast", new Broadcast("broadcast"));
|
||||
commands.put("channel", new Channel("channel"));
|
||||
@ -684,11 +667,8 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
|
||||
|
||||
Bukkit.getConsoleSender().sendMessage(consoleChat);
|
||||
|
||||
if(db != null) {
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = formatter.format(currentDate.getTime());
|
||||
db.writeVentureChat(date, senderUUID.toString(), senderName, server, chatchannel, chat.replace("'", "''"), "Chat");
|
||||
if(Database.isEnabled()) {
|
||||
Database.writeVentureChat(senderUUID.toString(), senderName, server, chatchannel, chat.replace("'", "''"), "Chat");
|
||||
}
|
||||
|
||||
for(MineverseChatPlayer p : MineverseChatAPI.getOnlineMineverseChatPlayers()) {
|
||||
|
@ -3,27 +3,72 @@ package mineverse.Aust1n46.chat.database;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import mineverse.Aust1n46.chat.MineverseChat;
|
||||
|
||||
//Parent class for both the MySQL and SQLite database classes.
|
||||
public abstract class Database {
|
||||
/**
|
||||
* Initializes and handles writing to the chat logging database.
|
||||
*/
|
||||
public class Database {
|
||||
private static HikariDataSource dataSource = null;
|
||||
|
||||
public static void initializeMySQL() {
|
||||
ConfigurationSection mysqlConfig = MineverseChat.getInstance().getConfig().getConfigurationSection("mysql");
|
||||
if (mysqlConfig.getBoolean("enabled", false)) {
|
||||
String host = mysqlConfig.getString("host");
|
||||
int port = mysqlConfig.getInt("port");
|
||||
String database = mysqlConfig.getString("database");
|
||||
String user = mysqlConfig.getString("user");
|
||||
String password = mysqlConfig.getString("password");
|
||||
|
||||
final HikariConfig config = new HikariConfig();
|
||||
//config.setDriverClassName(org.postgresql.Driver.class.getName());
|
||||
//final String jdbcUrl = String.format("jdbc:postgresql://%s:%d/%s", hostname, port, database);
|
||||
final String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s?autoReconnect=true&useSSL=false", host, port, database);
|
||||
config.setJdbcUrl(jdbcUrl);
|
||||
config.setUsername(user);
|
||||
config.setPassword(password);
|
||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||
dataSource = new HikariDataSource(config);
|
||||
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS VentureChat " +
|
||||
"(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, " +
|
||||
"Server TEXT, Channel TEXT, Text TEXT, Type TEXT)";
|
||||
try (final Connection conn = dataSource.getConnection();
|
||||
final PreparedStatement statement = conn.prepareStatement(SQL_CREATE_TABLE)) {
|
||||
statement.executeUpdate();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isEnabled() {
|
||||
return dataSource != null;
|
||||
}
|
||||
|
||||
protected HikariDataSource dataSource = null;
|
||||
|
||||
public void writeVentureChat(String time, String uuid, String name, String server, String channel, String text, String type) {
|
||||
public static void writeVentureChat(String uuid, String name, String server, String channel, String text, String type) {
|
||||
MineverseChat plugin = MineverseChat.getInstance();
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = formatter.format(currentDate.getTime());
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try(final Connection conn = dataSource.getConnection();
|
||||
final PreparedStatement statement = conn.prepareStatement(
|
||||
"INSERT INTO VentureChat " +
|
||||
"(ChatTime, UUID, Name, Server, Channel, Text, Type) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?)")) {
|
||||
statement.setString(1, time);
|
||||
statement.setString(1, date);
|
||||
statement.setString(2, uuid);
|
||||
statement.setString(3, name);
|
||||
statement.setString(4, server);
|
||||
@ -32,9 +77,9 @@ public abstract class Database {
|
||||
statement.setString(7, type);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
catch(SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
catch(SQLException error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,36 +0,0 @@
|
||||
package mineverse.Aust1n46.chat.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
//This class initializes the plugin's connection to the MySQL database if it's enabled.
|
||||
public class MySQL extends Database {
|
||||
|
||||
public MySQL(String hostname, int port, String database, String user, String password) {
|
||||
final HikariConfig config = new HikariConfig();
|
||||
//config.setDriverClassName(org.postgresql.Driver.class.getName());
|
||||
//final String jdbcUrl = String.format("jdbc:postgresql://%s:%d/%s", hostname, port, database);
|
||||
final String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s", hostname, port, database);
|
||||
config.setJdbcUrl(jdbcUrl);
|
||||
config.setUsername(user);
|
||||
config.setPassword(password);
|
||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||
dataSource = new HikariDataSource(config);
|
||||
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS VentureChat " +
|
||||
"(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, " +
|
||||
"Server TEXT, Channel TEXT, Text TEXT, Type TEXT)";
|
||||
try (final Connection conn = dataSource.getConnection();
|
||||
final PreparedStatement statement = conn.prepareStatement(SQL_CREATE_TABLE)) {
|
||||
statement.executeUpdate();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package mineverse.Aust1n46.chat.database;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import mineverse.Aust1n46.chat.MineverseChat;
|
||||
|
||||
//This class initializes the connection to a SQLite database, which has no implementations currently in the plugin.
|
||||
public class SQLite extends Database {
|
||||
|
||||
public SQLite(String dbLocation) {
|
||||
File dataFolder = MineverseChat.getInstance().getDataFolder();
|
||||
if (!dataFolder.exists()) dataFolder.mkdirs();
|
||||
File databaseFile = new File(dataFolder, dbLocation);
|
||||
try {
|
||||
if (!databaseFile.exists()) databaseFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
HikariConfig config = new HikariConfig();
|
||||
final String jdbcUrl = String.format("jdbc:sqlite:%s", databaseFile);
|
||||
config.setJdbcUrl(jdbcUrl);
|
||||
dataSource = new HikariDataSource(config);
|
||||
}
|
||||
}
|
@ -2,8 +2,6 @@ package mineverse.Aust1n46.chat.listeners;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@ -30,6 +28,7 @@ import mineverse.Aust1n46.chat.api.events.ChannelJoinEvent;
|
||||
import mineverse.Aust1n46.chat.api.events.VentureChatEvent;
|
||||
import mineverse.Aust1n46.chat.channel.ChatChannel;
|
||||
import mineverse.Aust1n46.chat.command.mute.MuteContainer;
|
||||
import mineverse.Aust1n46.chat.database.Database;
|
||||
import mineverse.Aust1n46.chat.localization.LocalizedMessage;
|
||||
import mineverse.Aust1n46.chat.utilities.Format;
|
||||
import mineverse.Aust1n46.chat.versions.VersionHandler;
|
||||
@ -154,11 +153,8 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
mcp.setReplyPlayer(tp.getUUID());
|
||||
tp.setReplyPlayer(mcp.getUUID());
|
||||
if(plugin.db != null) {
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = formatter.format(currentDate.getTime());
|
||||
plugin.db.writeVentureChat(date, mcp.getUUID().toString(), mcp.getName(), "Local", "Messaging_Component", chat.replace("'", "''"), "Chat");
|
||||
if(Database.isEnabled()) {
|
||||
Database.writeVentureChat(mcp.getUUID().toString(), mcp.getName(), "Local", "Messaging_Component", chat.replace("'", "''"), "Chat");
|
||||
}
|
||||
}
|
||||
return;
|
||||
@ -193,11 +189,8 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
}
|
||||
Bukkit.getConsoleSender().sendMessage(partyformat);
|
||||
if(plugin.db != null) {
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = formatter.format(currentDate.getTime());
|
||||
plugin.db.writeVentureChat(date, mcp.getUUID().toString(), mcp.getName(), "Local", "Party_Component", chat.replace("'", "''"), "Chat");
|
||||
if(Database.isEnabled()) {
|
||||
Database.writeVentureChat(mcp.getUUID().toString(), mcp.getName(), "Local", "Party_Component", chat.replace("'", "''"), "Chat");
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -526,11 +519,8 @@ public class ChatListener implements Listener {
|
||||
boolean bungee = event.isBungee();
|
||||
|
||||
if(!bungee) {
|
||||
if(plugin.db != null) {
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = formatter.format(currentDate.getTime());
|
||||
plugin.db.writeVentureChat(date, mcp.getUUID().toString(), mcp.getName(), "Local", channel.getName(), chat.replace("'", "''"), "Chat");
|
||||
if(Database.isEnabled()) {
|
||||
Database.writeVentureChat(mcp.getUUID().toString(), mcp.getName(), "Local", channel.getName(), chat.replace("'", "''"), "Chat");
|
||||
}
|
||||
|
||||
if(recipientCount == 1) {
|
||||
|
@ -1,8 +1,6 @@
|
||||
package mineverse.Aust1n46.chat.listeners;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
import mineverse.Aust1n46.chat.MineverseChat;
|
||||
import mineverse.Aust1n46.chat.alias.Alias;
|
||||
@ -10,6 +8,7 @@ import mineverse.Aust1n46.chat.alias.AliasInfo;
|
||||
import mineverse.Aust1n46.chat.api.MineverseChatAPI;
|
||||
import mineverse.Aust1n46.chat.api.MineverseChatPlayer;
|
||||
import mineverse.Aust1n46.chat.channel.ChatChannel;
|
||||
import mineverse.Aust1n46.chat.database.Database;
|
||||
import mineverse.Aust1n46.chat.gui.GuiSlot;
|
||||
import mineverse.Aust1n46.chat.localization.LocalizedMessage;
|
||||
import mineverse.Aust1n46.chat.utilities.Format;
|
||||
@ -75,30 +74,9 @@ public class CommandListener implements CommandExecutor, Listener {
|
||||
}
|
||||
|
||||
String message = event.getMessage();
|
||||
/*
|
||||
* boolean cus = false; if((message.startsWith("/pl") ||
|
||||
* message.startsWith("/plugins")) &&
|
||||
* plugin.getConfig().getBoolean("modifypluginlist", true)) {
|
||||
* if(message.contains(" ")) { if(message.split(" ")[0].equals("/pl") ||
|
||||
* message.split(" ")[0].equals("/plugins")) { cus = true; } }
|
||||
* if(message.equals("/pl") || message.equals("/plugins")) { cus = true;
|
||||
* } if(cus && mcp.getPlayer().hasPermission("bukkit.command.plugins"))
|
||||
* { String pluginlist = ""; for(Plugin p :
|
||||
* Bukkit.getPluginManager().getPlugins()) { pluginlist +=
|
||||
* ChatColor.GREEN + p.getName().replace("VentureChat",
|
||||
* plugin.getConfig().getString("pluginname", "VentureChat")) +
|
||||
* ChatColor.WHITE + ", "; } if(pluginlist.length() > 2) { pluginlist =
|
||||
* pluginlist.substring(0, pluginlist.length() - 2); }
|
||||
* mcp.getPlayer().sendMessage("Plugins (" +
|
||||
* Bukkit.getPluginManager().getPlugins().length + "): " + pluginlist);
|
||||
* event.setCancelled(true); return; } }
|
||||
*/
|
||||
|
||||
if(plugin.db != null) {
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = formatter.format(currentDate.getTime());
|
||||
plugin.db.writeVentureChat(date, mcp.getUUID().toString(), mcp.getName(), "Local", "Command_Component", event.getMessage().replace("'", "''"), "Command");
|
||||
if(Database.isEnabled()) {
|
||||
Database.writeVentureChat(mcp.getUUID().toString(), mcp.getName(), "Local", "Command_Component", event.getMessage().replace("'", "''"), "Command");
|
||||
}
|
||||
|
||||
for(Alias a : aa.getAliases()) {
|
||||
@ -209,11 +187,8 @@ public class CommandListener implements CommandExecutor, Listener {
|
||||
//old 1.8 command map
|
||||
@EventHandler
|
||||
public void onServerCommand(ServerCommandEvent event) {
|
||||
if (plugin.db != null) {
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = formatter.format(currentDate.getTime());
|
||||
plugin.db.writeVentureChat(date, "N/A", "Console", "Local", "Command_Component", event.getCommand().replace("'", "''") , "Command");
|
||||
if (Database.isEnabled()) {
|
||||
Database.writeVentureChat("N/A", "Console", "Local", "Command_Component", event.getCommand().replace("'", "''") , "Command");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user