Connect to public partsltd_dev database for use of mod by other people.
This commit is contained in:
@@ -1,32 +1,87 @@
|
||||
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.util.UUID;
|
||||
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_USER = "root";
|
||||
private static final String DB_PASS = System.getenv("DB_PASSWORD"); // "password";
|
||||
private static final Logger LOGGER = Logger.getLogger(DatabaseManager.class.getName());
|
||||
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_dev";
|
||||
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());
|
||||
|
||||
/*
|
||||
public DatabaseManager() {
|
||||
config = new Properties();
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
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(DB_URL, DB_USER, DB_PASS);
|
||||
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 player_stats (player_uuid, stat_type, stat_value, item_id, timestamp) VALUES (?, ?, ?, ?, ?)")) {
|
||||
"INSERT INTO MCMPS_player_statistic (player_uuid, stat_type, stat_value, item_id, timestamp) VALUES (?, ?, ?, ?, ?)")) {
|
||||
|
||||
stmt.setString(1, playerUUID.toString());
|
||||
stmt.setString(2, statType);
|
||||
@@ -42,7 +97,7 @@ public class DatabaseManager {
|
||||
public void recordLocation(Player player) {
|
||||
try (Connection conn = getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(
|
||||
"INSERT INTO player_locations (player_uuid, x, y, z, dimension, timestamp) VALUES (?, ?, ?, ?, ?, ?)")) {
|
||||
"INSERT INTO MCMPS_player_locations (player_uuid, x, y, z, dimension, timestamp) VALUES (?, ?, ?, ?, ?, ?)")) {
|
||||
|
||||
stmt.setString(1, player.getUUID().toString());
|
||||
stmt.setDouble(2, player.getX());
|
||||
@@ -60,7 +115,7 @@ public class DatabaseManager {
|
||||
try (Connection conn = getConnection()) {
|
||||
// Create tables
|
||||
conn.createStatement().execute("""
|
||||
CREATE TABLE IF NOT EXISTS player_stats (
|
||||
CREATE TABLE IF NOT EXISTS MCMPS_player_statistic (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
player_uuid VARCHAR(36),
|
||||
stat_type VARCHAR(50),
|
||||
@@ -73,7 +128,7 @@ public class DatabaseManager {
|
||||
""");
|
||||
|
||||
conn.createStatement().execute("""
|
||||
CREATE TABLE IF NOT EXISTS player_locations (
|
||||
CREATE TABLE IF NOT EXISTS MCMPS_player_locations (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
player_uuid VARCHAR(36),
|
||||
x DOUBLE,
|
||||
|
||||
Reference in New Issue
Block a user