package minecraft_mod_player_statistics.src.main.java; import minecraft_mod_player_statistics.src.main.java.PlayerStatisticsMod; // import minecraft_mod_player_statistics.src.main.java.ConfigManager; import net.minecraft.world.entity.player.Player; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.time.Instant; import java.util.UUID; import java.util.logging.Logger; import java.util.logging.Level; import java.util.Properties; public class DatabaseManager { // protected final Properties config; /* Local connection private static final String DB_URL = "jdbc:mysql://localhost:3306/partsltd_dev"; private static final String DB_USERNAME = "root"; private static final String DB_PASSWORD = System.getenv("DB_PASSWORD"); // "password"; */ /* Public connection */ protected static final String DB_URL = "jdbc:mysql://185.221.63.17/partsltd_minecraft"; protected static final String DB_USERNAME = "partsltd_minecraft_mod"; protected static final String DB_PASSWORD = "nipples_are_always_erect"; protected static final Logger LOGGER = Logger.getLogger(DatabaseManager.class.getName()); static { try { // Explicitly load the MySQL driver Class.forName("com.mysql.cj.jdbc.Driver"); LOGGER.info("MySQL JDBC Driver registered successfully"); } catch (ClassNotFoundException e) { LOGGER.severe("Failed to load MySQL JDBC driver: " + e.getMessage()); throw new RuntimeException("Failed to load MySQL JDBC driver", e); } } /* protected void loadConfig() { File configFile = new File("config/playerstats/database.properties"); if (!configFile.exists()) { configFile.getParentFile().mkdirs(); createDefaultConfig(configFile); LOGGER.warning("Please configure database settings in config/playerstats/database.properties"); } try (FileInputStream fis = new FileInputStream(configFile)) { config.load(fis); } catch (IOException e) { LOGGER.severe("Failed to load database config: " + e.getMessage()); } } protected void createDefaultConfig(File configFile) { try (FileOutputStream fos = new FileOutputStream(configFile)) { Properties defaultProps = new Properties(); defaultProps.setProperty("db.url", "jdbc:mysql://localhost:3306/partsltd_dev"); defaultProps.setProperty("db.user", "change_me"); defaultProps.setProperty("db.password", "change_me"); defaultProps.store(fos, "PlayerStats Database Configuration"); } catch (IOException e) { LOGGER.severe("Failed to create default config: " + e.getMessage()); } } public Connection getConnection() throws SQLException { return DriverManager.getConnection( config.getProperty("db.url"), config.getProperty("db.user"), config.getProperty("db.password") ); } */ public Connection getConnection() throws SQLException { return DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); } public void recordStat(UUID playerUUID, String statType, int value, String itemId) { try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement( "INSERT INTO MCMPS_player_statistic (player_uuid, stat_type, stat_value, item_id, timestamp) VALUES (?, ?, ?, ?, ?)")) { stmt.setString(1, playerUUID.toString()); stmt.setString(2, statType); stmt.setInt(3, value); stmt.setString(4, itemId); stmt.setTimestamp(5, java.sql.Timestamp.from(Instant.now())); stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void recordLocation(Player player) { try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement( "INSERT INTO MCMPS_player_location (player_uuid, x, y, z, dimension, timestamp) VALUES (?, ?, ?, ?, ?, ?)")) { stmt.setString(1, player.getUUID().toString()); stmt.setDouble(2, player.getX()); stmt.setDouble(3, player.getY()); stmt.setDouble(4, player.getZ()); stmt.setString(5, player.level.dimension().location().toString()); stmt.setTimestamp(6, java.sql.Timestamp.from(Instant.now())); stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void initializeDatabase() { try (Connection conn = getConnection()) { /* // Create tables conn.createStatement().execute(""" CREATE TABLE IF NOT EXISTS MCMPS_player_statistic ( id BIGINT AUTO_INCREMENT PRIMARY KEY, player_uuid VARCHAR(36), stat_type VARCHAR(50), stat_value INT, item_id VARCHAR(100), timestamp TIMESTAMP, INDEX idx_player_uuid (player_uuid), INDEX idx_stat_type (stat_type) ) """); conn.createStatement().execute(""" CREATE TABLE IF NOT EXISTS MCMPS_player_location ( id BIGINT AUTO_INCREMENT PRIMARY KEY, player_uuid VARCHAR(36), x DOUBLE, y DOUBLE, z DOUBLE, dimension VARCHAR(50), timestamp TIMESTAMP, INDEX idx_player_uuid (player_uuid) ) """); */ LOGGER.log(Level.INFO, "Database initialised"); } catch (SQLException e) { LOGGER.log(Level.INFO, "Database initialisation error"); e.printStackTrace(); } } }