package me.domi.database; import a4.papers.chatfilter.chatfilter.shared.FilterWrapper; import java.sql.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MySQL { // WORDFILTER QUERIES private static final String WORDFILTER_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS chatfilter_wordfilter (" + "word TEXT NOT NULL," + "enabled BOOLEAN NOT NULL," + "regex TEXT NOT NULL," + "warn_staff BOOLEAN NOT NULL," + "warn_player BOOLEAN NOT NULL," + "warn_console BOOLEAN NOT NULL," + "cancelchat_cancel BOOLEAN NOT NULL," + "cancelchat_replace BOOLEAN NOT NULL," + "cancelchat_replacewith TEXT NOT NULL," + "action TEXT NOT NULL," + "addedby TEXT NOT NULL" + ");"; private static final String WORDFILTER_INSERT = "INSERT INTO chatfilter_wordfilter (" + "word, enabled, regex, warn_staff, warn_player, warn_console, cancelchat_cancel, cancelchat_replace, cancelchat_replacewith, action, addedby" + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; private static final String WORDFILTER_DELETE = "DELETE FROM chatfilter_wordfilter WHERE word=?;"; private static final String WORDFILTER_SELECT = "SELECT word, enabled, regex, warn_staff, warn_player, warn_console, cancelchat_cancel, cancelchat_replace, cancelchat_replacewith, action, addedby FROM chatfilter_wordfilter;"; private static final String WORDFILTER_COUNT = "SELECT COUNT(*) FROM chatfilter_wordfilter WHERE word=?;"; // UNICODEFILTER QUERIES private static final String UNICODEFILTER_CREATE_TABLE = ""; private static final String UNICODEFILTER_INSERT = ""; private static final String UNICODEFILTER_DELETE = ""; private static final String UNICODEFILTER_SELECT = ""; private static final String UNICODEFILTER_COUNT = ""; private Connection connection; private final String host; private final String port; private final String database; private final String username; private final String password; public MySQL(String host, String port, String database, String username, String password) { this.host = host; this.port = port; this.database = database; this.username = username; this.password = password; } public boolean isConnected() { return connection != null; } public void connect() { if (isConnected()) { System.err.println("Database already connected!"); return; } String url = "jdbc:mysql://" + host + ":" + port + "/" + database; try { connection = DriverManager.getConnection(url, username, password); System.out.println("Database connected!"); } catch (SQLException e) { System.err.println("Error(C): " + e.getMessage()); } } public void disconnect() { if (!isConnected()) { System.err.println("Database not connected!"); return; } try { connection.close(); connection = null; System.out.println("Database disconnected!"); } catch (SQLException e) { System.err.println("Error(D): " + e.getMessage()); } } public Connection getConnection() { try { if (connection == null || connection.isClosed()) { String url = "jdbc:mysql://" + host + ":" + port + "/" + database; connection = DriverManager.getConnection(url, username, password); System.out.println("Database reconnected!"); } } catch (SQLException e) { System.err.println("Error(GC): " + e.getMessage()); } return connection; } public void createTable() { if (!isConnected()) { System.err.println("Database not connected!"); return; } try (PreparedStatement preparedStatement = getConnection().prepareStatement(WORDFILTER_CREATE_TABLE)) { preparedStatement.executeUpdate(); System.out.println("Table created successfully!"); } catch (SQLException e) { System.err.println("Error(CT): " + e.getMessage()); } } public void addWordFilter(String word, boolean enabled, String regex, boolean warnStaff, boolean warnPlayer, boolean warnConsole, boolean cancelChatCancel, boolean cancelChatReplace, String cancelChatReplaceWith, String action, String addedBy) { if (!isConnected()) { System.err.println("Database not connected!"); return; } try (PreparedStatement preparedStatement = getConnection().prepareStatement(WORDFILTER_INSERT)) { preparedStatement.setString(1, word); preparedStatement.setBoolean(2, enabled); preparedStatement.setString(3, regex); preparedStatement.setBoolean(4, warnStaff); preparedStatement.setBoolean(5, warnPlayer); preparedStatement.setBoolean(6, warnConsole); preparedStatement.setBoolean(7, cancelChatCancel); preparedStatement.setBoolean(8, cancelChatReplace); preparedStatement.setString(9, cancelChatReplaceWith); preparedStatement.setString(10, action); preparedStatement.setString(11, addedBy); int rowsAffected = preparedStatement.executeUpdate(); System.out.println(rowsAffected > 0 ? "Word filter added successfully: " + word : "Failed to add word filter: " + word); } catch (SQLException e) { System.err.println("Error(AW): " + e.getMessage()); } } public void deleteWordFilter(String word) { if (!isConnected()) { System.err.println("Database not connected!"); return; } try (PreparedStatement preparedStatement = getConnection().prepareStatement(WORDFILTER_DELETE)) { preparedStatement.setString(1, word); int rowsAffected = preparedStatement.executeUpdate(); System.out.println(rowsAffected > 0 ? "Word filter deleted: " + word : "Word filter not found: " + word); } catch (SQLException e) { System.err.println("Error(DW): " + e.getMessage()); } } public List getWordFilters() { List filters = new ArrayList<>(); if (!isConnected()) { System.err.println("Database not connected!"); return filters; } try (PreparedStatement preparedStatement = getConnection().prepareStatement(WORDFILTER_SELECT); ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { String word = resultSet.getString("word"); boolean enabled = resultSet.getBoolean("enabled"); String regex = resultSet.getString("regex"); boolean warnStaff = resultSet.getBoolean("warn_staff"); boolean warnPlayer = resultSet.getBoolean("warn_player"); boolean warnConsole = resultSet.getBoolean("warn_console"); boolean cancelChatCancel = resultSet.getBoolean("cancelchat_cancel"); boolean cancelChatReplace = resultSet.getBoolean("cancelchat_replace"); String cancelChatReplaceWith = resultSet.getString("cancelchat_replacewith"); String action = resultSet.getString("action"); FilterWrapper filter = new FilterWrapper(word, Arrays.asList(action), regex, cancelChatCancel, cancelChatReplace, cancelChatReplaceWith, warnStaff, warnConsole, warnPlayer); if (enabled) filters.add(filter); } } catch (SQLException e) { System.err.println("Error(GWF): " + e.getMessage()); } return filters; } public boolean containsWord(String word) { if (!isConnected()) { System.err.println("Database not connected!"); return false; } try (PreparedStatement preparedStatement = getConnection().prepareStatement(WORDFILTER_COUNT)) { preparedStatement.setString(1, word); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { return resultSet.getInt(1) > 0; } } } catch (SQLException e) { System.err.println("Error(CW): " + e.getMessage()); } return false; } }