2020-10-21 18:49:37 -04:00
|
|
|
package party._2a03.mc.util;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
2020-10-25 11:35:00 -04:00
|
|
|
import java.sql.Statement;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
2020-10-21 18:49:37 -04:00
|
|
|
import java.sql.SQLException;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2020-10-25 11:35:00 -04:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import party._2a03.mc.util.PlayerData;
|
|
|
|
import party._2a03.mc.util.PlayerPosition;
|
2020-10-21 18:49:37 -04:00
|
|
|
|
2023-05-31 06:29:52 -04:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2020-10-21 18:49:37 -04:00
|
|
|
public class Database {
|
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
private static Connection conn = null;
|
|
|
|
private static File file;
|
|
|
|
|
|
|
|
public static void init(File configDir) {
|
|
|
|
file = new File(configDir, "2a03.db");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void open() throws Exception {
|
|
|
|
LOGGER.info("Loading 2a03.party database");
|
|
|
|
String url = "jdbc:sqlite:";
|
|
|
|
url = url.concat(file.getPath());
|
|
|
|
conn = DriverManager.getConnection(url);
|
2020-10-25 11:35:00 -04:00
|
|
|
try {
|
|
|
|
Statement stmt;
|
|
|
|
stmt = conn.createStatement();
|
|
|
|
stmt.executeUpdate(
|
|
|
|
"CREATE TABLE IF NOT EXISTS players (" +
|
2023-05-31 06:29:52 -04:00
|
|
|
"uuid TEXT NOT NULL UNIQUE," +
|
|
|
|
"name TEXT," +
|
|
|
|
"skinval TEXT," +
|
|
|
|
"skinsig TEXT," +
|
|
|
|
"home TEXT," +
|
|
|
|
"asn INTEGER," +
|
2020-10-25 11:35:00 -04:00
|
|
|
"PRIMARY KEY(uuid)" +
|
|
|
|
");");
|
2020-10-27 02:45:26 -04:00
|
|
|
stmt.executeUpdate(
|
|
|
|
"CREATE TABLE IF NOT EXISTS claims (" +
|
|
|
|
"id TEXT NOT NULL UNIQUE," +
|
|
|
|
"owner TEXT NOT NULL," +
|
|
|
|
"members TEXT NOT NULL," +
|
|
|
|
"rules TEXT NOT NULL," +
|
|
|
|
"PRIMARY KEY(id)" +
|
|
|
|
");");
|
|
|
|
stmt.executeUpdate(
|
|
|
|
"CREATE TABLE IF NOT EXISTS areas (" +
|
|
|
|
"chunk_x INTEGER NOT NULL," +
|
|
|
|
"chunk_z INTEGER NOT NULL," +
|
|
|
|
"min_x INTEGER NOT NULL," +
|
|
|
|
"min_z INTEGER NOT NULL," +
|
|
|
|
"max_x INTEGER NOT NULL," +
|
|
|
|
"max_z INTEGER NOT NULL," +
|
|
|
|
"claim TEXT NOT NULL" +
|
|
|
|
");");
|
2020-10-25 11:35:00 -04:00
|
|
|
stmt.close();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
}
|
2020-10-21 18:49:37 -04:00
|
|
|
LOGGER.info("Database loaded");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void close() {
|
|
|
|
LOGGER.info("Unloading 2a03.party database");
|
|
|
|
try {
|
|
|
|
if (conn != null)
|
|
|
|
conn.close();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 11:35:00 -04:00
|
|
|
|
2023-05-31 06:29:52 -04:00
|
|
|
public static PlayerData getPlayer(UUID uuid) {
|
|
|
|
return getPlayer(uuid.toString());
|
|
|
|
}
|
|
|
|
|
2020-10-25 11:35:00 -04:00
|
|
|
public static PlayerData getPlayer(String uuid) {
|
|
|
|
try {
|
|
|
|
PreparedStatement pstmt;
|
|
|
|
pstmt = conn.prepareStatement(
|
2023-05-31 06:29:52 -04:00
|
|
|
"SELECT home, name, skinval, skinsig FROM players WHERE uuid = ?;");
|
2020-10-25 11:35:00 -04:00
|
|
|
pstmt.setString(1, uuid);
|
|
|
|
ResultSet res = pstmt.executeQuery();
|
|
|
|
if (res.next()) {
|
2023-05-31 06:29:52 -04:00
|
|
|
String homeData = res.getString("home");
|
|
|
|
PlayerPosition home = new PlayerPosition();
|
|
|
|
if (homeData != "" && homeData != null)
|
|
|
|
home = new PlayerPosition(new JSONArray(homeData));
|
|
|
|
String name = res.getString("name");
|
|
|
|
String skinval = res.getString("skinval");
|
|
|
|
String skinsig = res.getString("skinsig");
|
|
|
|
return new PlayerData(uuid, home, name, skinval, skinsig);
|
2020-10-25 11:35:00 -04:00
|
|
|
}
|
|
|
|
pstmt.close();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
return new PlayerData(uuid);
|
|
|
|
}
|
|
|
|
|
2023-05-31 06:29:52 -04:00
|
|
|
public static void setPlayerTextures(String uuid, String skinval, String skinsig) {
|
|
|
|
try {
|
|
|
|
PreparedStatement pstmt;
|
|
|
|
pstmt = conn.prepareStatement(
|
|
|
|
"INSERT INTO players (uuid, skinval, skinsig) " +
|
|
|
|
"VALUES(?, ?, ?) " +
|
|
|
|
"ON CONFLICT(uuid) " +
|
|
|
|
"DO UPDATE SET skinval=?, skinsig=?;");
|
|
|
|
pstmt.setString(1, uuid);
|
|
|
|
pstmt.setString(2, skinval);
|
|
|
|
pstmt.setString(3, skinsig);
|
|
|
|
pstmt.setString(4, skinval);
|
|
|
|
pstmt.setString(5, skinsig);
|
|
|
|
pstmt.executeUpdate();
|
|
|
|
pstmt.close();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setPlayerName(String uuid, String name) {
|
|
|
|
try {
|
|
|
|
PreparedStatement pstmt;
|
|
|
|
pstmt = conn.prepareStatement(
|
|
|
|
"INSERT INTO players (uuid, name) " +
|
|
|
|
"VALUES(?, ?) " +
|
|
|
|
"ON CONFLICT(uuid) " +
|
|
|
|
"DO UPDATE SET name=?;");
|
|
|
|
pstmt.setString(1, uuid);
|
|
|
|
pstmt.setString(2, name);
|
|
|
|
pstmt.setString(3, name);
|
|
|
|
pstmt.executeUpdate();
|
|
|
|
pstmt.close();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 11:35:00 -04:00
|
|
|
public static void setPlayerHome(String uuid, PlayerPosition home) {
|
|
|
|
try {
|
|
|
|
PreparedStatement pstmt;
|
|
|
|
pstmt = conn.prepareStatement(
|
|
|
|
"INSERT INTO players (uuid, home) " +
|
|
|
|
"VALUES(?, ?) " +
|
|
|
|
"ON CONFLICT(uuid) " +
|
|
|
|
"DO UPDATE SET home=?;");
|
|
|
|
pstmt.setString(1, uuid);
|
|
|
|
pstmt.setString(2, home.getJSON().toString());
|
|
|
|
pstmt.setString(3, home.getJSON().toString());
|
|
|
|
pstmt.executeUpdate();
|
|
|
|
pstmt.close();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 18:49:37 -04:00
|
|
|
}
|