Initial commit
This commit is contained in:
93
src/main/java/DatabaseManager.java
Normal file
93
src/main/java/DatabaseManager.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package minecraft_mod_player_statistics.src.main.java;
|
||||
|
||||
import minecraft_mod_player_statistics.src.main.java.PlayerStatisticsMod;
|
||||
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
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.logging.Logger;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class DatabaseManager {
|
||||
private static final String DB_URL = "jdbc:mysql://localhost:3306/minecraft_stats";
|
||||
private static final String DB_USER = "root";
|
||||
private static final String DB_PASS = "password";
|
||||
private static final Logger LOGGER = Logger.getLogger(DatabaseManager.class.getName());
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
}
|
||||
|
||||
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 (?, ?, ?, ?, ?)")) {
|
||||
|
||||
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 player_locations (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 player_stats (
|
||||
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 player_locations (
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user