Files
src
Code
21 2,491
src/main/java/com/gapteam/database/MySQLAdapter.java
package com.gapteam.database;
import com.gapteam.GapTeamPlugin;
import com.gapteam.model.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
public class MySQLAdapter implements DatabaseAdapter {
private final GapTeamPlugin plugin;
private final String host, database, username, password;
private final int port;
private Connection connection;
public MySQLAdapter(GapTeamPlugin plugin) {
this.plugin = plugin;
var config = plugin.getConfigManager().getConfig();
this.host = config.getString("database.host", "localhost");
this.port = config.getInt("database.port", 3306);
this.database = config.getString("database.database", "gapteam");
this.username = config.getString("database.username", "root");
this.password = config.getString("database.password", "");
}
@Override
public void connect() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://" + host + ":" + port + "/" + database +
"?useSSL=false&characterEncoding=utf8";
connection = DriverManager.getConnection(url, username, password);
createTables();
}
private void createTables() throws SQLException {
try (Statement stmt = connection.createStatement()) {
stmt.execute("""
CREATE TABLE IF NOT EXISTS teams (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(32) UNIQUE NOT NULL,
owner VARCHAR(36) NOT NULL,
level TINYINT DEFAULT 1,
balance DOUBLE DEFAULT 0,
home_world VARCHAR(64),
home_x DOUBLE, home_y DOUBLE, home_z DOUBLE,
home_yaw FLOAT, home_pitch FLOAT,
has_home BOOLEAN DEFAULT FALSE,
created_at BIGINT NOT NULL,
kills INT DEFAULT 0,
blocks_broken INT DEFAULT 0,
mobs_killed INT DEFAULT 0,
crops_harvested INT DEFAULT 0,
play_time BIGINT DEFAULT 0,
team_xp BIGINT DEFAULT 0,
particles_enabled BOOLEAN DEFAULT TRUE
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS team_members (
team_id INT NOT NULL,
player_uuid VARCHAR(36) NOT NULL,
rank VARCHAR(16) NOT NULL,
joined_at BIGINT NOT NULL,
PRIMARY KEY (team_id, player_uuid),
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS team_warps (
id INT AUTO_INCREMENT PRIMARY KEY,
team_id INT NOT NULL,
name VARCHAR(32) NOT NULL,
world VARCHAR(64) NOT NULL,
x DOUBLE NOT NULL, y DOUBLE NOT NULL, z DOUBLE NOT NULL,
yaw FLOAT, pitch FLOAT,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS team_transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
team_id INT NOT NULL,
player_uuid VARCHAR(36) NOT NULL,
amount DOUBLE NOT NULL,
type VARCHAR(16) NOT NULL,
timestamp BIGINT NOT NULL,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS team_storage (
team_id INT PRIMARY KEY,
contents LONGBLOB,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE
)
""");
}
}
@Override
public void disconnect() {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
plugin.getLogger().log(Level.WARNING, "Error closing database", e);
}
}
@Override
public boolean isConnected() {
try {
return connection != null && !connection.isClosed();
} catch (SQLException e) {
return false;
}
}
private Connection getConnection() throws SQLException {
if (!isConnected()) {
try {
connect();
} catch (Exception e) {
throw new SQLException("Cannot reconnect", e);
}
}
return connection;
}
private Team mapTeam(ResultSet rs) throws SQLException {
Team team = new Team();
team.setId(rs.getInt("id"));
team.setName(rs.getString("name"));
team.setOwner(UUID.fromString(rs.getString("owner")));
team.setLevel(TeamLevel.fromLevel(rs.getInt("level")));
team.setBalance(rs.getDouble("balance"));
team.setHomeWorld(rs.getString("home_world"));
team.setHomeX(rs.getDouble("home_x"));
team.setHomeY(rs.getDouble("home_y"));
team.setHomeZ(rs.getDouble("home_z"));
team.setHomeYaw(rs.getFloat("home_yaw"));
team.setHomePitch(rs.getFloat("home_pitch"));
team.setHasHome(rs.getBoolean("has_home"));
team.setCreatedAt(rs.getLong("created_at"));
team.setKills(rs.getInt("kills"));
team.setBlocksBroken(rs.getInt("blocks_broken"));
team.setMobsKilled(rs.getInt("mobs_killed"));
team.setCropsHarvested(rs.getInt("crops_harvested"));
team.setPlayTime(rs.getLong("play_time"));
team.setTeamXp(rs.getLong("team_xp"));
team.setParticlesEnabled(rs.getBoolean("particles_enabled"));
return team;
}
@Override
public void createTeam(Team team) {
String sql = "INSERT INTO teams (name, owner, level, balance, created_at, particles_enabled) VALUES (?,?,?,?,?,?)";
try (PreparedStatement ps = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, team.getName());
ps.setString(2, team.getOwner().toString());
ps.setInt(3, team.getLevel().getLevel());
ps.setDouble(4, team.getBalance());
ps.setLong(5, team.getCreatedAt());
ps.setBoolean(6, team.isParticlesEnabled());
ps.executeUpdate();
try (ResultSet keys = ps.getGeneratedKeys()) {
if (keys.next()) team.setId(keys.getInt(1));
}
saveMember(team.getId(), team.getMember(team.getOwner()));
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "Error creating team", e);
}
}
@Override
public void saveTeam(Team team) {
String sql = "UPDATE teams SET name=?, owner=?, level=?, balance=?, " +
"home_world=?, home_x=?, home_y=?, home_z=?, home_yaw=?, home_pitch=?, " +
"has_home=?, kills=?, blocks_broken=?, mobs_killed=?, crops_harvested=?, " +
"play_time=?, team_xp=?, particles_enabled=? WHERE id=?";
try (PreparedStatement ps = getConnection().prepareStatement(sql)) {
ps.setString(1, team.getName());
ps.setString(2, team.getOwner().toString());
ps.setInt(3, team.getLevel().getLevel());
ps.setDouble(4, team.getBalance());
if (team.getHomeLocation() != null) {
ps.setString(5, team.getHomeWorld());
ps.setDouble(6, team.getHomeX());
ps.setDouble(7, team.getHomeY());
ps.setDouble(8, team.getHomeZ());
ps.setFloat(9, team.getHomeYaw());
ps.setFloat(10, team.getHomePitch());
} else {
ps.setNull(5, Types.VARCHAR);
ps.setNull(6, Types.DOUBLE);
ps.setNull(7, Types.DOUBLE);
ps.setNull(8, Types.DOUBLE);